package com.u2ware.springfield.sample.part3.step2; import lombok.Getter; import lombok.Setter; import lombok.ToString; import com.u2ware.springfield.config.Springfield; import com.u2ware.springfield.config.Springfield.Strategy; import com.u2ware.springfield.repository.QueryMethod; import com.u2ware.springfield.sample.part3.step1.TargetBean; @Springfield( strategy=Strategy.JPA, entity=TargetBean.class ) @QueryMethod("findByIdAndPasswordOrderByAgeDesc") public @ToString class CustomBean { @Getter @Setter private String id; @Getter @Setter private String password; }
package com.u2ware.springfield.sample.part3.step2; 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.EntityPageable; import com.u2ware.springfield.repository.EntityRepository; import com.u2ware.springfield.sample.part3.step1.TargetBean; import com.u2ware.springfield.service.EntityServiceImpl; @Service public class CustomBeanService extends EntityServiceImpl<TargetBean, CustomBean>{ @Autowired @Qualifier("targetBeanRepository") private EntityRepository<TargetBean, String> targetBeanRepository; @Override protected EntityRepository<TargetBean, ?> getRepository() { return targetBeanRepository; } @Override @Transactional public Iterable<?> findForm(CustomBean request, EntityPageable pageable) { logger.debug("Overide findForm "); return super.findForm(request, pageable); } }
package com.u2ware.springfield.sample.part3.step2; import org.springframework.stereotype.Service; import org.springframework.validation.Errors; import org.springframework.validation.ValidationUtils; import com.u2ware.springfield.sample.part3.step1.TargetBean; import com.u2ware.springfield.validation.EntityValidatorImpl; @Service public class CustomBeanValidator extends EntityValidatorImpl<TargetBean, CustomBean>{ @Override public void create(TargetBean target, Errors errors) { super.create(target, errors); //JSR-303 logger.debug("Overide create "); ValidationUtils.rejectIfEmpty(errors, "password", "errorCode"); } }
@Springfield(strategy=Strategy.JPA, entity=TargetBean.class) 선언으로 다음 3개의 Bean 이 등록 된다. 자동생성 하려는 Bean(Name)이 이미 존재한다면, @Springfield 는 Bean 을 따로 생성하지 않는다.
bean name | bean type | bean implement |
---|---|---|
"customBeanController" | EntityController<TargetBean, CustomBean> | com.u2ware.springfield.controller. EntityHandler |
"customBeanValidator" | EntityValidator<TargetBean, CustomBean> | com.u2ware.springfield.sample.part3.step2. CustomBeanValidator |
"customBeanService" | EntityService<TargetBean, CustomBean> | com.u2ware.springfield.sample.part3.step2. CustomBeanService |
매핑 경로 | HTTP Method | Controller Method 이름 | 예제 Command 객체 | 예제 매핑 경로 |
---|---|---|---|---|
/{topLevelMapping}/ | GET | home() | CustomBean | /part3/step2/ |
/{topLevelMapping} | GET | findForm() | CustomBean | /part3/step2 |
/{topLevelMapping} | POST | find() | CustomBean | /part3/step2 |
/{topLevelMapping}/{id} | GET | read() | TargetBean | /part3/step2/bar |
/{topLevelMapping}/new | GET | createForm() | TargetBean | /part3/step2/new |
/{topLevelMapping}/new | POST | create() | TargetBean | /part3/step2/new |
/{topLevelMapping}/{id}/edit | GET | updateForm() | TargetBean | /part3/step2/bar/eidt |
/{topLevelMapping}/{id}/edit | PUT | update() | TargetBean | /part3/step2/bar/eidt |
/{topLevelMapping}/{id}/edit | DELETE | delete() | TargetBean | /part3/step2/bar/eidt |
Bean Name 규칙이 맞도록, EntityValidator 의 구현체를 작성하고 등록한다. EntityValidatorImpl 를 상속하여 구현이 가능하다.
EntityValidatorImpl 은 JSR-303 Bean Validation 을 처리하는 Validator 컴퍼넌트이다
예제에서, 중복키 여부를 체크하여 에러를 reject 하는 방식이 사용되었는데, 이는 org.springframework.validation.Validator 의 구현방식과 동일하다.