Spring-based generic dependency injection

Source: Internet
Author: User

A reference to a member variable of a generic type that can be injected into a subclass for a subclass in Spring 4.x (so that the generic classes corresponding to subclasses and subclasses automatically establish a relationship)
Specific Description:
Generic injection: Bean1 and Bean2 are injected with generics, and Bean1 and Bean2 establish dependencies, which are automatically established by class Bean3 (inheritance Bean1) and Bean4 (inheritance bean2)
Not generic injection: that is, Bean1 and Bean2 are not injected into generics, only relationships are established, subclasses Bean3 (inheritance bean1) and Bean4 (inheritance bean2) also automatically establish relationships

Implementation steps for generic dependency injection:
1 Create a new two generic class and establish a dependency relationship:
Specific code:
Baserepository:

 Package Com.jeremy.spring.genericityDI;  Public class baserepository{}

Baseservice:

 Package Com.jeremy.spring.genericityDI; Import org.springframework.beans.factory.annotation.Autowired;  Public class Baseservice<t> {        @Autowired------//This tells the IOC container to automatically assemble a dependent    beanprotected baserepository baserepository;--------//This is a subclass inheritance dependency      Public void Add () {        System.out.println ("Add ... .....");        System.out.println (baserepository);    }}

2 Create a new generic class:

User:

 Package Com.jeremy.spring.genericityDI;  Public class User {}

3 new Baserepository and Baseservice subclasses
Userrepository:

 Package Com.jeremy.spring.genericityDI; Import org.springframework.stereotype.Component; @Component  Public class extends baserepository{    }

UserService:

 Package Com.jeremy.spring.genericityDI; Import Org.springframework.stereotype.Service; @Service  Public class extends baseservice{}

4: Configure the automatic assembly of annotated beans in the spring configuration file:
  

<?XML version= "1.0" encoding= "UTF-8"?><Beansxmlns= "Http://www.springframework.org/schema/beans"Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"Xmlns:context= "Http://www.springframework.org/schema/context"xsi:schemalocation= "Http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd Http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd "><context:component-scan base-package= "Com.jeremy.spring.genericityDI" ></context:component-scan> </Beans>

5 test Code and results
Test code:

@Test      Public void Test () {        applicationcontext actx=new classpathxmlapplicationcontext ("Bean-genericity-di.xml ");        UserService userservice= (userservice) actx.getbean ("UserService");        Userservice.add ();    }

Test results:

Add ..... ..... [Email protected]

From the results, although the subclass does not establish a dependency, but the Userrepository instance is instantiated, it proves the dependency of the parent class, and the subclass can be inherited.

It can also be explained here that even if the parent class is not managed by the IOC container, but the @autowired annotation is added when the relationship is established, the relationship of the parent class is inherited.

Off Topic:
What are the pros and cons of generics injection and not generic injection??? What is the real role of both??
As for the two different ways to inject exactly what are the pros and cons, later in the development of their own slowly understand it, now is the basis for

Spring-based generic 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.