Spring 与@Autowired绑定在使用';启动的实例内不起作用;新';

Spring 与@Autowired绑定在使用';启动的实例内不起作用;新';,spring,dependency-injection,Spring,Dependency Injection,在我的WebSpring应用程序中,我使用关键字new创建了一个实例,如下所示。 在我的one of action类中,存在以下方法 public void process() { MyBean b=new MyBean(); //initiated the instance with new b.process(); } 其他MyBean类 @Service public class MyBean { @Autowired MyService serv

在我的WebSpring应用程序中,我使用关键字
new
创建了一个实例,如下所示。
在我的one of action类中,存在以下方法

public void process() { 

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

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

MyService实例不是由spring依赖项注入设置的。是因为我自己用
new
而不是Spring创建了MyBean实例吗?

是的。它不是由Spring的DI设置的,因为您使用new关键字创建的实例不是由Spring容器管理的。默认情况下,Spring将只为Spring管理的实例注入依赖项。所以要解决这个问题,你可以通过两种方法来解决。首先,不要使用
new
并在
Mybean
实例上使用
@Autowired

第二个是在
MyBean
类上使用
@Configurable
。通过
@Configurable
注释,spring甚至会为通过
new
关键字创建的对象注入依赖项。记住在类路径上有
AspectJ
jar和
@configurable
注释,因为Spring需要它们注入依赖项

对于第二种方法,使用
@Configurable(preConstruction=true)
并将以下依赖项添加到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>
org.springframework
春季方面
3.1.1.1发布
org.aspectj
aspectjrt
1.6.8
org.aspectj
aspectjweaver
1.6.8
org.aspectj
aspectjtools
1.6.8
您还需要通过AspectJ编译器对其进行编译,以便生成的字节码具有所需的功能。您应该使用以下条目来确保系统在编译时使用AspectJ编译器

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

org.codehaus.mojo
aspectj maven插件
1.4
真的
1.6
1.6
忽视
1.6
UTF-8
假的
org.springframework
春季方面
编译
测试编译
org.aspectj
aspectjrt
1.6.8
org.aspectj
aspectjtools
1.6.11

如果要以编程方式自动连线,可以使用:

private @Autowired AutowireCapableBeanFactory beanFactory;

public void process() {
   MyBean obj = new MyBean();
   beanFactory.autowireBean(obj);
   // obj will now have its dependencies autowired.
}

在您的例子中,spring无法识别
MyBean
bean,因为您是使用
new
操作符创建它的。让spring初始化这个bean,然后您就可以在
MyBean
中访问自动连线bean。例如

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


应用程序上下文中的上述条目将在spring容器中创建
MyBean
。使用此对象,您可以访问其中写入的服务。

使用new创建对象时,autowire\inject不起作用

作为解决方法,您可以尝试以下方法:

创建MyBean的模板bean

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

原型:这将单个bean定义的范围限定为具有任意数量的对象实例。

另一个解决方案可以是使用
@组件
,如下所示:

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

    }
}
@Autowired
MyBean b

public void process(){  


  b.process();


}   
process()
所在的类中,您可以像这样自动连线:

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

    }
}
@Autowired
MyBean b

public void process(){  


  b.process();


}   

非常感谢你。您的意思是
org.springframework-springaspects 4.0.6.RELEASE
仅此而已?可能重复