Spring @Autowired injection is null

the reason

  1. Deletion configuration, such as a scan driver to turn annotations, registered inject assembly;
  2. Use the new keyword to create an object from spring container management, not injected;
  3. Injection static variables, static variables attribute / object class is not variable, but a class attribute, spring is dependent on the object based on the level of the injection.

Directly on the code to know

 

 

 

Static injection of three ways

In some tools that may be dependent object service layer, tools are static methods in general use, the corresponding member variables need to be declared as static, at this time if the direct use @Autowired dependency injection, the method call when will report NullpointerException.

@Autowired
private static AdvancedDatastore dsForRW;

Can try, dsForRW in this state can not be relied upon injection, it will throw an exception java.lang.NullPointerException run, why? Static variables / class attribute variable is not an object, but a class attribute, spring is It is dependent on the object based on the level of the injection.
But it was more like packaging tools, and components can be successful by @Component comment, but the method of functional components are generally static methods, static methods can only call a static member variables, so there is the following problem. Sometimes the package seal the functional components will need to inject the underlying service, how to do it?
Go online to search for a solution to the next, briefly summarize several implementations;

1.xml way to achieve 
this is right for the project based on the WEB XML configuration; 

< the bean ID = "mongoFileOperationUtil" class =. ".. COM * * MongoFileOperationUtil" the init-Method = "the init" > 
    < Property name = "dsForRW" REF = "dsForRW" /> 
</ the bean > 
. 1 
2 
. 3 
public class MongoFileOperationUtil { 
    
    Private static AdvancedDatastore dsForRW; 
 
    Private static MongoFileOperationUtil mongoFileOperationUtil; 
 
    public void the init () { 
        mongoFileOperationUtil = the this; 
        mongoFileOperationUtil.dsForRW = this.dsForRW;
    }
 
}

2.@PostConstruct方式实现
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 @PostConstruct annotation is executed after loading class constructor, i.e. after loading the constructor, the init method; (@ PreDestroy annotation was made before the operation of destroying the container defined)
in this manner and arranged in xml init -method destory-method and similar methods, the definition of the operation of the spring container made before initialization bean container and destruction;

 
 
 

3 Add @Autowired annotation, the annotation adding @Component class defines the methods .set

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 AdvancedDatastore be able to scan the bean, and then injected through a setter method; and Note: not need to add annotations on @Autowired member variables;

 

Guess you like

Origin www.cnblogs.com/weibanggang/p/11373243.html