Class Name
1. VO class 만들기
EmployeeVO
//긁지않은 개발자
@AllArgsConstructor //여기에 필드에 쓴 모든생성자만 만들어준다.
@NoArgsConstructor //기본 생성자를 만들어준다.
@Data // getter, setter 만들어준다.
public class EmployeeVO{
private int id;
private String name;
private LocalDate Date;
}
lombok
@AllArgsConstructor : 여기에 필드에 쓴 모든생성자만 만들어준다.
@NoArgsConstructor : 기본 생성자를 만들어준다.
@Data : getter, setter 만들어준다.
2. DAO
interface
EmployeeDao
//긁지않은 개발자
public interface EmployeeDao {
void saveInDatebase(EmployeeVO employeeVO);
}
EmployeeDaoImpl
//긁지않은 개발자
//@Repository DAO clss에서 쓰인다.
//비지니스 로직을 수행하는 Class라는 것을 나타내는 용도이다.
@Repository("employeeDao")
public class EmployeeDaoImpl implements EmployeeDao{
@Override
public void saveInDatebase(EmployeeVO employeeVO) {
System.out.println(employeeVO + "저장 합니다.");
}
}
@Repository DAO clss에서 쓰인다.
비지니스 로직을 수행하는 Class라는 것을 나타내는 용도이다.
3. Service
interface
DateService
//긁지않은 개발자
import java.time.LocalDate;
public interface DateService {
LocalDate getDate();
}
DateServiceImpl
//긁지않은 개발자
// @Service Service Class에서 쓰인다.
// 비지니스 로직을 수행하는 Class라는 것을 나타내는 용도 이다.
@Service("dateService")
public class DateServiceImpl implements DateService{
public LocalDate getDate() {
return LocalDate.of(2023, 06, 14);
}
}
interface
EmployeeService
//긁지않은 개발자
public interface EmployeeService {
void serviceEmployee(EmployeeVO employeeVO);
}
EmployeeServiceImpl
//긁지않은 개발자
@Service("employeeService")
public class EmployeeServiceImpl implements EmployeeService{
@Autowired
private DateService dateService;
@Autowired
private EmployeeDao employeeDao;
@Override
public void serviceEmployee(EmployeeVO employeeVO) {
employeeVO.setDate(dateService.getDate());
employeeDao.saveInDatebase(employeeVO);
}
}
@Autowired
속성, setter method, constructor(생성자)에서 사용하며 Name, Type에 따라 알아서 Bean을 주입 해준다.
무조건적인 객체에 대한 의존성을 주입시킨다.
이 @annotation을 사용할 시, 스프링이 자동적으로 값을 할당한다.
controller 클래스에서 DAO나 Swrvice에 관한 객체들을 주입 시킬 때 많이 사용한다.
Name 우순 선위 못찾으면 Type으로
EmployeeController
//긁지않은개발자
@Controller
public class EmployeeController {
@Autowired
private EmployeeService employeeService;
public void save(EmployeeVO employeeVO) {
employeeService.serviceEmployee(employeeVO);
}
}
@Controller
spring의 Controller를 의미한다.
Spring MVC에서 Controller클래스에서 쓰인다.
APPConfig
@Configuration
@ComponentScan(basePackages = {"kr.hcat.app"})
public class APPConfig {
}
@Configuration
설정파일을 만들기 위한 @Annotation or Bean을 등록하기 위한 Annotation
@ComponentScan
@Component와 @Service, @Repository, @Controller, @Configuration이 붙은 클래스
Bean들을 찾아서 Context에 bean등록을 해주는 Annotation이다.
@Component Annotation이 있는 클래스에 대하여 bean 인스턴스를 생성
ApplicationContext.xml에 <bean id="jeongpro" class="jeongpro" /> 과 같이 xml에 bean을 직접등록하는 방법도 있고 위와 같이 Annotation을 붙여서 하는 방법도 있다.
@Component
@Component 어노테이션을 이용하면 Bean Configuration 파일에 Bean을 따로 등록하지 않아도 사용할 수 있다.
빈 등록자체를 빈 클래스 자체에다가 할 수 있다는 의미이다.
AppMain(실행)
//긁지않은 개발자
public class AppMain {
public static void main(String[] args) {
//ACAC
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
context.scan("kr.hcat.app");
context.refresh(); // 꼭 써주기
EmployeeVO vo = new EmployeeVO();
vo.setId(2023);
vo.setName("긁지않은개발자");
EmployeeService employeeService = context.getBean("employeeService",EmployeeService.class);
employeeService.serviceEmployee(vo);
EmployeeController employeecontroller = context.getBean("employeeController", EmployeeController.class);
employeecontroller.save(vo);
context.close();
}
}
실행 결과
EmployeeVO(id=2023, name=긁지않은개발자, Date=2023-06-14)저장 합니다.
EmployeeVO(id=2023, name=긁지않은개발자, Date=2023-06-14)저장 합니다.
기존의 방법
JavaClass 실행할때
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(JavaClassName.class);
Xml 실행할때
AbstractApplicationContext context = new ClassPathXmlApplicationContext("XmlName.xml");
이 포스팅에서 쓴 방법
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
context.scan("PackageName");
context.refresh();
'SPING > Spring 이용하기' 카테고리의 다른 글
[Spring] interface 클래스 여러 클래스가 implements할 경우 (0) | 2023.06.14 |
---|---|
[Spring] Spring @Component뜻과 사용법 (0) | 2023.06.14 |
[Spring] Spring 이메일, SNS 메세지 보내기 (0) | 2023.06.13 |
[Spring] Spring 암호화 , 2가지방법 (0) | 2023.06.13 |
[Spring] Spring 문자 메세지 전송 출력 (0) | 2023.06.13 |