Spring

Autowired

꼬렙 2013. 9. 16. 12:35

ㅇ 스프링에서 Bean 객체 간에 서로 주입을 시켜줄때는 생성자 또는 Setter 형태로 선언을 해야 하지만

    @Autowired 라는 애노테이션을 사용함으로써 선언 생략 가능


ㅇ 사용방법

public class First {

    @Autowired

    private Second second;

}


ㅇ Bean 객체 형태로 선언만 되어 있다면 어디서든 @Autowired를 통해 객체 주입 가능


ㅇ 설정

<?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:context="http://www.springframework.org/schema/context"

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

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

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

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

      

    <context:annotation-config />

     

    <bean id="first" class="com.spring.exam2.First" />

    <bean id="second" class="com.spring.exam2.Second">

        <property name="age">

            <value>18</value>

        </property>

    </bean>

</beans>

context 네임스페이스 & 스키마, <context:annotation-config /> 태그 반드시 추가



예제 파일

AutowiredExam.zip