使用new关键字创建的Spring bean(@Component)中的自动装配 [英] Autowiring in Spring bean (@Component) created with new keyword

查看:139
本文介绍了使用new关键字创建的Spring bean(@Component)中的自动装配的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个spring bean,如下所示:

I have two spring beans as follows:

@Component("A")
@Scope("prototype")
public class A extends TimerTask {

    @Autowired
    private CampaignDao campaignDao;
    @Autowired
    private CampaignManager campManger;
    A(){
        init_A();
       }
    }

我必须用新的A创建一个新对象关键字,由于遗留代码

I have to make a new object of A with new keyword, due to a legacy code

@Component("B")
@Scope("prototype")
public class B{
     public void test(){
       A a = new A();
     }
}

运行时 - > A类中的spring bean是空的,我可以创建一个新的spring bean A的实例,还是使用自动装配?

when Running -> the spring beans in the class A are null, can i create a new instance of the spring bean A and still use autowiring in it ?

推荐答案

你的组件A 不是由Spring容器创建的,因此依赖关系不会被注入。但是,如果您需要支持一些遗留代码(从您的问题可以看出),您可以使用 @Configurable 注释和构建/编译时编织:

Yours component "A" is not created by Spring container, thus, dependencies are not injected. However, if you need to support some legacy code (as I understand from your question), you can use @Configurable annotation and build/compile time weaving:

@Configurable(autowire = Autowire.BY_TYPE)
public class A extends TimerTask {
  // (...)
}

然后,Spring将注入自动连线依赖关系到组件A,无论是否被实例化容器本身,或者如果它是通过新的实例化的。

Then, Spring will inject autowired dependencies to component A, no matter if it's instantiated by container itself, or if it's instantiated in some legacy code by new.

例如,要设置build-时间编织与maven插件你必须:

For example, to set up build-time weaving with maven plugin you have to:


  1. 添加< context:spring-configured /> 到Spring应用程序上下文

  2. 配置 Maven AspectJ插件< a>:

  1. Add <context:spring-configured/> to the Spring application context
  2. Configure Maven AspectJ plugin:

在构建插件部分:

<build>
  <plugins>
    <plugin>
      <groupId>org.codehaus.mojo</groupId>
      <artifactId>aspectj-maven-plugin</artifactId>
      <version>1.4</version>
      <configuration>
        <complianceLevel>1.6</complianceLevel>
        <encoding>UTF-8</encoding>
        <aspectLibraries>
          <aspectLibrary>
            <groupId>org.springframework</groupId>
            <artifactId>spring-aspects</artifactId>
          </aspectLibrary>
        </aspectLibraries>
        <!-- 
          Xlint set to warning alleviate some issues, such as SPR-6819. 
          Please consider it as optional.
          https://jira.springsource.org/browse/SPR-6819
        -->
        <Xlint>warning</Xlint>
      </configuration>
      <executions>
        <execution>
          <goals>
            <goal>compile</goal>
            <goal>test-compile</goal>
          </goals>
        </execution>
      </executions>
    </plugin>
  </plugins>
</build>

...和依赖项:

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

有关详细信息,请参阅Spring参考:
http://static.springsource.org/spring/docs/current/spring-framework-reference /html/aop.html#aop-atconfigurable

Please consult Spring reference for more details: http://static.springsource.org/spring/docs/current/spring-framework-reference/html/aop.html#aop-atconfigurable

这篇关于使用new关键字创建的Spring bean(@Component)中的自动装配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆