Three ways of spring static injection

Source: Internet
Author: User


Three ways to spring static injection:

(Note: Mongofileoperationutil is a MongoDB file read-write tool of its own encapsulation, which needs to rely on Advanceddatastore object instances, DSFORRW to obtain MongoDB data source)

In Springframework, we can't @autowired a static variable to make it a spring bean, such as the following:

[Java] view plain copy print?   @Autowired private static Advanceddatastore DSFORRW;

@Autowired
private static Advanceddatastore DSFORRW;
can try, DSFORRW in this state can not be dependent injection, will throw Run-time exception java.lang.NullPointerException, why? The static variable/class variable is not an object's property, but a property of a class. Spring is based on dependency injection at the object level.
But I prefer to encapsulate the tool class, and through the @component annotation success can component, but the functional components of the method is generally static method, static method can only call static member variables, so there are the following problems. Sometimes the encapsulation function component will need the bottom of the service injection, how to do it.

Go online Search the solution, a simple summary of several ways to achieve;

1.xml mode implementation;

[HTML] view plain copy print? <bean id= "Mongofileoperationutil class=" com.*.*. Mongofileoperationutil "init-method=" init "> <property name=" DSFORRW "ref=" DSFORRW "/> </bean>

<bean id= "Mongofileoperationutil" class= "Com.*.*". Mongofileoperationutil "init-method=" init ">
	<property name=" DSFORRW "ref=" DSFORRW "/>
</bean >

[Java] view plain copy print? public class mongofileoperationutil {               private static advanceddatastore  dsForRW;          private static mongofileoperationutil  mongoFileOperationUtil;          public void init ()  {           mongoFileOperationUtil = this;            mongoFileOperationUtil.dsForRW =  this.dsforrw;       }     }  

public class Mongofileoperationutil {
    
    private static advanceddatastore DSFORRW;

    private static Mongofileoperationutil Mongofileoperationutil;

    public void init () {
        mongofileoperationutil = this;
        MONGOFILEOPERATIONUTIL.DSFORRW = THIS.DSFORRW;
    }

This approach is suitable for Web projects based on XML configuration;


2. Implementation of the @PostConstruct approach;

[Java] view plain copy print? import org.mongodb.morphia.advanceddatastore;   import org.springframework.beans.factory.annotation.autowired;         @ component   public class mongofileoperationutil {       @ autowired       private static AdvancedDatastore dsForRW;           private static MongoFileOperationUtil  mongofileoperationutil;           @PostConstruct         public void init ()  {            mongofileoperationutil = this;            mongofileoperationutil.dsforrw = this.dsforrw;       }     }  

Import Org.mongodb.morphia.AdvancedDatastore;
Import org.springframework.beans.factory.annotation.Autowired;


@Component public
class Mongofileoperationutil {
    @Autowired
    private static Advanceddatastore DSFORRW;

    private static Mongofileoperationutil Mongofileoperationutil;

    @PostConstruct public
    void init () {
        mongofileoperationutil = this;
        MONGOFILEOPERATIONUTIL.DSFORRW = THIS.DSFORRW;
    }

The method of @PostConstruct annotations executes after the constructor of the class is loaded, that is, after the constructor has been loaded, the Init method is executed, (@PreDestroy the annotation defines the action before the container is destroyed)

This approach is similar to configuring the Init-method and Destory-method methods in XML to define what the spring container does before initializing the bean and container destroy;


3.set method Add @autowired annotation, add @component annotation on class definition;

[Java] view plain copy print? import org.mongodb.morphia.advanceddatastore;   import org.springframework.beans.factory.annotation.autowired;   import  org.springframework.stereotype.component;         @Component    Public  class MongoFileOperationUtil {          private  static advanceddatastore dsforrw;                @Autowired        public void setdatastore (advanceddatastore  DSFORRW)  {   &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;MONGOFILEOPERATIONUTIL.DSFORRW  = dsForRW;       }  }  

Import Org.mongodb.morphia.AdvancedDatastore;
Import org.springframework.beans.factory.annotation.Autowired;
Import org.springframework.stereotype.Component;


@Component public
class Mongofileoperationutil {

    private static advanceddatastore DSFORRW;
    
    @Autowired public
    void Setdatastore (Advanceddatastore dsforrw) {
        MONGOFILEOPERATIONUTIL.DSFORRW = DSFORRW;
    }
}

First spring should be able to scan the Advanceddatastore bean, then inject it through the setter method, and then note that no @autowired annotation is required on the member variable;

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.