Skip to content

Commit

Permalink
change bean from singleton to prototype by postProcessBeanFactory test
Browse files Browse the repository at this point in the history
  • Loading branch information
zhongliangjun1 committed Nov 5, 2014
1 parent 61ec183 commit f693c12
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 2 deletions.
21 changes: 21 additions & 0 deletions src/main/java/com/dianping/start/MyBeanFactoryPostProcessor.java
Expand Up @@ -29,6 +29,27 @@ public void postProcessBeanFactory(ConfigurableListableBeanFactory configurableL
propertyValue.setConvertedValue("guess ni mei");
System.out.println("modify myBean sex");
}

String[] beanDefinitionNames = configurableListableBeanFactory.getBeanDefinitionNames();
if (beanDefinitionNames!=null && beanDefinitionNames.length>0) {
for (String beanDefinitionName : beanDefinitionNames) {
BeanDefinition beanDefinition = configurableListableBeanFactory.getBeanDefinition(beanDefinitionName);
if ( beanDefinition.isSingleton() ) {
if ( beanDefinitionName.equals("singletonBeanUpdateTest") ) {
String beanClassName = beanDefinition.getBeanClassName();
try {
Class c = Class.forName(beanClassName);
if ( SingletonBeanUpdateTest.class.isAssignableFrom(c) ) {
beanDefinition.setScope("prototype");
}
} catch (ClassNotFoundException e) {
throw new RuntimeException(e);
}
}
}
}
}

System.out.println("postProcessBeanFactory---------end");
}

Expand Down
14 changes: 14 additions & 0 deletions src/main/java/com/dianping/start/SingletonBeanUpdateTest.java
@@ -0,0 +1,14 @@
package com.dianping.start;

/**
* Created with IntelliJ IDEA.
* Author: liangjun.zhong
* Date: 14-11-5
* Time: PM2:49
* To change this template use File | Settings | File Templates.
*/
public interface SingletonBeanUpdateTest {

public void test();

}
19 changes: 19 additions & 0 deletions src/main/java/com/dianping/start/SingletonBeanUpdateTestImpl.java
@@ -0,0 +1,19 @@
package com.dianping.start;

/**
* Created with IntelliJ IDEA.
* Author: liangjun.zhong
* Date: 14-11-5
* Time: PM2:49
* To change this template use File | Settings | File Templates.
*/
public class SingletonBeanUpdateTestImpl implements SingletonBeanUpdateTest {

private int count = 0;

@Override
public void test() {
count = count + 1;
System.out.println("now count is "+count);
}
}
20 changes: 19 additions & 1 deletion src/main/java/com/dianping/struts/ActionOfSpringBean.java
@@ -1,6 +1,10 @@
package com.dianping.struts;

import com.dianping.aop.UserService;
import com.dianping.start.SingletonBeanUpdateTest;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;

/**
* Created with IntelliJ IDEA.
Expand All @@ -9,17 +13,31 @@
* Time: AM1:12
* To change this template use File | Settings | File Templates.
*/
public class ActionOfSpringBean {
public class ActionOfSpringBean implements ApplicationContextAware {

private UserService userService;

private ApplicationContext applicationContext;
//private SingletonBeanUpdateTest singletonBeanUpdateTest;


public String execute(){
userService.addTopic(11, "ActionOfSpringBean");
SingletonBeanUpdateTest singletonBeanUpdateTest = (SingletonBeanUpdateTest) applicationContext.getBean("singletonBeanUpdateTest");
singletonBeanUpdateTest.test();
return "success";
}

public void setUserService(UserService userService) {
this.userService = userService;
}

// public void setSingletonBeanUpdateTest(SingletonBeanUpdateTest singletonBeanUpdateTest) {
// this.singletonBeanUpdateTest = singletonBeanUpdateTest;
// }

@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
this.applicationContext = applicationContext;
}
}
4 changes: 3 additions & 1 deletion src/main/resources/config/spring/appcontext-core.xml
Expand Up @@ -27,8 +27,9 @@

<bean id="myBankSavings" class="com.dianping.start.ChildBeanFactory" factory-method="getBankSavings" lazy-init="true" />

<bean id="myBeanFactoryPostProcessor" class="com.dianping.start.MyBeanFactoryPostProcessor" />
<bean id="singletonBeanUpdateTest" class="com.dianping.start.SingletonBeanUpdateTestImpl" scope="singleton" />

<bean id="myBeanFactoryPostProcessor" class="com.dianping.start.MyBeanFactoryPostProcessor" />



Expand Down Expand Up @@ -80,6 +81,7 @@
<bean id="userService" class="com.dianping.aop.UserServiceImpl" />
<bean id="actionOfSpringBean" class="com.dianping.struts.ActionOfSpringBean" >
<property name="userService" ref="userService"/>
<!--<property name="singletonBeanUpdateTest" ref="singletonBeanUpdateTest"/>-->
</bean>


Expand Down

0 comments on commit f693c12

Please sign in to comment.