Binding with @Autowired not working inside instanc

2019-02-25 11:58发布

In my web spring application I create an instance with key word new as following.
In my one of action class, following method exists.

public void process() { 

    MyBean b=new MyBean(); //initiated the instance with new  
    b.process();
}   

Other MyBean class

@Service
public class MyBean {  

@Autowired  
MyService service;  

public void process() { 
    service.execute(); // this service instance has not initialized by Spring DI :( .service object is null. 
}

the MyService instance is not set by spring Dependency injection. Is it because that I create the instance of MyBean myself with new not the Spring ?

5条回答
闹够了就滚
2楼-- · 2019-02-25 12:10

Yes. It is not being set by DI of Spring as the instance you created using new keyword is not being managed by Spring container. Spring will by default inject dependencies only for spring managed instances. So to resolve this problem you can do it by two ways. First is don't use the new and use @Autowired on Mybean instance.

Second is to use @Configurable on MyBean class. With the @Configurable annotation spring will inject dependencies even for objects created through new keyword. Remember to have AspectJ jars on your classpath with @configurable annotation as Spring need them to inject Dependecies.

For the second approach use @Configurable(preConstruction = true) and add the following dependencies to your pom.xml

<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
<version>3.1.1.RELEASE</version>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>1.6.8</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.6.8</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjtools</artifactId>
<version>1.6.8</version>
</dependency>

You also need to compile it through AspectJ Compiler so that Byte code generated has the required abilities. You should use the following entries to make sure system using AspectJ Compiler at compile time.

<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>aspectj-maven-plugin</artifactId>
<version>1.4</version>
<configuration>
<showWeaveInfo>true</showWeaveInfo>
<source>1.6</source>
<target>1.6</target>
<Xlint>ignore</Xlint>
<complianceLevel>1.6</complianceLevel>
<encoding>UTF-8</encoding>
<verbose>false</verbose>
<aspectLibraries>
<aspectLibrary>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
</aspectLibrary>
</aspectLibraries>
</configuration>
<executions>
<execution>
<goals>
<goal>compile</goal>
<goal>test-compile</goal>
</goals>
</execution>
</executions>
<dependencies>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>1.6.8</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjtools</artifactId>
<version>1.6.11</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>
查看更多
我命由我不由天
3楼-- · 2019-02-25 12:11

In your case, spring does not recognize MyBean bean because you are creating it with new operator. Let spring initialize this bean and then you can have autowired beans accessed inside MyBean. For e.g.

<bean id="myBean" class="your.package.MyBean"></bean>

Above entry in your application context will create MyBean in spring container. And using this object you can access the services written inside it.

查看更多
爷、活的狠高调
4楼-- · 2019-02-25 12:12

When you create an object by new, autowire\inject don't work...

as workaround you can try this:

create your template bean of MyBean

<bean id="myBean" class="..." scope="prototype">
    <!-- collaborators and configuration for this bean go here -->
</bean>

and create an istance in this way

context.getBean("myBean");

PROTOTYPE : This scopes a single bean definition to have any number of object instances.

查看更多
唯我独甜
5楼-- · 2019-02-25 12:16

If you want to autowire programmatically, you can use:

private @Autowired AutowireCapableBeanFactory beanFactory;

public void process() {
   MyBean obj = new MyBean();
   beanFactory.autowireBean(obj);
   // obj will now have its dependencies autowired.
}
查看更多
疯言疯语
6楼-- · 2019-02-25 12:25

Another solution can be the use of @Component like this:

@Component("myBean")
public class MyBean{  

    @Autowired  
    MyService service;  

    public void process(){ 

    service.execute(); // this service instance has not initialized by Spring DI :( .service object is null. 

    }
}

And at your class where process() is, you can Autowire like this:

@Autowired
MyBean b

public void process(){  


  b.process();


}   
查看更多
登录 后发表回答