Java Tutorial/Spring/Dependency Injection

Материал из Java эксперт
Перейти к: навигация, поиск

Contextualized Dependency Lookup Demo

File: context.properties



   <source lang="java">

source.(class)=SimpleMessageData destination.(class)=StdoutMessageReporter service.(class)=DefaultMessageService service.source(ref)=source service.destination(ref)=destination</source>





dependency check Demo

File: context.xml



   <source lang="java">

<?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.xsd">
   <bean id="target1" class="SimpleBean"
           dependency-check="simple">
       <property name="someInt" value="1"/>
   </bean>
   <bean id="target2" class="SimpleBean"
       dependency-check="objects">
       <property name="nestedSimpleBean" ref="nestedSimpleBean"/>
   </bean>
   <bean id="target3" class="SimpleBean"
       dependency-check="all">
       <property name="nestedSimpleBean" ref="nestedSimpleBean"/>
       <property name="someInt" value="1"/>
   </bean>
   <bean id="nestedSimpleBean" class="SimpleBean"/>

</beans></source>





Dependency Injection Demo

Dependency Pull Demo

File: context.properties



   <source lang="java">

source.(class)=SimpleMessageData destination.(class)=StdoutMessageReporter service.(class)=DefaultMessageService service.source(ref)=source service.destination(ref)=destination</source>





Setter Dependency Injection Demo

File: context.properties



   <source lang="java">

source.(class)=SimpleMessageData destination.(class)=StdoutMessageReporter service.(class)=DefaultMessageService service.source(ref)=source service.destination(ref)=destination</source>





Spring bean dependency Demo

File: context.xml



   <source lang="java">

<?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.xsd">
   <bean id="b" class="B" depends-on="a"/>
   <bean id="a" class="A"/>

</beans></source>