@PostConstruct
의존성 객체 주입 후 초기화 작업을 해주는 어노테이션
해당 어노테이션을 지정한 메소드는 생성자 역할을 한다
@PreDestroy
빈즈 객체 소멸 전에 실행된다
@Component
스프링 컨테이너가 직접 클래스를 검색해서 빈으로 등록해주는 기능으로
환경파일에서 빈으로 등록하지 않아도 원하는 클래스를 빈으로 등록할 수 있다
<!--xml파일-->
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/..../spring-beans-4.2.xsd">
<context:component-scan base-package="com.spring.annotation"></context:component-scan>
<!-- @Required, @Autowired, @Resource -->
<!--
<bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor" />
<bean class="org.springframework.beans.factory.annotation.RequiredAnnotationBeanPostProcessor" />
<bean class="org.springframework.context.annotation.CommonAnnotationBeanPostProcessor" />
<bean id="test1" class="com.spring.annotation.Test1" />
<bean id="test2" class="com.spring.annotation.Test2" />
-->
</beans>
-> 환경 파일에서 빈으로 등록한 부분을 모두 주석처리하고 context 네임스페이스를 추가해준다
<context:component-scan base-package="패키지명"/>
어떤 패키지를 스캔 대상으로 할 것인지 지정해준다
자바 클래스 파일에 @Component 어노테이션을 지정해, 어떤 클래스를 자동으로 등록되도록
할 것인지 정해준다
디폴트로 빈즈객체 id값은 클래스명으로 하고, 다른 값으로 정해주고 싶다면 아래와 같이 적어준다
@Component("id값")
@Configuration
xml대신 자바빈클래스가 환경설정을 해주는 역할을 할 수 있도록 설정하는 어노테이션
@Bean
<bean></bean> 와 같이 빈을 등록시켜주는 기능을 가진 어노테이션
<!--(1)-->
<!--xml 파일-->
<bean id="getBean" class="com.spring.MyBeanImpl"/>
//(2)
//자바 클래스 파일
@Bean
public MyBean getBean() {
return new MyBeanImpl();
}
xml 환경 파일에서의 (1)과 자바빈 클래스 파일에서의 (2)는 동일한 기능을 한다
'Spring' 카테고리의 다른 글
에러) The superclass "javax.servlet.http.HttpServlet" was not found on the Java Build Path (0) | 2021.04.08 |
---|---|
[Spring]Mybatis 개념, 특징(+jdbc) (0) | 2021.04.05 |
[Spring]어노테이션)@Required, @Autowired, @Inject, @Resource (0) | 2021.03.31 |
[Spring]DI설정) byType, byName, 추상, 상속 (0) | 2021.03.29 |
[Spring]DI설정) Set, Properties, Map 객체 자료형 (0) | 2021.03.29 |