복붙노트

[SPRING] 새 키워드로 생성 된 Spring bean (@Component)의 자동 와이어 링

SPRING

새 키워드로 생성 된 Spring bean (@Component)의 자동 와이어 링

나는 두 개의 봄 콩을 다음과 같이 가지고있다.

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

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

레거시 코드로 인해 새로운 키워드로 A의 새 개체를 만들어야합니다.

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

실행 중 -> A 클래스의 스프링 빈이 null 인 경우 스프링 빈 A의 새 인스턴스를 만들고 여전히 자동 와이어 링을 사용할 수 있습니까?

해결법

  1. ==============================

    1.당신의 컴포넌트 "A"는 Spring 컨테이너에 의해 생성되지 않으므로, 의존성은 주입되지 않는다. 그러나 일부 레거시 코드를 지원해야하는 경우 (질문에서 알 수 있듯이) @Configurable 주석을 사용하고 시간 짜기를 빌드 / 컴파일 할 수 있습니다.

    당신의 컴포넌트 "A"는 Spring 컨테이너에 의해 생성되지 않으므로, 의존성은 주입되지 않는다. 그러나 일부 레거시 코드를 지원해야하는 경우 (질문에서 알 수 있듯이) @Configurable 주석을 사용하고 시간 짜기를 빌드 / 컴파일 할 수 있습니다.

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

    그런 다음 Spring은 컨테이너 자체에 의해 인스턴스화되었거나 새로운 레거시 코드에서 인스턴스화되는 경우와 상관없이 자동 적 종속성을 컴포넌트 A에 주입합니다.

    예를 들어 메이븐 플러그인으로 빌드 타임 짜기를 설정하려면 다음을 수행해야합니다.

    빌드 플러그인 섹션에서 :

    <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

  2. ==============================

    2.new 연산자를 사용하여 클래스 A의 객체를 직접 만들므로 해당 객체에서 자동 와이어 된 필드를 가져 오지 않고 null을 찾습니다. 스프링 용기에서 콩을 얻으십시오.

    new 연산자를 사용하여 클래스 A의 객체를 직접 만들므로 해당 객체에서 자동 와이어 된 필드를 가져 오지 않고 null을 찾습니다. 스프링 용기에서 콩을 얻으십시오.

    희망이 당신을 도와줍니다. 건배.

  3. from https://stackoverflow.com/questions/10997092/autowiring-in-spring-bean-component-created-with-new-keyword by cc-by-sa and MIT license