List of usage examples for org.springframework.context.annotation AnnotationConfigUtils registerAnnotationConfigProcessors
public static void registerAnnotationConfigProcessors(BeanDefinitionRegistry registry)
From source file:br.com.caelum.vraptor.ioc.spring.SpringRegistry.java
void configure() { registerVRaptorComponents();//from w w w . j a v a2s .c om AnnotationConfigUtils.registerAnnotationConfigProcessors((BeanDefinitionRegistry) beanFactory); AopConfigUtils.registerAspectJAnnotationAutoProxyCreatorIfNecessary((BeanDefinitionRegistry) beanFactory); registerCustomInjectionProcessor(); registerCachedComponentsOn(); WebApplicationContextUtils.registerWebApplicationScopes(beanFactory); }
From source file:org.apache.camel.spring.javaconfig.test.JavaConfigContextLoader.java
/** * Loads a new {@link ApplicationContext context} based on the supplied {@code locations}, * configures the context, and finally returns the context in fully <em>refreshed</em> state. * <p/>// w w w. j a va 2 s . c om * * Configuration locations are either fully-qualified class names or base package names. These * locations will be given to a {@link JavaConfigApplicationContext} for configuration via the * {@link JavaConfigApplicationContext#addConfigClass(Class)} and * {@link JavaConfigApplicationContext#addBasePackage(String)} methods. * * @param locations the locations to use to load the application context * @return a new application context * @throws IllegalArgumentException if any of <var>locations</var> are not valid fully-qualified * Class or Package names */ public ApplicationContext loadContext(String... locations) { if (logger.isDebugEnabled()) { logger.debug("Creating a JavaConfigApplicationContext for " + Arrays.asList(locations)); } AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(); ArrayList<Class<?>> configClasses = new ArrayList<Class<?>>(); ArrayList<String> basePackages = new ArrayList<String>(); for (String location : locations) { // if the location refers to a class, use it. Otherwise assume it's a base package name try { final Class<?> aClass = this.getClass().getClassLoader().loadClass(location); configClasses.add(aClass); } catch (ClassNotFoundException e) { if (Package.getPackage(location) == null) { throw new IllegalArgumentException( String.format("A non-existent class or package name was specified: [%s]", location)); } basePackages.add(location); } } if (logger.isDebugEnabled()) { logger.debug("Setting config classes to " + configClasses); logger.debug("Setting base packages to " + basePackages); } for (Class<?> configClass : configClasses) { context.register(configClass); } for (String basePackage : basePackages) { context.scan(basePackage); } context.refresh(); // Have to create a child context that implements BeanDefinitionRegistry // to pass to registerAnnotationConfigProcessors, since // JavaConfigApplicationContext does not final GenericApplicationContext gac = new GenericApplicationContext(context); AnnotationConfigUtils.registerAnnotationConfigProcessors(gac); // copy BeanPostProcessors to the child context for (String bppName : context.getBeanFactory().getBeanNamesForType(BeanPostProcessor.class)) { gac.registerBeanDefinition(bppName, context.getBeanFactory().getBeanDefinition(bppName)); } gac.refresh(); gac.registerShutdownHook(); return gac; }
From source file:org.hyperic.hq.context.IntegrationTestContextLoader.java
public ApplicationContext loadContext(final String... locations) throws Exception { if (logger.isDebugEnabled()) { logger.debug("Loading ApplicationContext for locations [" + StringUtils.arrayToCommaDelimitedString(locations) + "]."); } //EO if logger is enabled final GenericApplicationContext context = new ProxyingGenericApplicationContext(); //verify sigar's resources existence & load native libraries configureSigar(context, logger);//from w ww .j a va 2s .c o m //clean previous application context (if exists) and create a new one Bootstrap.setAppContext(context); try { //initialize the application context delegateLoader.createBeanDefinitionReader(context).loadBeanDefinitions(locations); AnnotationConfigUtils.registerAnnotationConfigProcessors(context); context.refresh(); context.registerShutdownHook(); return context; } catch (Throwable t) { logger.error("An Error had occured during the applicationContext creation, disposing!"); Bootstrap.dispose(); throw (Exception) t; } //EO catch block }
From source file:org.sakaiproject.component.impl.SakaiContextLoader.java
/** * Allows loading/override of custom bean definitions from sakai.home *//from w w w .j a va2s . co m * <p>The pattern is the 'servlet_name-context.xml'</p> * * @param servletContext current servlet context * @return the new WebApplicationContext * @throws org.springframework.beans.BeansException * if the context couldn't be initialized */ @Override public WebApplicationContext initWebApplicationContext(ServletContext servletContext) throws BeansException { ConfigurableWebApplicationContext cwac = (ConfigurableWebApplicationContext) super.initWebApplicationContext( servletContext); // optionally look in sakai home for additional bean deifinitions to load if (cwac != null) { final String servletName = servletContext.getServletContextName(); String location = getHomeBeanDefinitionIfExists(servletName); if (StringUtils.isNotBlank(location)) { log.debug("Servlet " + servletName + " is attempting to load bean definition [" + location + "]"); XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader( (BeanDefinitionRegistry) cwac.getBeanFactory()); try { int loaded = reader.loadBeanDefinitions(new FileSystemResource(location)); log.info("Servlet " + servletName + " loaded " + loaded + " beans from [" + location + "]"); AnnotationConfigUtils.registerAnnotationConfigProcessors(reader.getRegistry()); cwac.getBeanFactory().preInstantiateSingletons(); } catch (BeanDefinitionStoreException bdse) { log.warn("Failure loading beans from [" + location + "]", bdse); } catch (BeanCreationException bce) { log.warn("Failure instantiating beans from [" + location + "]", bce); } } } return cwac; }
From source file:org.springframework.context.annotation.AnnotationProcessorPerformanceTests.java
@Test public void testPrototypeCreationWithResourcePropertiesIsFastEnough() { GenericApplicationContext ctx = new GenericApplicationContext(); AnnotationConfigUtils.registerAnnotationConfigProcessors(ctx); ctx.refresh();//from w w w .j a v a 2 s . c om RootBeanDefinition rbd = new RootBeanDefinition(ResourceAnnotatedTestBean.class); rbd.setScope(RootBeanDefinition.SCOPE_PROTOTYPE); ctx.registerBeanDefinition("test", rbd); ctx.registerBeanDefinition("spouse", new RootBeanDefinition(TestBean.class)); TestBean spouse = (TestBean) ctx.getBean("spouse"); StopWatch sw = new StopWatch(); sw.start("prototype"); for (int i = 0; i < 100000; i++) { TestBean tb = (TestBean) ctx.getBean("test"); assertSame(spouse, tb.getSpouse()); } sw.stop(); assertTrue("Prototype creation took too long: " + sw.getTotalTimeMillis(), sw.getTotalTimeMillis() < 4000); }
From source file:org.springframework.context.annotation.AnnotationProcessorPerformanceTests.java
@Test public void testPrototypeCreationWithOverriddenResourcePropertiesIsFastEnough() { GenericApplicationContext ctx = new GenericApplicationContext(); AnnotationConfigUtils.registerAnnotationConfigProcessors(ctx); ctx.refresh();/* w ww. j ava2 s.co m*/ RootBeanDefinition rbd = new RootBeanDefinition(ResourceAnnotatedTestBean.class); rbd.setScope(RootBeanDefinition.SCOPE_PROTOTYPE); rbd.getPropertyValues().add("spouse", new RuntimeBeanReference("spouse")); ctx.registerBeanDefinition("test", rbd); ctx.registerBeanDefinition("spouse", new RootBeanDefinition(TestBean.class)); TestBean spouse = (TestBean) ctx.getBean("spouse"); StopWatch sw = new StopWatch(); sw.start("prototype"); for (int i = 0; i < 100000; i++) { TestBean tb = (TestBean) ctx.getBean("test"); assertSame(spouse, tb.getSpouse()); } sw.stop(); assertTrue("Prototype creation took too long: " + sw.getTotalTimeMillis(), sw.getTotalTimeMillis() < 4000); }
From source file:org.springframework.context.annotation.AnnotationProcessorPerformanceTests.java
@Test public void testPrototypeCreationWithAutowiredPropertiesIsFastEnough() { GenericApplicationContext ctx = new GenericApplicationContext(); AnnotationConfigUtils.registerAnnotationConfigProcessors(ctx); ctx.refresh();//w ww .j a v a 2 s . c o m RootBeanDefinition rbd = new RootBeanDefinition(AutowiredAnnotatedTestBean.class); rbd.setScope(RootBeanDefinition.SCOPE_PROTOTYPE); ctx.registerBeanDefinition("test", rbd); ctx.registerBeanDefinition("spouse", new RootBeanDefinition(TestBean.class)); TestBean spouse = (TestBean) ctx.getBean("spouse"); StopWatch sw = new StopWatch(); sw.start("prototype"); for (int i = 0; i < 100000; i++) { TestBean tb = (TestBean) ctx.getBean("test"); assertSame(spouse, tb.getSpouse()); } sw.stop(); assertTrue("Prototype creation took too long: " + sw.getTotalTimeMillis(), sw.getTotalTimeMillis() < 4000); }
From source file:org.springframework.context.annotation.AnnotationProcessorPerformanceTests.java
@Test public void testPrototypeCreationWithOverriddenAutowiredPropertiesIsFastEnough() { GenericApplicationContext ctx = new GenericApplicationContext(); AnnotationConfigUtils.registerAnnotationConfigProcessors(ctx); ctx.refresh();//from w w w . j a v a 2 s . co m RootBeanDefinition rbd = new RootBeanDefinition(AutowiredAnnotatedTestBean.class); rbd.setScope(RootBeanDefinition.SCOPE_PROTOTYPE); rbd.getPropertyValues().add("spouse", new RuntimeBeanReference("spouse")); ctx.registerBeanDefinition("test", rbd); ctx.registerBeanDefinition("spouse", new RootBeanDefinition(TestBean.class)); TestBean spouse = (TestBean) ctx.getBean("spouse"); StopWatch sw = new StopWatch(); sw.start("prototype"); for (int i = 0; i < 100000; i++) { TestBean tb = (TestBean) ctx.getBean("test"); assertSame(spouse, tb.getSpouse()); } sw.stop(); assertTrue("Prototype creation took too long: " + sw.getTotalTimeMillis(), sw.getTotalTimeMillis() < 6000); }
From source file:org.springframework.context.expression.ApplicationContextExpressionTests.java
@Test public void genericApplicationContext() throws Exception { GenericApplicationContext ac = new GenericApplicationContext(); AnnotationConfigUtils.registerAnnotationConfigProcessors(ac); ac.getBeanFactory().registerScope("myScope", new Scope() { @Override/* ww w. j av a 2s .c om*/ public Object get(String name, ObjectFactory<?> objectFactory) { return objectFactory.getObject(); } @Override public Object remove(String name) { return null; } @Override public void registerDestructionCallback(String name, Runnable callback) { } @Override public Object resolveContextualObject(String key) { if (key.equals("mySpecialAttr")) { return "42"; } else { return null; } } @Override public String getConversationId() { return null; } }); PropertyPlaceholderConfigurer ppc = new PropertyPlaceholderConfigurer(); Properties placeholders = new Properties(); placeholders.setProperty("code", "123"); ppc.setProperties(placeholders); ac.addBeanFactoryPostProcessor(ppc); GenericBeanDefinition bd0 = new GenericBeanDefinition(); bd0.setBeanClass(TestBean.class); bd0.getPropertyValues().add("name", "myName"); bd0.addQualifier(new AutowireCandidateQualifier(Qualifier.class, "original")); ac.registerBeanDefinition("tb0", bd0); GenericBeanDefinition bd1 = new GenericBeanDefinition(); bd1.setBeanClassName("#{tb0.class}"); bd1.setScope("myScope"); bd1.getConstructorArgumentValues().addGenericArgumentValue("XXX#{tb0.name}YYY#{mySpecialAttr}ZZZ"); bd1.getConstructorArgumentValues().addGenericArgumentValue("#{mySpecialAttr}"); ac.registerBeanDefinition("tb1", bd1); GenericBeanDefinition bd2 = new GenericBeanDefinition(); bd2.setBeanClassName("#{tb1.class.name}"); bd2.setScope("myScope"); bd2.getPropertyValues().add("name", "{ XXX#{tb0.name}YYY#{mySpecialAttr}ZZZ }"); bd2.getPropertyValues().add("age", "#{mySpecialAttr}"); bd2.getPropertyValues().add("country", "${code} #{systemProperties.country}"); ac.registerBeanDefinition("tb2", bd2); GenericBeanDefinition bd3 = new GenericBeanDefinition(); bd3.setBeanClass(ValueTestBean.class); bd3.setScope("myScope"); ac.registerBeanDefinition("tb3", bd3); GenericBeanDefinition bd4 = new GenericBeanDefinition(); bd4.setBeanClass(ConstructorValueTestBean.class); bd4.setScope("myScope"); ac.registerBeanDefinition("tb4", bd4); GenericBeanDefinition bd5 = new GenericBeanDefinition(); bd5.setBeanClass(MethodValueTestBean.class); bd5.setScope("myScope"); ac.registerBeanDefinition("tb5", bd5); GenericBeanDefinition bd6 = new GenericBeanDefinition(); bd6.setBeanClass(PropertyValueTestBean.class); bd6.setScope("myScope"); ac.registerBeanDefinition("tb6", bd6); System.getProperties().put("country", "UK"); try { ac.refresh(); TestBean tb0 = ac.getBean("tb0", TestBean.class); TestBean tb1 = ac.getBean("tb1", TestBean.class); assertEquals("XXXmyNameYYY42ZZZ", tb1.getName()); assertEquals(42, tb1.getAge()); TestBean tb2 = ac.getBean("tb2", TestBean.class); assertEquals("{ XXXmyNameYYY42ZZZ }", tb2.getName()); assertEquals(42, tb2.getAge()); assertEquals("123 UK", tb2.getCountry()); ValueTestBean tb3 = ac.getBean("tb3", ValueTestBean.class); assertEquals("XXXmyNameYYY42ZZZ", tb3.name); assertEquals(42, tb3.age); assertEquals(42, tb3.ageFactory.getObject().intValue()); assertEquals("123 UK", tb3.country); assertEquals("123 UK", tb3.countryFactory.getObject()); System.getProperties().put("country", "US"); assertEquals("123 UK", tb3.country); assertEquals("123 US", tb3.countryFactory.getObject()); System.getProperties().put("country", "UK"); assertEquals("123 UK", tb3.country); assertEquals("123 UK", tb3.countryFactory.getObject()); assertSame(tb0, tb3.tb); tb3 = (ValueTestBean) SerializationTestUtils.serializeAndDeserialize(tb3); assertEquals("123 UK", tb3.countryFactory.getObject()); ConstructorValueTestBean tb4 = ac.getBean("tb4", ConstructorValueTestBean.class); assertEquals("XXXmyNameYYY42ZZZ", tb4.name); assertEquals(42, tb4.age); assertEquals("123 UK", tb4.country); assertSame(tb0, tb4.tb); MethodValueTestBean tb5 = ac.getBean("tb5", MethodValueTestBean.class); assertEquals("XXXmyNameYYY42ZZZ", tb5.name); assertEquals(42, tb5.age); assertEquals("123 UK", tb5.country); assertSame(tb0, tb5.tb); PropertyValueTestBean tb6 = ac.getBean("tb6", PropertyValueTestBean.class); assertEquals("XXXmyNameYYY42ZZZ", tb6.name); assertEquals(42, tb6.age); assertEquals("123 UK", tb6.country); assertSame(tb0, tb6.tb); } finally { System.getProperties().remove("country"); } }
From source file:org.springframework.context.expression.ApplicationContextExpressionTests.java
@Test public void prototypeCreationReevaluatesExpressions() { GenericApplicationContext ac = new GenericApplicationContext(); AnnotationConfigUtils.registerAnnotationConfigProcessors(ac); GenericConversionService cs = new GenericConversionService(); cs.addConverter(String.class, String.class, new Converter<String, String>() { @Override// w ww . j ava 2 s . co m public String convert(String source) { return source.trim(); } }); ac.getBeanFactory().registerSingleton(GenericApplicationContext.CONVERSION_SERVICE_BEAN_NAME, cs); RootBeanDefinition rbd = new RootBeanDefinition(PrototypeTestBean.class); rbd.setScope(RootBeanDefinition.SCOPE_PROTOTYPE); rbd.getPropertyValues().add("country", "#{systemProperties.country}"); rbd.getPropertyValues().add("country2", new TypedStringValue("-#{systemProperties.country}-")); ac.registerBeanDefinition("test", rbd); ac.refresh(); try { System.getProperties().put("name", "juergen1"); System.getProperties().put("country", " UK1 "); PrototypeTestBean tb = (PrototypeTestBean) ac.getBean("test"); assertEquals("juergen1", tb.getName()); assertEquals("UK1", tb.getCountry()); assertEquals("-UK1-", tb.getCountry2()); System.getProperties().put("name", "juergen2"); System.getProperties().put("country", " UK2 "); tb = (PrototypeTestBean) ac.getBean("test"); assertEquals("juergen2", tb.getName()); assertEquals("UK2", tb.getCountry()); assertEquals("-UK2-", tb.getCountry2()); } finally { System.getProperties().remove("name"); System.getProperties().remove("country"); } }