Spring-lookup-method way to implement dependency injection

Source: Internet
Author: User

Introduction

Suppose that a singleton pattern of bean a needs to refer to another bean B that is not a singleton pattern, so that we can get the latest bean B every time we reference, we could let the bean A by implementing Applicationcontextware to perceive ApplicationContext (that is, the container context can be obtained), so that it can be run through Applicationcontext.getbean (String Beanname) method to get the latest bean B. But if you use the Applicationcontextaware interface, let us be coupled with the spring code, contrary to the inversion control principle (IoC, that is, the bean is completely managed by the spring container, our own code only needs to use the bean).

So spring provides a way for us to inject a method to implement the above scenario. Methods are injected mainly through <lookup-method/> tags.

Instance

Let's use an example to illustrate the use of Lookup-method.

Suppose there is a fruit plate, fruit in the fruit bowl, such as apples, bananas, etc., we hope that every time we get in the fruit bowl is the freshest fruit.

Java code:

//define a fruit class Public classFruit { PublicFruit () {System.out.println ("I got Fruit"); }}//Apple Public classAppleextendsFruit { PublicApple () {System.out.println ("I got a fresh apple"); }}//Banana Public classBananerextendsFruit { PublicBananer () {System.out.println ("I got a fresh Bananer"); }}//Fruit plate, can get fruit Public Abstract classfruitplate{//abstract ways to get fresh fruit    protected AbstractFruit getfruit ();}

Spring Configuration:

<!--This is a bean of 2 non-singleton patterns -<BeanID= "Apple"class= "Cn.com.willchen.test.di.Apple"Scope= "Prototype"/><BeanID= "Bananer"class= "Cn.com.willchen.test.di.Bananer"Scope= "Prototype"/> <BeanID= "FruitPlate1"class= "Cn.com.willchen.test.di.FruitPlate">    <Lookup-methodname= "Getfruit"Bean= "Apple"/></Bean><BeanID= "FruitPlate2"class= "Cn.com.willchen.test.di.FruitPlate">    <Lookup-methodname= "Getfruit"Bean= "Bananer"/></Bean>

Test code:

 Public Static void Main (string[] args) {    new classpathxmlapplicationcontext ("classpath:resource/ Applicationcontext.xml ");    Fruitplate FP1= (fruitplate) app.getbean ("fruitPlate1");     = (fruitplate) app.getbean ("FruitPlate2");    Fp1.getfruit ();    Fp2.getfruit ();}

Test results:

I got Fruit

I got a fresh apple

I got Fruit

I got a fresh bananer

Example Description:

As we can see from the above example, in the code, we do not use any of spring's classes and interfaces to implement the coupling to the spring code.

The most important part is the configuration of Lookup-method and the Fruitplate.getfruit () method. In the above code, we can see that the Getfruit () method is an abstract method, and we didn't implement it, so how did it get the fruit? The secret here is that srping has applied the cglib (dynamic Proxy) class library. Spring initializes the container with a special handling of the bean configuration <lookup-method/>, and spring will do a dynamic proxy for the class specified by the bean, proxy <lookup-method/> The method specified by the Name property in the label returns the Bean instance object specified by the Bean property. Each time we call the Getfruit () method of the 2 bean fruitPlate1 or fruitPlate2, it is actually a method of invoking the dynamic proxy class generated by Cglib. About Cglib you can check online.

Lookup-method Implementation Method Description:

<class= "Beanclass">    <name = "Method" Bean = "Non-singleton-bean" /> </ Bean >

Method is one of the methods in Beanclass, Beanclass and method are not abstract are indifferent, will not affect cglib dynamic agent, according to the actual needs of the project to define. Non-singleton-bean refers to the bean attribute in the Lookup-method must be a non-singleton pattern of the bean, of course, if not also will not report an error, but each get is the same reference bean (the same instance), It makes no sense to use Lookup-method.

In addition, the following criteria are available for the signature of method in code:

<public|protected> [Abstract] <return-type> themethodname (no-arguments);

The public|protected requirement method must be overridden and called by the quilt class;

Abstract optional, if it is an abstraction method, the dynamic proxy class of Cglib will implement this method, if it is not an abstract method, it will overwrite this method, so there is no effect;

Return-type is the type of Non-singleton-bean, of course, it can be its parent class or interface.

No-arguments parameters are not allowed.

Spring-lookup-method way to implement dependency injection

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.