@Springfield - Override

Code

package com.u2ware.springfield.sample.part3.step3;

import lombok.Getter;
import lombok.Setter;

import com.u2ware.springfield.config.Springfield;
import com.u2ware.springfield.config.Springfield.Strategy;


@Springfield(
	strategy=Strategy.DTO,
	identity={"id"}
)
public class FormBean {

	@Getter @Setter private String id;
	@Getter @Setter private Integer age;
}
		
package com.u2ware.springfield.sample.part3.step3;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.u2ware.springfield.domain.EntityPageImpl;
import com.u2ware.springfield.domain.EntityPageable;
import com.u2ware.springfield.repository.EntityRepository;
import com.u2ware.springfield.sample.part3.step1.TargetBean;
import com.u2ware.springfield.service.EntityService;

@Service
public class FormBeanService implements EntityService<FormBean, FormBean>{

	protected final Log logger = LogFactory.getLog(getClass());

	@Autowired 
	@Qualifier("targetBeanRepository")
	protected EntityRepository<TargetBean, String> targetBeanRepository;

	@Override
	@Transactional
	public Iterable<?> findForm(FormBean query, EntityPageable pageable) {
		return find(query, pageable);
	}

	@Override
	@Transactional
	public Iterable<?> find(FormBean query, EntityPageable pageable) {
		if(pageable != null && pageable.isEnable()){
			return new EntityPageImpl<TargetBean>(targetBeanRepository.findAll(query, pageable));
		}else{
			return targetBeanRepository.findAll(query, pageable);
		}
	}
	
	@Override
	public Object home(FormBean query) {
		return query;
	}

	@Override
	public FormBean createForm(FormBean entity) {
		return entity;
	}

	@Override
	public FormBean updateForm(FormBean entity) {
		return entity;
	}

	@Override
	@Transactional
	public FormBean read(FormBean entity) {
		TargetBean target = targetBeanRepository.read(entity.getId());
		entity.setId(target.getId());
		entity.setAge(target.getAge());
		return entity;
	}

	
	@Override
	@Transactional
	public FormBean create(FormBean entity) {
		TargetBean target = new TargetBean();
		target.setId(entity.getId());
		target.setPassword("password"+entity.getId());
		target.setName("name"+entity.getId());
		target.setAge(entity.getAge());

		targetBeanRepository.create(target);
		return entity;
	}


	@Override
	@Transactional
	public FormBean update(FormBean entity) {
		TargetBean target = targetBeanRepository.read(entity.getId());
		target.setId(entity.getId());
		target.setAge(entity.getAge());
		return entity;
	}

	@Override
	@Transactional
	public FormBean delete(FormBean entity) {
		targetBeanRepository.delete(entity.getId());
		return entity;
	}

}
		

Beans

@Springfield(strategy=Strategy.DTO) 선언으로 다음 3개의 Bean 이 등록 된다.선언으로 다음 3개의 Bean 이 등록 된다. 자동생성 하려는 Bean(Name)이 이미 존재한다면, @Springfield 는 Bean 을 따로 생성하지 않는다.

bean name bean type bean implement
"formBeanController"EntityController<FormBean, FormBean>com.u2ware.springfield.controller. EntityHandler
"formBeanValidator" EntityValidator<FormBean, FormBean> com.u2ware.springfield.validation. EntityValidatorImpl
"formBeanService" EntityService<FormBean, FormBean> com.u2ware.springfield.sample.part3.step3. FormBeanService
Note:
FormBeanService 에서 주입받은 "targetBeanRepository" Bean 은 이전 예제에서 이미 등록되어 있다.

RequestMapping

매핑 경로 HTTP Method Controller Method 이름 예제 Command 객체 예제 매핑 경로
/{topLevelMapping}/ GET home() FormBean/part3/step3/
/{topLevelMapping} GET findForm() FormBean/part3/step3
/{topLevelMapping} POST find() FormBean/part3/step3
/{topLevelMapping}/{id} GET read() FormBean/part3/step3/baz
/{topLevelMapping}/new GET createForm()FormBean/part3/step3/new
/{topLevelMapping}/new POST create() FormBean/part3/step3/new
/{topLevelMapping}/{id}/editGET updateForm()FormBean/part3/step3/baz/eidt
/{topLevelMapping}/{id}/editPUT update() FormBean/part3/step3/baz/eidt
/{topLevelMapping}/{id}/editDELETEdelete() FormBean/part3/step3/baz/eidt

Service Layer

@Springfield(strategy=Strategy.DTO) 에 의해 생성된 EntityService 는 EntityRepository 를 주입 받지 않는다.

com.u2ware.springfield.service.EntityServiceImpl

EntityServiceImplEntityRepository 를 주입 받지 않았다면, 아무 동작도 하지않는 Dummy Service 가 된다.
따라서, 여러 Repository 들을 주입 받아서 이를 확장하면 자유롭게 비즈니스 로직의 구현이 가능하다.

예제에서는 FormBean 으로 TargetBean 을 CRUD 하는 서비스를 구현하고 있다.