ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • Bean 의존 관계 주입 기초 Ex
    Search: Spring Spring 2023. 7. 8. 14:30

    Ex Test1Controller에 Test1Service를 의존 관계로 주입.

    <bean id=”test1Service” class=”com.example.Ta1.Test1Service”/>
    <bean id=”test1Controller” class=”com.example.Ta1.Test1Controller”>
    	<constructor-arg ref=” test1Service”/> //생성자를 통한 전달
    </bean>

     

    AbstractApplicationContext ctt = new GenericXmlApplicationContext(application-config.xml”); // test1Controller 생성
    Test1Controller tc1 = ctt.getBean(“test1Controller”, “Test1Controller.class”); #인스턴스가 하나만 있을 때는 이름을 명시 하지 않아도 된다
    tc1.work1();
    ctt.close(); // test1Controller 소멸

     

    Ex Test1Controller에 Test1Service를 의존 관계로 주입.

    <bean id=”test1Service1” class=”com.example.Ta1.Test1Service”/>
    <bean id=”test1Controller” class=”com.example.Ta1.Test1Controller”>
    <property name=” test1ServiceFunc” ref=” test1Service1”/> //test1Service1개체를 setTest1ServiceFunc()함수를 통해서 주입.
    </bean>

     


    Ex Test1Controller에 Test1Service를 의존 관계로 주입(p네임스페이스 사용)

    //사용전 p네임스페이스를 추가해야 한다.(xmlns:p=”http:...”)
    <bean id=”test1Service1” class=”com.example.Ta1.Test1Service”/>
    <bean id=”test1Controller” class=”com.example.Ta1.Test1Controller” p:test1ServiceFunc-ref=”test1Service1”>
    <property name=” test1ServiceFunc” ref=” test1Service1”/> //test1Service1개체를 setTest1ServiceFunc()함수를 통해서 주입.
    </bean>

     

     

    'Spring' 카테고리의 다른 글

    Spring Web MVC DispatcherServlet  (0) 2023.07.09
    Dao  (0) 2023.07.09
    Jdbc  (0) 2023.07.09
    Aop 용어  (0) 2023.07.08
    GenericXmlApplicationContext 기본 예제  (0) 2023.07.08
    Container  (0) 2023.07.08
    Context  (0) 2023.07.08
    Java SpringBoot 개요  (0) 2023.07.07

    댓글