Java Code Examples for org.springframework.beans.factory.support.DefaultListableBeanFactory#destroyBean()

The following examples show how to use org.springframework.beans.factory.support.DefaultListableBeanFactory#destroyBean() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: CommonAnnotationBeanPostProcessorTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Test
public void testResourceInjectionWithPrototypes() {
	DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
	CommonAnnotationBeanPostProcessor bpp = new CommonAnnotationBeanPostProcessor();
	bpp.setResourceFactory(bf);
	bf.addBeanPostProcessor(bpp);
	RootBeanDefinition abd = new RootBeanDefinition(ResourceInjectionBean.class);
	abd.setScope(BeanDefinition.SCOPE_PROTOTYPE);
	bf.registerBeanDefinition("annotatedBean", abd);
	RootBeanDefinition tbd1 = new RootBeanDefinition(TestBean.class);
	tbd1.setScope(BeanDefinition.SCOPE_PROTOTYPE);
	bf.registerBeanDefinition("testBean", tbd1);
	RootBeanDefinition tbd2 = new RootBeanDefinition(TestBean.class);
	tbd2.setScope(BeanDefinition.SCOPE_PROTOTYPE);
	bf.registerBeanDefinition("testBean2", tbd2);

	ResourceInjectionBean bean = (ResourceInjectionBean) bf.getBean("annotatedBean");
	assertTrue(bean.initCalled);
	assertTrue(bean.init2Called);
	assertTrue(bean.init3Called);

	TestBean tb = bean.getTestBean();
	TestBean tb2 = bean.getTestBean2();
	assertNotNull(tb);
	assertNotNull(tb2);

	ResourceInjectionBean anotherBean = (ResourceInjectionBean) bf.getBean("annotatedBean");
	assertNotSame(anotherBean, bean);
	assertNotSame(anotherBean.getTestBean(), tb);
	assertNotSame(anotherBean.getTestBean2(), tb2);

	bf.destroyBean("annotatedBean", bean);
	assertTrue(bean.destroyCalled);
	assertTrue(bean.destroy2Called);
	assertTrue(bean.destroy3Called);
}
 
Example 2
Source File: DefaultListableBeanFactoryTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Test
public void testCreateBean() {
	DefaultListableBeanFactory lbf = new DefaultListableBeanFactory();
	TestBean tb = lbf.createBean(TestBean.class);
	assertSame(lbf, tb.getBeanFactory());
	lbf.destroyBean(tb);
}
 
Example 3
Source File: DefaultListableBeanFactoryTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Test
public void testCreateBeanWithDisposableBean() {
	DefaultListableBeanFactory lbf = new DefaultListableBeanFactory();
	DerivedTestBean tb = lbf.createBean(DerivedTestBean.class);
	assertSame(lbf, tb.getBeanFactory());
	lbf.destroyBean(tb);
	assertTrue(tb.wasDestroyed());
}
 
Example 4
Source File: CommonAnnotationBeanPostProcessorTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Test
public void testResourceInjectionWithPrototypes() {
	DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
	CommonAnnotationBeanPostProcessor bpp = new CommonAnnotationBeanPostProcessor();
	bpp.setResourceFactory(bf);
	bf.addBeanPostProcessor(bpp);
	RootBeanDefinition abd = new RootBeanDefinition(ResourceInjectionBean.class);
	abd.setScope(BeanDefinition.SCOPE_PROTOTYPE);
	bf.registerBeanDefinition("annotatedBean", abd);
	RootBeanDefinition tbd1 = new RootBeanDefinition(TestBean.class);
	tbd1.setScope(BeanDefinition.SCOPE_PROTOTYPE);
	bf.registerBeanDefinition("testBean", tbd1);
	RootBeanDefinition tbd2 = new RootBeanDefinition(TestBean.class);
	tbd2.setScope(BeanDefinition.SCOPE_PROTOTYPE);
	bf.registerBeanDefinition("testBean2", tbd2);

	ResourceInjectionBean bean = (ResourceInjectionBean) bf.getBean("annotatedBean");
	assertTrue(bean.initCalled);
	assertTrue(bean.init2Called);
	assertTrue(bean.init3Called);

	TestBean tb = bean.getTestBean();
	TestBean tb2 = bean.getTestBean2();
	assertNotNull(tb);
	assertNotNull(tb2);

	ResourceInjectionBean anotherBean = (ResourceInjectionBean) bf.getBean("annotatedBean");
	assertNotSame(anotherBean, bean);
	assertNotSame(anotherBean.getTestBean(), tb);
	assertNotSame(anotherBean.getTestBean2(), tb2);

	bf.destroyBean("annotatedBean", bean);
	assertTrue(bean.destroyCalled);
	assertTrue(bean.destroy2Called);
	assertTrue(bean.destroy3Called);
}
 
Example 5
Source File: DefaultListableBeanFactoryTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Test
public void testCreateBean() {
	DefaultListableBeanFactory lbf = new DefaultListableBeanFactory();
	TestBean tb = lbf.createBean(TestBean.class);
	assertSame(lbf, tb.getBeanFactory());
	lbf.destroyBean(tb);
}
 
Example 6
Source File: DefaultListableBeanFactoryTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Test
public void testCreateBeanWithDisposableBean() {
	DefaultListableBeanFactory lbf = new DefaultListableBeanFactory();
	DerivedTestBean tb = lbf.createBean(DerivedTestBean.class);
	assertSame(lbf, tb.getBeanFactory());
	lbf.destroyBean(tb);
	assertTrue(tb.wasDestroyed());
}
 
Example 7
Source File: SpringUtil.java    From youran with Apache License 2.0 5 votes vote down vote up
/**
 * 销毁bean
 *
 * @param beanName
 * @return
 */
public static boolean destroy(String beanName) {
    DefaultListableBeanFactory beanFactory = getBeanFactory();
    if (beanFactory == null) {
        throw new BusinessException("spring容器未启动");
    }
    if (!beanFactory.containsBean(beanName)) {
        return false;
    }
    beanFactory.destroySingleton(beanName);
    beanFactory.destroyBean(beanName);
    beanFactory.removeBeanDefinition(beanName);
    return true;
}
 
Example 8
Source File: CommonAnnotationBeanPostProcessorTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Test
public void testResourceInjectionWithPrototypes() {
	DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
	CommonAnnotationBeanPostProcessor bpp = new CommonAnnotationBeanPostProcessor();
	bpp.setResourceFactory(bf);
	bf.addBeanPostProcessor(bpp);
	RootBeanDefinition abd = new RootBeanDefinition(ResourceInjectionBean.class);
	abd.setScope(BeanDefinition.SCOPE_PROTOTYPE);
	bf.registerBeanDefinition("annotatedBean", abd);
	RootBeanDefinition tbd1 = new RootBeanDefinition(TestBean.class);
	tbd1.setScope(BeanDefinition.SCOPE_PROTOTYPE);
	bf.registerBeanDefinition("testBean", tbd1);
	RootBeanDefinition tbd2 = new RootBeanDefinition(TestBean.class);
	tbd2.setScope(BeanDefinition.SCOPE_PROTOTYPE);
	bf.registerBeanDefinition("testBean2", tbd2);

	ResourceInjectionBean bean = (ResourceInjectionBean) bf.getBean("annotatedBean");
	assertTrue(bean.initCalled);
	assertTrue(bean.init2Called);
	assertTrue(bean.init3Called);

	TestBean tb = bean.getTestBean();
	TestBean tb2 = bean.getTestBean2();
	assertNotNull(tb);
	assertNotNull(tb2);

	ResourceInjectionBean anotherBean = (ResourceInjectionBean) bf.getBean("annotatedBean");
	assertNotSame(anotherBean, bean);
	assertNotSame(anotherBean.getTestBean(), tb);
	assertNotSame(anotherBean.getTestBean2(), tb2);

	bf.destroyBean("annotatedBean", bean);
	assertTrue(bean.destroyCalled);
	assertTrue(bean.destroy2Called);
	assertTrue(bean.destroy3Called);
}
 
Example 9
Source File: DefaultListableBeanFactoryTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Test
public void testCreateBean() {
	DefaultListableBeanFactory lbf = new DefaultListableBeanFactory();
	TestBean tb = lbf.createBean(TestBean.class);
	assertSame(lbf, tb.getBeanFactory());
	lbf.destroyBean(tb);
}
 
Example 10
Source File: DefaultListableBeanFactoryTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Test
public void testCreateBeanWithDisposableBean() {
	DefaultListableBeanFactory lbf = new DefaultListableBeanFactory();
	DerivedTestBean tb = lbf.createBean(DerivedTestBean.class);
	assertSame(lbf, tb.getBeanFactory());
	lbf.destroyBean(tb);
	assertTrue(tb.wasDestroyed());
}
 
Example 11
Source File: BeanLifecycleDemo.java    From geekbang-lessons with Apache License 2.0 4 votes vote down vote up
public static void main(String[] args) throws InterruptedException {
    DefaultListableBeanFactory beanFactory = new DefaultListableBeanFactory();
    // 添加 BeanPostProcessor 实现 MyInstantiationAwareBeanPostProcessor
    beanFactory.addBeanPostProcessor(new MyInstantiationAwareBeanPostProcessor());
    // 添加 MyDestructionAwareBeanPostProcessor 执行销毁前回调
    beanFactory.addBeanPostProcessor(new MyDestructionAwareBeanPostProcessor());
    // 添加 CommonAnnotationBeanPostProcessor 解决 @PostConstruct @PreDestroy
    beanFactory.addBeanPostProcessor(new CommonAnnotationBeanPostProcessor());

    XmlBeanDefinitionReader beanDefinitionReader = new XmlBeanDefinitionReader(beanFactory);
    String[] locations = {"META-INF/dependency-lookup-context.xml", "META-INF/bean-constructor-dependency-injection.xml"};
    int beanNumbers = beanDefinitionReader.loadBeanDefinitions(locations);
    System.out.println("已加载 BeanDefinition 数量:" + beanNumbers);
    // 显示地执行 preInstantiateSingletons()
    // SmartInitializingSingleton 通常在 Spring ApplicationContext 场景使用
    // preInstantiateSingletons 将已注册的 BeanDefinition 初始化成 Spring Bean
    beanFactory.preInstantiateSingletons();

    // 通过 Bean Id 和类型进行依赖查找
    User user = beanFactory.getBean("user", User.class);
    System.out.println(user);

    User superUser = beanFactory.getBean("superUser", User.class);
    System.out.println(superUser);

    // 构造器注入按照类型注入,resolveDependency
    UserHolder userHolder = beanFactory.getBean("userHolder", UserHolder.class);

    System.out.println(userHolder);

    // 执行 Bean 销毁(容器内)
    beanFactory.destroyBean("userHolder", userHolder);
    // Bean 销毁并不意味着 Bean 垃圾回收了
    System.out.println(userHolder);

    // 销毁 BeanFactory 中的单例 Bean
    beanFactory.destroySingletons();
    // 强制 GC
    System.gc();
    // 等待一段时间
    Thread.sleep(1000L);
    // 强制 GC
    System.gc();
}