16
的每次調用創建bean的新實例,我旁邊的情況: Connection manager
應該有每次ConnectionServer
一個對象和DataBean
所以新對象,我創建了這些豆子並配置了它的Spring XML 。春:爲get方法
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<bean id="dataBena" class="com.test.DataBean" scope="prototype"/>
<bean id="servCon" class="com.test.ServerCon"/>
<!--<bean id="test" class="com.test.Test"/>-->
<context:component-scan base-package="com.test"/>
</beans>
,並添加範圍prototype
爲DataBean
在此之後,我創建了簡單的UTIL /組件類名爲Test
@Component
public class Test {
@Autowired
private DataBean bean;
@Autowired
private ServerCon server;
public DataBean getBean() {
return bean.clone();
}
public ServerCon getServer() {
return server;
}
}
,但調用我克隆的getBean()方法的每個時間這個bean,這對我來說是個問題。 可以從彈簧配置做到這一點,而不需要克隆方法? 謝謝。
只需將[cglib.jar](http://repo1.maven.org/maven2/cglib/cglib/2.2.2/cglib-2.2.2.jar)添加到您的CLASSPATH。 –
和asm-all-3.3.1.jar :),感謝所有作品的不錯! –
如果'DataBean'是一個通用的,如果它裏面有一個'Service',根據''類型是'@ Autowired',你將如何處理? –