Page 1 of 2 12 LastLast
Results 1 to 10 of 12

Thread: Exception while loading JavaConfigApplicationContext

  1. #1
    Join Date
    Mar 2009
    Location
    UK
    Posts
    22

    Default 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. #2
    Join Date
    Apr 2007
    Posts
    307

    Default

    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.
    Chris Beams
    Spring Framework committer, VMware
    http://github.com/cbeams

  3. #3
    Join Date
    Mar 2009
    Location
    UK
    Posts
    22

    Default

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

    Thanks and Regards,
    Rahul

  4. #4
    Join Date
    Mar 2009
    Location
    UK
    Posts
    22

    Default Exception while loading JavaConfigApplicationContext - Anybody to resove this issue?

    Hi,

    Still facing the problem. Is it the version issue?

    Cheers,
    Rahul

  5. #5
    Join Date
    Jul 2009
    Posts
    8

    Default

    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 at 10:27 AM.

  6. #6

    Default

    Spring 3.0.0.M3 comes with an extract of JavaConfig

  7. #7
    Join Date
    Apr 2007
    Posts
    307

    Default

    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
    Chris Beams
    Spring Framework committer, VMware
    http://github.com/cbeams

  8. #8
    Join Date
    Jul 2009
    Posts
    8

    Default

    Indeed it works perfectly with 3.0.0.M3 config features.

    Thanks !

  9. #9
    Join Date
    Feb 2010
    Posts
    3

    Default 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

  10. #10
    Join Date
    Apr 2007
    Posts
    307

    Default

    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.
    Chris Beams
    Spring Framework committer, VMware
    http://github.com/cbeams

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •