- XML(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"

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

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

    <bean id="messageBean" class="sample1.MessageBeanImpl" >

        <constructor-arg>  // 설정된 bean 의 생성자 파라미터 초기값 세팅

            <value>Spring</value>

        </constructor-arg>

        <property name="outputter">// 설정된 bean 의 갑 세팅(해당 클래스의 Setter 이 존재해야함)

            <ref local="outputter" />

        </property>

    </bean>

    <bean id="outputter" class="sample1.FileOutputter">

        <property name="filePath">

            <value>out.txt</value>

        </property>

    </bean>

</beans>


- XML(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"

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

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

    <bean id="messageBean" class="sample1.MessageBeanImpl" >

        <constructor-arg>  // 설정된 bean 의 생성자 파라미터 초기값 세팅

            <value>Spring</value>

        </constructor-arg>

    </bean>

    <bean id="outputter" class="sample1.FileOutputter" autowire="byType">

        <property name="filePath">

            <value>out.txt</value>

        </property>

    </bean>

</beans>



- xml 변경 후 해당 클래스 sample1.MessageBeanImpl 에서 <property> 값 세팅을 위한 Setter 은 없어도 된다.

  ex)

      (변경 전)

       private Outputter outputter;

       public void setOutputter(Outputter outputter) {

           this.outputter = outputter;

       }


        (변경 후)

        @Autowired

        private Outputter outputter;

    

+ Recent posts