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);
}
}
@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 |
| 매핑 경로 | 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}/edit | GET | updateForm() | TargetBean | /part3/step1/foo/eidt |
| /{topLevelMapping}/{id}/edit | PUT | update() | TargetBean | /part3/step1/foo/eidt |
| /{topLevelMapping}/{id}/edit | DELETE | delete() | TargetBean | /part3/step1/foo/eidt |
Bean Name 규칙이 맞도록, EntityService 의 구현체를 작성하고 등록한다. EntityServiceImpl 를 상속하여 구현이 가능하다.
EntityServiceImpl 은 EntityRepository 를 주입 받아서 Repository 를 이용한 기본적인 CRUD 를 처리하는 Service 컴퍼넌트이다.
com.u2ware.springfield.validation. RejectableException이 발생하면 EntityController 에서 이를 Validation results Object ( org.springframework.validation. BindingResult) 에 reject 한다.