org.springframework.context.annotation.CommonAnnotationBeanPostProcessor Java Examples

The following examples show how to use org.springframework.context.annotation.CommonAnnotationBeanPostProcessor. 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: EndpointDefinitionParser.java    From cxf with Apache License 2.0 6 votes vote down vote up
public static final void setBlocking(ApplicationContext ctx, EndpointImpl impl) {
    AutowireCapableBeanFactory fact = ctx.getAutowireCapableBeanFactory();
    if (fact instanceof DefaultListableBeanFactory) {
        DefaultListableBeanFactory dlbf = (DefaultListableBeanFactory)fact;
        for (BeanPostProcessor bpp : dlbf.getBeanPostProcessors()) {
            if (CommonAnnotationBeanPostProcessor.class.isInstance(bpp)) {
                impl.getServerFactory().setBlockPostConstruct(true);
                impl.getServerFactory().setBlockInjection(false);
                return;
            }
            if (bpp instanceof Jsr250BeanPostProcessor) {
                impl.getServerFactory().setBlockInjection(true);
            }
        }
    }
}
 
Example #2
Source File: BeanValidationPostProcessorTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void testNotNullConstraint() {
	GenericApplicationContext ac = new GenericApplicationContext();
	ac.registerBeanDefinition("bvpp", new RootBeanDefinition(BeanValidationPostProcessor.class));
	ac.registerBeanDefinition("capp", new RootBeanDefinition(CommonAnnotationBeanPostProcessor.class));
	ac.registerBeanDefinition("bean", new RootBeanDefinition(NotNullConstrainedBean.class));
	try {
		ac.refresh();
		fail("Should have thrown BeanCreationException");
	}
	catch (BeanCreationException ex) {
		assertTrue(ex.getRootCause().getMessage().contains("testBean"));
		assertTrue(ex.getRootCause().getMessage().contains("invalid"));
	}
	ac.close();
}
 
Example #3
Source File: BeanValidationPostProcessorTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Test
public void testNotNullConstraint() {
	GenericApplicationContext ac = new GenericApplicationContext();
	ac.registerBeanDefinition("bvpp", new RootBeanDefinition(BeanValidationPostProcessor.class));
	ac.registerBeanDefinition("capp", new RootBeanDefinition(CommonAnnotationBeanPostProcessor.class));
	ac.registerBeanDefinition("bean", new RootBeanDefinition(NotNullConstrainedBean.class));
	try {
		ac.refresh();
		fail("Should have thrown BeanCreationException");
	}
	catch (BeanCreationException ex) {
		assertTrue(ex.getRootCause().getMessage().contains("testBean"));
		assertTrue(ex.getRootCause().getMessage().contains("invalid"));
	}
	ac.close();
}
 
Example #4
Source File: BeanValidationPostProcessorTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test
public void testNotNullConstraint() {
	GenericApplicationContext ac = new GenericApplicationContext();
	ac.registerBeanDefinition("bvpp", new RootBeanDefinition(BeanValidationPostProcessor.class));
	ac.registerBeanDefinition("capp", new RootBeanDefinition(CommonAnnotationBeanPostProcessor.class));
	ac.registerBeanDefinition("bean", new RootBeanDefinition(NotNullConstrainedBean.class));
	try {
		ac.refresh();
		fail("Should have thrown BeanCreationException");
	}
	catch (BeanCreationException ex) {
		assertTrue(ex.getRootCause().getMessage().contains("testBean"));
		assertTrue(ex.getRootCause().getMessage().contains("invalid"));
	}
	ac.close();
}
 
Example #5
Source File: BeanValidationPostProcessorTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void testNotNullConstraint() {
	GenericApplicationContext ac = new GenericApplicationContext();
	ac.registerBeanDefinition("bvpp", new RootBeanDefinition(BeanValidationPostProcessor.class));
	ac.registerBeanDefinition("capp", new RootBeanDefinition(CommonAnnotationBeanPostProcessor.class));
	ac.registerBeanDefinition("bean", new RootBeanDefinition(NotNullConstrainedBean.class));
	try {
		ac.refresh();
		fail("Should have thrown BeanCreationException");
	}
	catch (BeanCreationException ex) {
		assertTrue(ex.getRootCause().getMessage().contains("testBean"));
		assertTrue(ex.getRootCause().getMessage().contains("invalid"));
	}
	ac.close();
}
 
Example #6
Source File: BeanValidationPostProcessorTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test
public void testNotNullConstraint() {
	GenericApplicationContext ac = new GenericApplicationContext();
	ac.registerBeanDefinition("bvpp", new RootBeanDefinition(BeanValidationPostProcessor.class));
	ac.registerBeanDefinition("capp", new RootBeanDefinition(CommonAnnotationBeanPostProcessor.class));
	ac.registerBeanDefinition("bean", new RootBeanDefinition(NotNullConstrainedBean.class));
	try {
		ac.refresh();
		fail("Should have thrown BeanCreationException");
	}
	catch (BeanCreationException ex) {
		assertTrue(ex.getRootCause().getMessage().contains("testBean"));
		assertTrue(ex.getRootCause().getMessage().contains("invalid"));
	}
	ac.close();
}
 
Example #7
Source File: BeanValidationPostProcessorTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Test
public void testNotNullConstraintAfterInitialization() {
	GenericApplicationContext ac = new GenericApplicationContext();
	RootBeanDefinition bvpp = new RootBeanDefinition(BeanValidationPostProcessor.class);
	bvpp.getPropertyValues().add("afterInitialization", true);
	ac.registerBeanDefinition("bvpp", bvpp);
	ac.registerBeanDefinition("capp", new RootBeanDefinition(CommonAnnotationBeanPostProcessor.class));
	ac.registerBeanDefinition("bean", new RootBeanDefinition(AfterInitConstraintBean.class));
	ac.refresh();
	ac.close();
}
 
Example #8
Source File: BeanValidationPostProcessorTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Test
public void testNotNullConstraintAfterInitialization() {
	GenericApplicationContext ac = new GenericApplicationContext();
	RootBeanDefinition bvpp = new RootBeanDefinition(BeanValidationPostProcessor.class);
	bvpp.getPropertyValues().add("afterInitialization", true);
	ac.registerBeanDefinition("bvpp", bvpp);
	ac.registerBeanDefinition("capp", new RootBeanDefinition(CommonAnnotationBeanPostProcessor.class));
	ac.registerBeanDefinition("bean", new RootBeanDefinition(AfterInitConstraintBean.class));
	ac.refresh();
	ac.close();
}
 
Example #9
Source File: BeanValidationPostProcessorTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Test
public void testNotNullConstraintSatisfied() {
	GenericApplicationContext ac = new GenericApplicationContext();
	ac.registerBeanDefinition("bvpp", new RootBeanDefinition(BeanValidationPostProcessor.class));
	ac.registerBeanDefinition("capp", new RootBeanDefinition(CommonAnnotationBeanPostProcessor.class));
	RootBeanDefinition bd = new RootBeanDefinition(NotNullConstrainedBean.class);
	bd.getPropertyValues().add("testBean", new TestBean());
	ac.registerBeanDefinition("bean", bd);
	ac.refresh();
	ac.close();
}
 
Example #10
Source File: BeanValidationPostProcessorTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Test
public void testNotNullConstraintAfterInitialization() {
	GenericApplicationContext ac = new GenericApplicationContext();
	RootBeanDefinition bvpp = new RootBeanDefinition(BeanValidationPostProcessor.class);
	bvpp.getPropertyValues().add("afterInitialization", true);
	ac.registerBeanDefinition("bvpp", bvpp);
	ac.registerBeanDefinition("capp", new RootBeanDefinition(CommonAnnotationBeanPostProcessor.class));
	ac.registerBeanDefinition("bean", new RootBeanDefinition(AfterInitConstraintBean.class));
	ac.refresh();
}
 
Example #11
Source File: BeanValidationPostProcessorTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Test
public void testNotNullConstraintSatisfied() {
	GenericApplicationContext ac = new GenericApplicationContext();
	ac.registerBeanDefinition("bvpp", new RootBeanDefinition(BeanValidationPostProcessor.class));
	ac.registerBeanDefinition("capp", new RootBeanDefinition(CommonAnnotationBeanPostProcessor.class));
	RootBeanDefinition bd = new RootBeanDefinition(NotNullConstrainedBean.class);
	bd.getPropertyValues().add("testBean", new TestBean());
	ac.registerBeanDefinition("bean", bd);
	ac.refresh();
}
 
Example #12
Source File: BeanValidationPostProcessorTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Test
public void testNotNullConstraint() {
	GenericApplicationContext ac = new GenericApplicationContext();
	ac.registerBeanDefinition("bvpp", new RootBeanDefinition(BeanValidationPostProcessor.class));
	ac.registerBeanDefinition("capp", new RootBeanDefinition(CommonAnnotationBeanPostProcessor.class));
	ac.registerBeanDefinition("bean", new RootBeanDefinition(NotNullConstrainedBean.class));
	try {
		ac.refresh();
		fail("Should have thrown BeanCreationException");
	}
	catch (BeanCreationException ex) {
		assertTrue(ex.getRootCause().getMessage().contains("testBean"));
		assertTrue(ex.getRootCause().getMessage().contains("invalid"));
	}
}
 
Example #13
Source File: BeanValidationPostProcessorTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Test
public void testNotNullConstraintAfterInitialization() {
	GenericApplicationContext ac = new GenericApplicationContext();
	RootBeanDefinition bvpp = new RootBeanDefinition(BeanValidationPostProcessor.class);
	bvpp.getPropertyValues().add("afterInitialization", true);
	ac.registerBeanDefinition("bvpp", bvpp);
	ac.registerBeanDefinition("capp", new RootBeanDefinition(CommonAnnotationBeanPostProcessor.class));
	ac.registerBeanDefinition("bean", new RootBeanDefinition(AfterInitConstraintBean.class));
	ac.refresh();
	ac.close();
}
 
Example #14
Source File: BeanValidationPostProcessorTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Test
public void testNotNullConstraintSatisfied() {
	GenericApplicationContext ac = new GenericApplicationContext();
	ac.registerBeanDefinition("bvpp", new RootBeanDefinition(BeanValidationPostProcessor.class));
	ac.registerBeanDefinition("capp", new RootBeanDefinition(CommonAnnotationBeanPostProcessor.class));
	RootBeanDefinition bd = new RootBeanDefinition(NotNullConstrainedBean.class);
	bd.getPropertyValues().add("testBean", new TestBean());
	ac.registerBeanDefinition("bean", bd);
	ac.refresh();
	ac.close();
}
 
Example #15
Source File: BeanValidationPostProcessorTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Test
public void testNotNullConstraintAfterInitializationWithProxy() {
	GenericApplicationContext ac = new GenericApplicationContext();
	RootBeanDefinition bvpp = new RootBeanDefinition(BeanValidationPostProcessor.class);
	bvpp.getPropertyValues().add("afterInitialization", true);
	ac.registerBeanDefinition("bvpp", bvpp);
	ac.registerBeanDefinition("capp", new RootBeanDefinition(CommonAnnotationBeanPostProcessor.class));
	ac.registerBeanDefinition("bean", new RootBeanDefinition(AfterInitConstraintBean.class));
	ac.registerBeanDefinition("autoProxyCreator", new RootBeanDefinition(DefaultAdvisorAutoProxyCreator.class));
	ac.registerBeanDefinition("asyncAdvisor", new RootBeanDefinition(AsyncAnnotationAdvisor.class));
	ac.refresh();
	ac.close();
}
 
Example #16
Source File: BeanValidationPostProcessorTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Test
public void testNotNullConstraintSatisfied() {
	GenericApplicationContext ac = new GenericApplicationContext();
	ac.registerBeanDefinition("bvpp", new RootBeanDefinition(BeanValidationPostProcessor.class));
	ac.registerBeanDefinition("capp", new RootBeanDefinition(CommonAnnotationBeanPostProcessor.class));
	RootBeanDefinition bd = new RootBeanDefinition(NotNullConstrainedBean.class);
	bd.getPropertyValues().add("testBean", new TestBean());
	ac.registerBeanDefinition("bean", bd);
	ac.refresh();
	ac.close();
}
 
Example #17
Source File: BeanInitializationLifecycleDemo.java    From geekbang-lessons with Apache License 2.0 5 votes vote down vote up
private static void executeBeanFactory() {
    DefaultListableBeanFactory beanFactory = new DefaultListableBeanFactory();
    // 添加 BeanPostProcessor 实现 MyInstantiationAwareBeanPostProcessor
    beanFactory.addBeanPostProcessor(new MyInstantiationAwareBeanPostProcessor());
    // 添加 CommonAnnotationBeanPostProcessor 解决 @PostConstruct
    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);

}
 
Example #18
Source File: BeanValidationPostProcessorTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Test
public void testNotNullConstraintAfterInitialization() {
	GenericApplicationContext ac = new GenericApplicationContext();
	RootBeanDefinition bvpp = new RootBeanDefinition(BeanValidationPostProcessor.class);
	bvpp.getPropertyValues().add("afterInitialization", true);
	ac.registerBeanDefinition("bvpp", bvpp);
	ac.registerBeanDefinition("capp", new RootBeanDefinition(CommonAnnotationBeanPostProcessor.class));
	ac.registerBeanDefinition("bean", new RootBeanDefinition(AfterInitConstraintBean.class));
	ac.refresh();
	ac.close();
}
 
Example #19
Source File: BeanValidationPostProcessorTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Test
public void testNotNullConstraintSatisfied() {
	GenericApplicationContext ac = new GenericApplicationContext();
	ac.registerBeanDefinition("bvpp", new RootBeanDefinition(BeanValidationPostProcessor.class));
	ac.registerBeanDefinition("capp", new RootBeanDefinition(CommonAnnotationBeanPostProcessor.class));
	RootBeanDefinition bd = new RootBeanDefinition(NotNullConstrainedBean.class);
	bd.getPropertyValues().add("testBean", new TestBean());
	ac.registerBeanDefinition("bean", bd);
	ac.refresh();
	ac.close();
}
 
Example #20
Source File: BeanValidationPostProcessorTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Test
public void testNotNullConstraintAfterInitializationWithProxy() {
	GenericApplicationContext ac = new GenericApplicationContext();
	RootBeanDefinition bvpp = new RootBeanDefinition(BeanValidationPostProcessor.class);
	bvpp.getPropertyValues().add("afterInitialization", true);
	ac.registerBeanDefinition("bvpp", bvpp);
	ac.registerBeanDefinition("capp", new RootBeanDefinition(CommonAnnotationBeanPostProcessor.class));
	ac.registerBeanDefinition("bean", new RootBeanDefinition(AfterInitConstraintBean.class));
	ac.registerBeanDefinition("autoProxyCreator", new RootBeanDefinition(DefaultAdvisorAutoProxyCreator.class));
	ac.registerBeanDefinition("asyncAdvisor", new RootBeanDefinition(AsyncAnnotationAdvisor.class));
	ac.refresh();
	ac.close();
}
 
Example #21
Source File: BeanValidationPostProcessorTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Test
public void testNotNullConstraintAfterInitialization() {
	GenericApplicationContext ac = new GenericApplicationContext();
	RootBeanDefinition bvpp = new RootBeanDefinition(BeanValidationPostProcessor.class);
	bvpp.getPropertyValues().add("afterInitialization", true);
	ac.registerBeanDefinition("bvpp", bvpp);
	ac.registerBeanDefinition("capp", new RootBeanDefinition(CommonAnnotationBeanPostProcessor.class));
	ac.registerBeanDefinition("bean", new RootBeanDefinition(AfterInitConstraintBean.class));
	ac.refresh();
	ac.close();
}
 
Example #22
Source File: BeanValidationPostProcessorTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Test
public void testNotNullConstraintSatisfied() {
	GenericApplicationContext ac = new GenericApplicationContext();
	ac.registerBeanDefinition("bvpp", new RootBeanDefinition(BeanValidationPostProcessor.class));
	ac.registerBeanDefinition("capp", new RootBeanDefinition(CommonAnnotationBeanPostProcessor.class));
	RootBeanDefinition bd = new RootBeanDefinition(NotNullConstrainedBean.class);
	bd.getPropertyValues().add("testBean", new TestBean());
	ac.registerBeanDefinition("bean", bd);
	ac.refresh();
	ac.close();
}
 
Example #23
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();
}