Lookup-method
Example:
Step One: define a Car class
Package Org.hope.spring.bean.lookup; Public class Car { private String brand; Private String Corp; Private Double Price ; Getter ()&setter () ....}
Step Two: define a boss interface
Package Org.hope.spring.bean.lookup; Public Interface Boss { Car havecar ();}
Step Three: define three beans in spring's configuration file Bean.xml
<BeanID= "Honeqi"class= "Org.hope.spring.bean.lookup.Car"P:brand= "Red Flag"P:price= "400000"Scope= "Prototype" /><BeanID= "BMW"class= "Org.hope.spring.bean.lookup.Car"P:brand= "Mercedes-Benz GLC260"P:price= "500000"Scope= "Prototype" /><BeanID= "Boss"class= "Org.hope.spring.bean.lookup.Boss"> <Lookup-methodname= "Havecar"Bean= "BMW"/></Bean>
Step Four: Write unit test test
PackageOrg.hope.spring.bean.lookup;Importorg.junit.Test;ImportOrg.junit.runner.RunWith;Importorg.springframework.beans.factory.annotation.Autowired;Importorg.springframework.test.context.ContextConfiguration;ImportOrg.springframework.test.context.junit4.SpringJUnit4ClassRunner; the @RunWith (Springjunit4classrunner.class) @ContextConfiguration (Locations= {"Classpath:bean.xml"}) Public classlookuptest {@AutowiredPrivateboss Boss; @Test Public voidTestboss () {Car car=Boss.havecar (); System.out.println (Car.getbrand ()); }}
Output:
Mercedes GLC260
Conclusion:
1, through the Lookup-method element tag can provide dynamic implementation for Boss Havecar () , return prototype type of car Bean.
2, the lookup method injection is a certain scope of use, generally in the hope of a singleton bean to get a prototype Bean is used
3, the Lookup method injection needs to use the Cglib class package
Replaced-method (method substitution)
A method that can be replaced with a new method when it is run again
Step one: Create a new Player.java
PackageOrg.hope.spring.bean.model; Public classPlayer {Private intID; PrivateString name; Public intgetId () {returnID; } Public voidSetId (intID) { This. ID =ID; } PublicString GetName () {returnname; } Public voidsetName (String name) { This. Name =name; } @Override PublicString toString () {return"player{" + "id=" + ID + ", name= '" + name + ' \ ' + '} '; }}
Step Two: Create a new class that will be replaced
Package Org.hope.spring.bean.replacedmethod; Import Org.hope.spring.bean.model.Car; Import Org.hope.spring.bean.model.Player; Public class Yijianlian { public player Onplace () { new player (); Player.setname ("Yi Jian Lian"); return player; }}
Step three: Create a new replacement class to implement Methodreplacer
PackageOrg.hope.spring.bean.replacedmethod;ImportOrg.hope.spring.bean.model.Player;ImportOrg.springframework.beans.factory.support.MethodReplacer;ImportJava.lang.reflect.Method;/*** @Description: The bean used to replace others must implement Methodreplacer*/ Public classYaoImplementsMethodreplacer { PublicObject Reimplement (Object obj, Method method, object[] args)throwsthrowable {player player=NewPlayer (); Player.setname ("Yao Ming"); returnplayer; }}
Step Four: Configure Bean relationships in Bean2.xml
class class= "Org.hope.spring.bean.replacedmethod.YiJianLian" > <replaced-method name= "Onplace" Replacer= "Yao"/></bean>
Step Five: Test
PackageOrg.hope.spring.bean.replacedmethod;Importorg.junit.Test;ImportOrg.junit.runner.RunWith;Importorg.springframework.beans.factory.annotation.Autowired;Importorg.springframework.test.context.ContextConfiguration;ImportOrg.springframework.test.context.junit4.SpringJUnit4ClassRunner; the @RunWith (Springjunit4classrunner.class) @ContextConfiguration (Locations= {"Classpath:bean2.xml"}) Public classreplacedtest {@AutowiredPrivateYijianlian Yijianlian; @Test Public voidTest () {System.out.println (Yijianlian.onplace ()); }}
Output:
Player{id=0, Name= ' Yao Ming '}
Conclusion:
1. You can replace the old method with a new method at run time.
2. The bean used to replace others must implement the Methodreplacer interface.
Reference:
[1] Proficient in spring4.x enterprise application development, electronic industry press, Chen Xionghua
Spring Bean neutron elements lookup-method and Replaced-method