@Component
class Foo {}
class Bar {
@Autowired
Foo foo;
}
@Component
class Zoo {
Bar bar = new Bar();
}
我怎樣才能insttruct彈簧自動裝配在bar
實例字段foo
?
更具體地說,我可以指示spring一旦看到一個實例就將Bar當作bean看待。例如,尊重@PostConstruct
註釋?
@Component
class Foo {}
class Bar {
@Autowired
Foo foo;
}
@Component
class Zoo {
Bar bar = new Bar();
}
我怎樣才能insttruct彈簧自動裝配在bar
實例字段foo
?
更具體地說,我可以指示spring一旦看到一個實例就將Bar當作bean看待。例如,尊重@PostConstruct
註釋?
Spring可以使用aspectj爲使用@Configurable註釋的類進行編譯時編織。在行家(由袋鼠創建的配置採取):
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>aspectj-maven-plugin</artifactId>
<version>1.2</version>
<!-- NB: do not use 1.3 or 1.3.x due to MASPECTJ-90 and do not use 1.4 due to declare parents issue -->
<dependencies>
<!-- NB: You must use Maven 2.0.9 or above or these are ignored (see MNG-2972) -->
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>${aspectj.version}</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjtools</artifactId>
<version>${aspectj.version}</version>
</dependency>
</dependencies>
<executions>
<execution>
<goals>
<goal>compile</goal>
<goal>test-compile</goal>
</goals>
</execution>
</executions>
<configuration>
<outxml>true</outxml>
<aspectLibraries>
<aspectLibrary>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
</aspectLibrary>
</aspectLibraries>
<source>${java.version}</source>
<target>${java.version}</target>
</configuration>
</plugin>
,並在應用程序上下文中添加<context:spring-configured/>
爲什麼不能它是
@Component
class Zoo {
@Autowired
Bar bar;
}
@Component
class Bar {
@Autowired
Foo foo;
}
你手工設置,因爲通過使用new
要創建哪些不被Spring IoC容器管理的對象。
它也不會尊重@PostContruct
,因爲該註釋僅適用於由容器管理的bean。
,因爲我不想讓一個「根」豆。在我的情況下,創建類傳遞Zoo的'this'參數,它是一個原型bean – IttayD
你是什麼意思的「根」豆? – soulcheck