Binding with @Autowired not working inside instances initiated with 'new'

你。 提交于 2019-12-20 01:45:46

问题


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 ?


回答1:


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.
}



回答2:


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:


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.




回答4:


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.




回答5:


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();


}   


来源:https://stackoverflow.com/questions/25635845/binding-with-autowired-not-working-inside-instances-initiated-with-new

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!