You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Because @Configuration classes don't produce bean definitions in quite the same way that XML configuration files do, an annotation-based @SpringConfigured annotation must work differently than context:spring-configured/. Consider the following approach:
@Configurationpublic class SpringConfiguredBeans {
@SpringConfigured
public void someEntity(MyEntity entity) {
entity.setRepository(myRepository());
}
@Bean
public MyRepository myRepository() {
return new MyRepository(dataSource());
}
// ...}
In response to comments on the aforementioned blog...
Maverick,
Yes, as described now, #12544 would essentially be a work-around, and you are correct: we would want the effects of using <context:spring-configured /> in XML to happen automatically with an @Configuration class approach as well.
A more robust solution would likely introduce a new @Enable* annotation -- for example, @EnableSpringConfigured - similar to what @EnableLoadTimeWeaving does for load-time weaving.
By the way, have you attempted configuring AnnotationBeanConfigurerAspect in your @Configuration class, analogous to what SpringConfiguredBeanDefinitionParser does behind the scenes when using <context:spring-configured />?
Yes, I stumbled across the SpringConfiguredBeanDefinitionParser, though there are two of them, but could not figure how to apply that to an @Configuration class.
Frank, if you read the JavaDoc for the two implementations of SpringConfiguredBeanDefinitionParser, you'll see that they are essentially the same code; they achieve the same goal.
As described in the Reference Manual, you can achieve the same as the context and aop XML namespaces by defining a bean as follows:
Note: I have not tried this out, but something similar to the following should hopefully work...
@Configuration
public class Config {
@Bean
public AnnotationBeanConfigurerAspect annotationBeanConfigurerAspect() {
return Aspects.aspectOf(AnnotationBeanConfigurerAspect.class);
}
}
Is the above described solution considered final or will there still be an effort to provide an annotation @EnableXY to avoid such kind of boiler plate code?
RC1 is pending release later today, and there's still a bit of thought to give to this one before going ahead with it. Given that there's a decent alternative (as described above), we'll wait for RC2 to introduce this.
This is complete, please give @EnableSpringConfigured a try!
commit be29c57ce063f51a215ffa1b9d0df0678121eb41
Author: Chris Beams <[email protected]>
Date: Sun Nov 27 22:34:16 2011 -0800
Introduce @EnableSpringConfigured
Equivalent to <context:spring-configured/>.
Also update @EnableLoadTimeWeaving Javadoc and spring-configured XSD
documentation to reflect.
I tried this but I'm having issues deploying to Google App Engine infrastructure. In @Configured classes dependencies are not injected.
Note that in the local dev environment this is working without any problem.
I'm wondering 2 things:
Is @EnableSpringConfigured doing something sepcial that might break on GAE
Is there a way I can debug this? All I have now is:
[s~vootynl-development/0-1.355018655005944620].<stdout>: 06:21:45,181 ERROR [org.springframework.web.context.ContextLoader] U: - Context initialization failed
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'bootStrap': Invocation of init method failed; nested exception is java.lang.IllegalStateException: Objectify Factory has not been injected
at org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor.postProcessBeforeInitialization(InitDestroyAnnotationBeanPostProcessor.java:135)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyBeanPostProcessorsBeforeInitialization(AbstractAutowireCapableBeanFactory.java:393)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1437)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:518)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:455)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:294)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:225)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:291)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:193)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:585)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:913)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:464)
at org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:381)
at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:283)
at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:111)
at org.mortbay.jetty.handler.ContextHandler.startContext(ContextHandler.java:548)
at org.mortbay.jetty.servlet.Context.startContext(Context.java:136)
at org.mortbay.jetty.webapp.WebAppContext.startContext(WebAppContext.java:1250)
at org.mortbay.jetty.handler.ContextHandler.doStart(ContextHandler.java:517)
at org.mortbay.jetty.webapp.WebAppContext.doStart(WebAppContext.java:467)
at org.mortbay.component.AbstractLifeCycle.start(AbstractLifeCycle.java:50)
at com.google.apphosting.runtime.jetty.AppVersionHandlerMap.createHandler(AppVersionHandlerMap.java:202)
at com.google.apphosting.runtime.jetty.AppVersionHandlerMap.getHandler(AppVersionHandlerMap.java:171)
at com.google.apphosting.runtime.jetty.JettyServletEngineAdapter.serviceRequest(JettyServletEngineAdapter.java:123)
at com.google.apphosting.runtime.JavaRuntime$RequestRunnable.run(JavaRuntime.java:392)
at com.google.tracing.TraceContext$TraceContextRunnable.runInContext(TraceContext.java:449)
at com.google.tracing.TraceContext$TraceContextRunnable$1.run(TraceContext.java:455)
at com.google.tracing.TraceContext.runInContext(TraceContext.java:695)
at com.google.tracing.TraceContext$AbstractTraceContextCallback.runInInheritedContextNoUnref(TraceContext.java:333)
at com.google.tracing.TraceContext$AbstractTraceContextCallback.runInInheritedContext(TraceContext.java:325)
at com.google.tracing.TraceContext$TraceContextRunnable.run(TraceContext.java:453)
at com.google.apphosting.runtime.ThreadGroupPool$PoolEntry.run(ThreadGroupPool.java:162)
at java.lang.Thread.run(Thread.java:679)
which does not give me an idea why the injection in @Configured classes is not working.
Do you get the same behavior using <context:spring-configured/> instead of @EnableSpringConfigured? What if you don't use either?
Also, you mention @Configured - there is no such annotation in Spring. Do you mean @Configurable, or @Configuration? Keep in mind that @EnableSpringConfigured enables dependency injection for @Configurable classes not otherwise managed by the Spring container, e.g. domain objects/entities, etc.
If this continues to be an issue for you, please create a new issue for it and reference this one.
Activity
spring-projects-issues commentedon Jul 20, 2011
Sam Brannen commented
See comments from the Spring 3.1 M2: Testing with @Configuration Classes and Profiles blog post for a related discussion.
spring-projects-issues commentedon Jul 20, 2011
Sam Brannen commented
In response to comments on the aforementioned blog...
Maverick,
Yes, as described now, #12544 would essentially be a work-around, and you are correct: we would want the effects of using
<context:spring-configured />in XML to happen automatically with an@Configurationclass approach as well.A more robust solution would likely introduce a new
@Enable*annotation -- for example,@EnableSpringConfigured- similar to what@EnableLoadTimeWeavingdoes for load-time weaving.By the way, have you attempted configuring
AnnotationBeanConfigurerAspectin your@Configurationclass, analogous to whatSpringConfiguredBeanDefinitionParserdoes behind the scenes when using<context:spring-configured />?spring-projects-issues commentedon Jul 20, 2011
Chris Beams commented
Slating this for 3.1 RC1, and will likely take the form of an
@Enableannotation.spring-projects-issues commentedon Jul 20, 2011
Frank Scheffler commented
Yes, I stumbled across the SpringConfiguredBeanDefinitionParser, though there are two of them, but could not figure how to apply that to an
@Configurationclass.spring-projects-issues commentedon Jul 24, 2011
Sam Brannen commented
Frank, if you read the JavaDoc for the two implementations of
SpringConfiguredBeanDefinitionParser, you'll see that they are essentially the same code; they achieve the same goal.As described in the Reference Manual, you can achieve the same as the
contextandaopXML namespaces by defining a bean as follows:Note: I have not tried this out, but something similar to the following should hopefully work...
Let us know if that works for you.
spring-projects-issues commentedon Aug 1, 2011
Frank Scheffler commented
Works perfect.
spring-projects-issues commentedon Aug 3, 2011
Frank Scheffler commented
Is the above described solution considered final or will there still be an effort to provide an annotation
@EnableXYto avoid such kind of boiler plate code?spring-projects-issues commentedon Aug 3, 2011
Chris Beams commented
Hi Frank,
Yes, we will introduce an
@Enableannotation to this effect. This issue tracks that effort -- expect it for RC1.Chris
spring-projects-issues commentedon Aug 4, 2011
Marcel Overdijk commented
Hi Chris,
I know I shouldn't ask it here, but any idea when we can expect RC1?
spring-projects-issues commentedon Aug 5, 2011
Chris Beams commented
Hi Marcel, No exact date at the moment, but generally you can expect RC1 within the next month.
spring-projects-issues commentedon Oct 11, 2011
Chris Beams commented
RC1 is pending release later today, and there's still a bit of thought to give to this one before going ahead with it. Given that there's a decent alternative (as described above), we'll wait for RC2 to introduce this.
spring-projects-issues commentedon Nov 27, 2011
Chris Beams commented
This is complete, please give
@EnableSpringConfigureda try!spring-projects-issues commentedon Nov 28, 2011
Marcel Overdijk commented
I tried this but I'm having issues deploying to Google App Engine infrastructure. In
@Configuredclasses dependencies are not injected.Note that in the local dev environment this is working without any problem.
I'm wondering 2 things:
@EnableSpringConfigureddoing something sepcial that might break on GAE[s~vootynl-development/0-1.355018655005944620].<stdout>: 06:21:45,181 ERROR [org.springframework.web.context.ContextLoader] U: - Context initialization failed
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'bootStrap': Invocation of init method failed; nested exception is java.lang.IllegalStateException: Objectify Factory has not been injected
at org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor.postProcessBeforeInitialization(InitDestroyAnnotationBeanPostProcessor.java:135)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyBeanPostProcessorsBeforeInitialization(AbstractAutowireCapableBeanFactory.java:393)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1437)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:518)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:455)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:294)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:225)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:291)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:193)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:585)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:913)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:464)
at org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:381)
at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:283)
at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:111)
at org.mortbay.jetty.handler.ContextHandler.startContext(ContextHandler.java:548)
at org.mortbay.jetty.servlet.Context.startContext(Context.java:136)
at org.mortbay.jetty.webapp.WebAppContext.startContext(WebAppContext.java:1250)
at org.mortbay.jetty.handler.ContextHandler.doStart(ContextHandler.java:517)
at org.mortbay.jetty.webapp.WebAppContext.doStart(WebAppContext.java:467)
at org.mortbay.component.AbstractLifeCycle.start(AbstractLifeCycle.java:50)
at com.google.apphosting.runtime.jetty.AppVersionHandlerMap.createHandler(AppVersionHandlerMap.java:202)
at com.google.apphosting.runtime.jetty.AppVersionHandlerMap.getHandler(AppVersionHandlerMap.java:171)
at com.google.apphosting.runtime.jetty.JettyServletEngineAdapter.serviceRequest(JettyServletEngineAdapter.java:123)
at com.google.apphosting.runtime.JavaRuntime$RequestRunnable.run(JavaRuntime.java:392)
at com.google.tracing.TraceContext$TraceContextRunnable.runInContext(TraceContext.java:449)
at com.google.tracing.TraceContext$TraceContextRunnable$1.run(TraceContext.java:455)
at com.google.tracing.TraceContext.runInContext(TraceContext.java:695)
at com.google.tracing.TraceContext$AbstractTraceContextCallback.runInInheritedContextNoUnref(TraceContext.java:333)
at com.google.tracing.TraceContext$AbstractTraceContextCallback.runInInheritedContext(TraceContext.java:325)
at com.google.tracing.TraceContext$TraceContextRunnable.run(TraceContext.java:453)
at com.google.apphosting.runtime.ThreadGroupPool$PoolEntry.run(ThreadGroupPool.java:162)
at java.lang.Thread.run(Thread.java:679)
which does not give me an idea why the injection in
@Configuredclasses is not working.spring-projects-issues commentedon Nov 30, 2011
Chris Beams commented
Marcel,
Do you get the same behavior using
<context:spring-configured/>instead of@EnableSpringConfigured? What if you don't use either?Also, you mention
@Configured- there is no such annotation in Spring. Do you mean@Configurable, or@Configuration? Keep in mind that@EnableSpringConfiguredenables dependency injection for@Configurableclasses not otherwise managed by the Spring container, e.g. domain objects/entities, etc.If this continues to be an issue for you, please create a new issue for it and reference this one.
Thanks,
Chris