Announcement Announcement Module
Collapse

JavaConfig forum decommissioned in favor of Core Container

As described at

http://static.springsource.org/sprin...fig/README.TXT

key features of the Spring JavaConfig project have been migrated into the core Spring Framework as of version 3.0.

Please see the Spring 3.0 documentation on @Configuration and @Bean support:

http://static.springsource.org/sprin...tml#beans-java

For any questions related to @Configuration classes and @Bean methods in Spring 3.0, please post in the dedicated 'Core Container' forum at

http://forum.springsource.org/forumdisplay.php?f=26
See more
See less
Exception while loading JavaConfigApplicationContext Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Exception while loading JavaConfigApplicationContext

    Hello,
    I am just trying the sample code to get the feel of JavaConfig. I am facing the exception while loading the JavaConfigApplicationContext. Following the stack trace for reference.
    Code:
    13-Jul-2009 00:11:47 org.springframework.context.support.AbstractApplicationContext prepareRefresh
    INFO: Refreshing org.springframework.config.java.context.JavaConfigApplicationContext@3ef810: display name [org.springframework.config.java.context.JavaConfigApplicationContext@3ef810]; startup date [Mon Jul 13 00:11:47 BST 2009]; root of context hierarchy
    13-Jul-2009 00:11:47 org.springframework.context.support.AbstractApplicationContext obtainFreshBeanFactory
    INFO: Bean factory for application context [org.springframework.config.java.context.JavaConfigApplicationContext@3ef810]: org.springframework.beans.factory.support.DefaultListableBeanFactory@3a5794
    Exception in thread "main" java.lang.annotation.AnnotationFormatError: Invalid default: public abstract org.springframework.beans.factory.annotation.Autowire org.springframework.config.java.annotation.Configuration.defaultAutowire()
    	at java.lang.reflect.Method.getDefaultValue(Method.java:728)
    	at sun.reflect.annotation.AnnotationType.<init>(AnnotationType.java:99)
    	at sun.reflect.annotation.AnnotationType.getInstance(AnnotationType.java:66)
    	at sun.reflect.annotation.AnnotationParser.parseAnnotation(AnnotationParser.java:202)
    	at sun.reflect.annotation.AnnotationParser.parseAnnotations2(AnnotationParser.java:69)
    	at sun.reflect.annotation.AnnotationParser.parseAnnotations(AnnotationParser.java:52)
    	at java.lang.Class.initAnnotationsIfNecessary(Class.java:3072)
    	at java.lang.Class.getAnnotation(Class.java:3029)
    	at org.springframework.config.java.internal.util.AnnotationExtractionUtils.extractClassAnnotation(AnnotationExtractionUtils.java:41)
    	at org.springframework.config.java.internal.model.ConfigurationClass.<clinit>(ConfigurationClass.java:65)
    	at org.springframework.config.java.internal.parsing.asm.AsmConfigurationParser.parse(AsmConfigurationParser.java:72)
    	at org.springframework.config.java.internal.factory.support.AsmJavaConfigBeanDefinitionReader.loadBeanDefinitions(AsmJavaConfigBeanDefinitionReader.java:70)
    	at org.springframework.config.java.internal.process.InternalConfigurationPostProcessor.parseAnyConfigurationClasses(InternalConfigurationPostProcessor.java:128)
    	at org.springframework.config.java.internal.process.InternalConfigurationPostProcessor.postProcessBeanFactory(InternalConfigurationPostProcessor.java:76)
    	at org.springframework.config.java.context.JavaConfigApplicationContext.invokeBeanFactoryPostProcessors(JavaConfigApplicationContext.java:121)
    	at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:359)
    	at org.springframework.config.java.context.JavaConfigApplicationContext.<init>(JavaConfigApplicationContext.java:80)
    	at com.my.Test.main(Test.java:8)
    The code is as follows
    Code:
    package com.my;
    
    import org.springframework.config.java.context.JavaConfigApplicationContext;
    
    public class Test {
    	
    	public static void main(String args[]){
    		JavaConfigApplicationContext ctx = new JavaConfigApplicationContext(AppConf.class);
    		Employee emp = ctx.getBean(Employee.class);
    	}
    }
    
    
    
    package com.my;
    
    import org.springframework.config.java.annotation.Bean;
    import org.springframework.config.java.annotation.Configuration;
    
    @Configuration
    public class AppConf {
    	
    	@Bean
    	public Employee employee(){
    		return new Employee();
    	}
    }
    
    
    package com.my;
    
    public class Employee {
    	
    	private int id;
    	private String name;
    	public int getId() {
    		return id;
    	}
    	public void setId(int id) {
    		this.id = id;
    	}
    	public String getName() {
    		return name;
    	}
    	public void setName(String name) {
    		this.name = name;
    	}
    }
    Am I missing something? Any help appreciated.
    Cheers,
    Rahul

  • #2
    At a glance, this looks like a version incompatibility issue. Please reply with a) what version of JavaConfig you are using and b) what version of Spring you are using.

    Comment


    • #3
      Hi Chris,
      I am using spring-javaconfig-1.0.0.M4 and Spring-framework-3.0.0.M3

      Thanks and Regards,
      Rahul

      Comment


      • #4
        Exception while loading JavaConfigApplicationContext - Anybody to resove this issue?

        Hi,

        Still facing the problem. Is it the version issue?

        Cheers,
        Rahul

        Comment


        • #5
          Did you find a work around?

          I get the exact same exception with Spring 3.0.0.M3 and java-config 1.0.0.M4 while it works just fine if I switch to Spring 2.5.6.

          It indeed appears to be a version problem as the default Autowire value defined in the @Configuration interface (Autowire.INHERITED) doesn't exist in Spring-beans 3.0.0.M3 org.springframework.beans.factory.annotation.Autow ire (this enum only has 3 values : NO, BY_TYPE and BY_NAME)


          For the records here are the relevant parts of my application

          My configuration class
          Code:
          import org.springframework.beans.factory.annotation.Autowire;
          import org.springframework.config.java.annotation.Configuration;
          import org.springframework.config.java.annotation.Lazy;
          import org.springframework.config.java.annotation.valuesource.PropertiesValueSource;
          import org.springframework.config.java.plugin.aop.AspectJAutoProxy;
          import org.springframework.config.java.plugin.context.AnnotationDrivenConfig;
          import org.springframework.config.java.plugin.tx.AnnotationDrivenTx;
          import org.springframework.config.java.support.ConfigurationSupport;
          
          @Configuration(defaultLazy = Lazy.FALSE, defaultAutowire = Autowire.BY_TYPE, checkRequired = true)
          @AspectJAutoProxy
          @AnnotationDrivenTx
          @AnnotationDrivenConfig
          @PropertiesValueSource(locations = { "classpath:database.properties" })
          public class DatabaseConfig extends ConfigurationSupport
          {
          ...
          My unit test
          Code:
          import org.junit.runner.RunWith;
          import org.springframework.config.java.test.JavaConfigContextLoader;
          import org.springframework.test.context.ContextConfiguration;
          import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
          
          
          @RunWith(SpringJUnit4ClassRunner.class)
          @ContextConfiguration(locations= 
          { 
          	"com.app.DatabaseConfig"
          },
                   loader=JavaConfigContextLoader.class)
          public class JavaConfigTest
          {
          my pom
          Code:
          <properties>
          	<spring.version>3.0.0.M3</spring.version>
          	<spring-security.version>3.0.0.M1</spring-security.version>
          	<junit.version>4.5</junit.version>
          </properties>
          
          ...
          
          <dependency>
          	<groupId>org.springframework</groupId>
          	<artifactId>spring-web</artifactId>
          	<version>${spring.version}</version>
          </dependency>
          <dependency>
          	<groupId>org.springframework</groupId>
          	<artifactId>spring-tx</artifactId>
          	<version>${spring.version}</version>
          </dependency>
          <dependency>
          	<groupId>org.springframework</groupId>
          	<artifactId>spring-aop</artifactId>
          	<version>${spring.version}</version>
          </dependency>
          <dependency>
          	<groupId>org.springframework</groupId>
          	<artifactId>spring-orm</artifactId>
          	<version>${spring.version}</version>
          </dependency>
          <dependency>
          	<groupId>org.springframework</groupId>
          	<artifactId>spring-jdbc</artifactId>
          	<version>${spring.version}</version>
          </dependency>
          <dependency>
          	<groupId>org.springframework.integration</groupId>
          	<artifactId>spring-integration-mail</artifactId>
          	<version>1.0.2.SR1</version>
          </dependency>
          <dependency>
          	<groupId>org.springframework.javaconfig</groupId>
          	<artifactId>spring-javaconfig</artifactId>
          	<version>1.0.0.M4</version>
          </dependency>
          Last edited by skal111; Jul 20th, 2009, 11:27 AM.

          Comment


          • #6
            Spring 3.0.0.M3 comes with an extract of JavaConfig

            Comment


            • #7
              Hello all,

              As Juergen mentions above, Spring 3.0.0.M3 now contains most of the functionality provided by the standalone JavaConfig project: support for @Configuration, @Bean, etc.

              Certain classes are not (yet) supported in Spring Core, such as JavaConfigApplicationContext.

              Keep an eye on the issue below to be notified when JavaConfig is updated to be compatible with Spring 3.0.

              http://jira.springframework.org/browse/SJC-231

              Comment


              • #8
                Indeed it works perfectly with 3.0.0.M3 config features.

                Thanks !

                Comment


                • #9
                  JavaConfigApplicationContext

                  Is the java config functionality in spring 3.0.0 dependent on other open source projects like cglib and objectweb/asm?

                  Can someone point me to usage of javaconfig through spring 3.0.0 framework directly without any other dependencies?

                  Appreciate the help

                  Comment


                  • #10
                    Processing @Configuration classes is dependent on CGLIB, yes. ASM is also used, but we repackage ASM within Spring as org.springframework.asm.

                    If you want to avoid the need for CGLIB, do not annotate your classes with @Configuration. For example:

                    CGLIB will be required for the following:
                    Code:
                    @Configuration
                    public class AppConfig {
                        @Bean
                        public Foo foo() { return new Foo(); }
                    }
                    ...
                    new AnnotationConfigApplicationContext(AppConfig.class);
                    Processing @Configuration classes is dependent on CGLIB, yes. ASM is also used, but we repackage ASM within Spring as org.springframework.asm.

                    If you want to avoid the need for CGLIB, do not annotate your classes with @Configuration. For example:

                    CGLIB will be *NOT* required for the following:
                    Code:
                    public class AppConfig {
                        @Bean
                        public Foo foo() { return new Foo(); }
                    }
                    ...
                    new AnnotationConfigApplicationContext(AppConfig.class);
                    If CGLIB is not used, then scoping semantics for @Bean methods cannot be managed by the framework. In the case above, it means that all calls to foo() will essentially be prototype-scoped. If this works for you, go for it.

                    Comment


                    • #11
                      That works!

                      Thanks for the quick response and the heads up on the scope limitation. Since my current requirement is for prototype scoped that should work in the short term.

                      Comment


                      • #12
                        Sorry. Got into a new thread by mistake with my previous post

                        Thanks for the quick response and the heads up on the scope limitation. Since my current requirement is for prototype scoped that should work in the short term.

                        Comment

                        Working...
                        X