Spring IOC is often used to manage dependencies between objects, but in general there is a premise that these bean objects must be instantiated through the spring container.
In practice, however, it is possible to encounter such scenarios in the project:
A class is created not by a spring container, but by a direct new object (), which in turn has dependencies on objects managed in other spring containers.
What to do then, of course, we can manually call the Setxxxx () method to set the dependency between objects, but this is too high coupling. Fortunately spring also provides the ability to inject non-spring bean objects.
package com.springinaction.springidol; import org.springframework.beans.factory.annotation.configurable; @Configurable ("pianist") public class Instrumentalist implements Performer { public instrumentalist () {} public void perform () throws PerformanceException { System.out.print ("playing " + song + " : "); instrument.play (); } private String song; public void setsong (String song) { this.song = song; } private instrument instrument; &nbsP; public void setinstrument (instrument instrument) { this.instrument = instrument; } }
Of these, @Configurable ("pianist") has two functions:
1. It indicates that the current class object is instantiated outside the spring container, but can still be managed by the spring container;
2. It associates the instrumentalist class with the bean with the id "pianist", and later, when spring attempts to configure the instrumentalist instance, the pianist Bean is the template.
<?xml version= "1.0" encoding= "UTF-8"? > <beans xmlns= "http// Www.springframework.org/schema/beans " xmlns:aop=" http// Www.springframework.org/schema/aop " 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-2.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd "> <aop:spring-configured/> <bean id= " Pianist " class=" Com.springinaction.springidol.Instrumentalist " abstraCt= "true" > <property name= "song" value= "Chopsticks" / > <property name= "Instrument" > <bean class= "Com.springinaction.springidol.Piano" /> </property> </bean> </beans>
The role of,<aop:spring-configured/> is to tell spring that some beans need to be configured, including bean objects created outside of the spring container.
"Spring Combat" injects non-spring bean objects