"Spring Combat" injects non-spring bean objects

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.