Skip to content

Introduce @EnableSpringConfigured [SPR-7888] #12544

Closed

Description

@spring-projects-issues

Chris Beams opened SPR-7888 and commented

Equivalent to context:spring-configured/

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());
}

// ...}

Issue Links:

Referenced from: commits 22e37aa, d356205, 4318ccd

1 votes, 2 watchers

Activity

  1. spring-projects-issues commented on Jul 20, 2011

    @spring-projects-issues
    CollaboratorAuthor

    Sam Brannen commented

    See comments from the Spring 3.1 M2: Testing with @Configuration Classes and Profiles blog post for a related discussion.

  2. spring-projects-issues commented on Jul 20, 2011

    @spring-projects-issues
    CollaboratorAuthor

    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 @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 />?

  3. spring-projects-issues commented on Jul 20, 2011

    @spring-projects-issues
    CollaboratorAuthor

    Chris Beams commented

    Slating this for 3.1 RC1, and will likely take the form of an @Enable annotation.

  4. spring-projects-issues commented on Jul 20, 2011

    @spring-projects-issues
    CollaboratorAuthor

    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 @Configuration class.

  5. spring-projects-issues commented on Jul 24, 2011

    @spring-projects-issues
    CollaboratorAuthor

    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 context and aop XML namespaces by defining a bean as follows:

    <bean class="org.springframework.beans.factory.aspectj.AnnotationBeanConfigurerAspect"
        factory-method="aspectOf"/>

    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);
    
    	}
    }
    

    Let us know if that works for you.

  6. spring-projects-issues commented on Aug 1, 2011

    @spring-projects-issues
    CollaboratorAuthor

    Frank Scheffler commented

    Works perfect.

  7. spring-projects-issues commented on Aug 3, 2011

    @spring-projects-issues
    CollaboratorAuthor

    Frank Scheffler commented

    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?

  8. spring-projects-issues commented on Aug 3, 2011

    @spring-projects-issues
    CollaboratorAuthor

    Chris Beams commented

    Hi Frank,

    Yes, we will introduce an @Enable annotation to this effect. This issue tracks that effort -- expect it for RC1.

    Chris

  9. spring-projects-issues commented on Aug 4, 2011

    @spring-projects-issues
    CollaboratorAuthor

    Marcel Overdijk commented

    Hi Chris,
    I know I shouldn't ask it here, but any idea when we can expect RC1?

  10. spring-projects-issues commented on Aug 5, 2011

    @spring-projects-issues
    CollaboratorAuthor

    Chris Beams commented

    Hi Marcel, No exact date at the moment, but generally you can expect RC1 within the next month.

  11. spring-projects-issues commented on Oct 11, 2011

    @spring-projects-issues
    CollaboratorAuthor

    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.

  12. spring-projects-issues commented on Nov 27, 2011

    @spring-projects-issues
    CollaboratorAuthor

    Chris Beams commented

    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.
    
  13. spring-projects-issues commented on Nov 28, 2011

    @spring-projects-issues
    CollaboratorAuthor

    Marcel Overdijk commented

    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:

    1. Is @EnableSpringConfigured doing something sepcial that might break on GAE
    2. 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.
  14. spring-projects-issues commented on Nov 30, 2011

    @spring-projects-issues
    CollaboratorAuthor

    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 @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.

    Thanks,

    Chris

  15. added
    type: enhancement
    in: core
    on Jan 11, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    in: coretype: enhancement

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions