본문 바로가기
Back-end/Spring Framework

java spring 다양한 의존 객체 주입 (DI) , 초기메서드-destroy메서드

by javapp 자바앱 2020. 7. 24.
728x90

.xml

1. 생성자로 주입

       public StudentRegisterService(StudentDao studentDao) {

             this.studentDao = studentDao;

       }

 

--> 주입 객체를 먼저 등록하고   <constructor-arg> 로 감싼다.

       <bean id="studentDao" class="ems.member.dao.StudentDao" ></bean>

      

       <bean id="registerService" class="ems.member.service.StudentRegisterService">

             <constructor-arg ref="studentDao" ></constructor-arg>

       </bean>

 

 

 

2. setter 로 주입

 

public class DataBaseConnectionInfo {

       private String jdbcUrl;

       private String userId;

       private String userPw;

      

       public String getJdbcUrl() {

             return jdbcUrl;

       }

       public void setJdbcUrl(String jdbcUrl) {

             this.jdbcUrl = jdbcUrl;

       }

       public String getUserId() {

             return userId;

       }

       public void setUserId(String userId) {

             this.userId = userId;

       }

       public String getUserPw() {

             return userPw;

       }

       public void setUserPw(String userPw) {

             this.userPw = userPw;

       }      

}

 

-->

       <!-- setJdbcUrl --><!-- set 이용한 주입 -->

       <bean id="dataBaseConnectionInfoDev" class="ems.member.DataBaseConnectionInfo">

             <property name="jdbcUrl" value="jdbc:oracle:thin:@localhost:1521:xe" />

             <property name="userId" value="scott" />

             <property name="userPw" value="tiger" />

       </bean>

 

3. 프로펄티가 List일 경우

       public void setDevelopers(List<String> developers) {

             this.developers = developers;

       }

 

-->

            <property name="developers">

                    <list>

                           <value>Cheney.</value>

                           <value>Eloy.</value>

                           <value>Jasper.</value>

                           <value>Dillon.</value>

                           <value>Kian.</value>

                    </list>

             </property>

 

4. 맵일 경우

       public void setAdministrators(Map<String, String> administrators) {

             this.administrators = administrators;

       }

 

-->

             <!-- map 타입일 경우 -->

             <property name="administrators">

                    <map>

                           <entry>

                                 <key>

                                        <value>Cheney</value>

                                 </key>

                                 <value>cheney@springPjt.org</value>

                           </entry>

                           <entry>

                                 <key>

                                        <value>Jasper</value>

                                 </key>

                                 <value>jasper@springPjt.org</value>

                           </entry>

                    </map>

             </property>

 

5. 그 밖의 프로퍼ㄹ티

       public void setVer(String ver) {

             this.ver = ver;

       }

       public void setsYear(int sYear) {

             this.sYear = sYear;

       }

       public void setsMonth(int sMonth) {

             this.sMonth = sMonth;

       }

       public void setsDay(int sDay) {

             this.sDay = sDay;

       }

 

-->

             <property name="ver">

                    <value>The version is 1.0</value>

             </property>

             <property name="sYear">

                    <value>2015</value>

             </property>

             <property name="sMonth">

                    <value>1</value>

             </property>

             <property name="sDay">

                    <value>1</value>

             </property>

 

 

6. 의존 객체 자동 주입

 

1. @Autowired : 생성자 or 변수나 setter --> 빈생성자 생성, 주입하려고 하는 객체의 타입이 일치하는 객체

@Autowired
public WordRegisterServiceUseAutowired(WordDao wordDao) {
  this.wordDao = wordDao;
}

 

2. @Resource : 변수나 seeter 메소드 --> 빈생성자 생성, 주입하려고 하는 객체의 이름이 일치하는 객체에

 

@Resource
private WordDao wordDao;

 

or

@Resource 

public void setWordDao(WordDao wordDao) {
  this.wordDao = wordDao;
}

 

 

< .xml>

 

<?xml version="1.0" encoding="UTF-8"?>

 

<beans xmlns="http://www.springframework.org/schema/beans"

       xmlns:context="http://www.springframework.org/schema/context"

       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

      xsi:schemaLocation="http://www.springframework.org/schema/beans

             http://www.springframework.org/schema/beans/spring-beans.xsd

             http://www.springframework.org/schema/context

             http://www.springframework.org/schema/context/spring-context.xsd">

 

       <context:annotation-config /> <!-- 추가 -->

 

       <bean id="wordDao" class="com.word.dao.WordDao" />

       <bean id="wordDao2" class="com.word.dao.WordDao" />

       <bean id="wordDao3" class="com.word.dao.WordDao" />

      

       <bean id="registerService" class="com.word.service.WordRegisterServiceUseAutowired" />

       <bean id="searchService" class="com.word.service.WordSearchServiceUseAutowired" />

</beans>

 

6_1. 같은 스프링 컨테이너에 같은 bean 객체가 생성돼 있을 때

Caused by: org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type [com.word.dao.WordDao] is defined: expected single matching bean but found 3: wordDao1,wordDao2,wordDao3

 

 

이름만 다르다.

       <bean id="wordDao1"class="com.word.dao.WordDao"/>

       <bean id="wordDao2" class="com.word.dao.WordDao" />

       <bean id="wordDao3" class="com.word.dao.WordDao" />

 

-->

       <bean id="wordDao1" class="com.word.dao.WordDao" >

             <qualifier value="usedDao"/> <!-- 이름 값을 명시해준다-->

       </bean>

 

 

<Autowired 사용>

       @Autowired

      @Qualifier("usedDao")

       private WordDao wordDao;

 

<inject 사용>

       @Inject

       @Named(value="wordDao1")

       private WordDao wordDao;

 

7. init-method , destroy-method

1) 인터페이스 사용

public class BookDao implements InitializingBean, DisposableBean {

..

       @Override

       public void afterPropertiesSet() throws Exception {

             System.out.println("(Bean)객체 생성 단계");

       }

 

       @Override

       public void destroy() throws Exception {

             System.out.println("(Bean)객체 소멸 단계");

       }

}

 

2) 설정 파일 사용

       <bean id="bookRegisterService" class="com.brms.book.service.BookRegisterService"

       init-method="initMethod" destroy-method="destroyMethod"/>

 

     

       public void initMethod() {

             System.out.println("BookRegisterService (Bean)객체 생성 단계");

       }

      

       public void destroyMethod() {

             System.out.println("BookRegisterService (Bean)객체 소멸 단계");

       }

 

바로 사용 가능

댓글