Java Tutorial/Spring/Dependency Injection
Версия от 17:44, 31 мая 2010; (обсуждение)
Содержание
Contextualized Dependency Lookup Demo
File: context.properties
source.(class)=SimpleMessageData
destination.(class)=StdoutMessageReporter
service.(class)=DefaultMessageService
service.source(ref)=source
service.destination(ref)=destination
dependency check Demo
File: context.xml
<?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>
Dependency Injection Demo
Dependency Pull Demo
File: context.properties
source.(class)=SimpleMessageData
destination.(class)=StdoutMessageReporter
service.(class)=DefaultMessageService
service.source(ref)=source
service.destination(ref)=destination
Setter Dependency Injection Demo
File: context.properties
source.(class)=SimpleMessageData
destination.(class)=StdoutMessageReporter
service.(class)=DefaultMessageService
service.source(ref)=source
service.destination(ref)=destination
Spring bean dependency Demo
File: context.xml
<?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>