@Springfield - Override

Code

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

import javax.persistence.Entity;
import javax.persistence.Id;
import javax.validation.constraints.NotNull;

import lombok.Getter;
import lombok.Setter;

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

@Springfield(
	strategy=Strategy.JPA
)
@Entity
public class TargetBean {

	@Id
	@Getter @Setter private @NotNull String id;
	@Getter @Setter private @NotNull String password;
	@Getter @Setter private @NotNull String name;
	@Getter @Setter private @NotNull Integer age;
}
		
package com.u2ware.springfield.sample.part3.step1;

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.repository.EntityRepository;
import com.u2ware.springfield.service.EntityServiceImpl;
import com.u2ware.springfield.validation.RejectableException;

@Service
public class TargetBeanService extends EntityServiceImpl<TargetBean, TargetBean> {

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

	@Override
	protected EntityRepository<TargetBean, String> getRepository() {
		return targetBeanRepository;
	}
	
	@Override
	@Transactional
	public TargetBean create(TargetBean entity) {
		if(getRepository().exists(entity)){
			throw new RejectableException("id" , "Duplication", "중복키입니다.");
		}
		return getRepository().create(entity);
	}
	
}
		

Beans

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

bean name bean type bean implement
"targetBeanController"EntityController<TargetBean, TargetBean>com.u2ware.springfield.controller. EntityHandler
"targetBeanValidator" EntityValidator<TargetBean, TargetBean> com.u2ware.springfield.validation. EntityValidatorImpl
"targetBeanService" EntityService<TargetBean, TargetBean> com.u2ware.springfield.sample.part3.step1. TargetBeanService
"targetBeanRepository"EntityRepository<TargetBean, String>com.u2ware.springfield.repository.jpa. EntityJpaRepository

RequestMapping

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

Service Layer

Bean Name 규칙이 맞도록, EntityService 의 구현체를 작성하고 등록한다. EntityServiceImpl 를 상속하여 구현이 가능하다.

com.u2ware.springfield.service.EntityServiceImpl

EntityServiceImplEntityRepository 를 주입 받아서 Repository 를 이용한 기본적인 CRUD 를 처리하는 Service 컴퍼넌트이다.

Validation Layer

com.u2ware.springfield.validation. RejectableException이 발생하면 EntityController 에서 이를 Validation results Object ( org.springframework.validation. BindingResult) 에 reject 한다.

Note
@Springfield 에서 Validation 처리는 두가지 방법을 적절히 이용할 수 있다.
  1. RejectableException 을 이용하여 서비스 레이어에서 예외를 던지는 방식. 주로 비즈니스 의존적인 검증 프로세스에 쓰인다.
  2. EntityValidator 에서 처리하는 방식. 예제