Announcement Announcement Module
Collapse
No announcement yet.
Spring 3.2 @Configurable and @Autowire fails to inject anything Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Spring 3.2 @Configurable and @Autowire fails to inject anything

    http://stackoverflow.com/questions/2...nject-anything

    I want to inject a SecureRandom instance into an @Entity class, but when I check the value while running the debugger version of Tomcat, I only see a null value there. I don't get any error messages.

    I have placed the spring-instrument-tomcat-3.2.5.RELEASE.jar into C:\apache-tomcat-7.0.40\lib and it seems to be picked up fine.

    ## Gradle.build file
    Just the dependencies

    dependencies {
    def springVersion = '3.2.5.RELEASE'
    def springSecurityVersion = '3.2.0.RELEASE'
    def jacksonVersion = '2.3.0'
    def hibernateVersion = '4.3.0.Final'
    def tomcatVersion = '7.0.11'

    compile group: 'com.jayway.restassured', name: 'rest-assured', version: '2.1.0'
    testCompile group: 'junit', name: 'junit', version: '4.11'
    providedCompile group: 'javax.servlet', name: 'javax.servlet-api', version: '3.0.1'
    testCompile group: 'org.mockito', name: 'mockito-core', version: '1.9.5'
    testCompile group: 'org.springframework', name:'spring-test', version: springVersion
    compile group: 'javax.mail', name:'mail', version:'1.4.1'
    compile group: 'org.springframework', name: 'spring-context-support', version: springVersion //This is used for sending emails
    compile group: 'org.springframework', name: 'spring-webmvc', version: springVersion
    compile group: 'org.springframework', name: 'spring-orm', version: springVersion
    compile group: 'org.springframework', name: 'spring-aspects', version: springVersion
    compile group: 'org.aspectj', name: 'aspectjrt', version: '1.7.4'
    compile group: 'org.aspectj', name: 'aspectjweaver', version: '1.7.4'
    compile group: 'org.springframework', name: 'spring-aop', version: springVersion
    compile group: 'org.springframework.security', name: 'spring-security-web', version: springSecurityVersion
    compile group: 'org.springframework.security', name: 'spring-security-core', version: springSecurityVersion
    compile group: 'org.springframework.security', name: 'spring-security-config', version: springSecurityVersion
    compile group: 'org.hibernate', name: 'hibernate-core', version: hibernateVersion
    compile group: 'org.hibernate', name: 'hibernate-entitymanager', version: hibernateVersion
    compile group: 'commons-dbcp', name: 'commons-dbcp', version: '1.4'
    compile group: 'log4j', name: 'log4j', version: '1.2.17'
    runtime group: 'com.fasterxml.jackson.core', name: 'jackson-core', version: jacksonVersion
    runtime group: 'com.fasterxml.jackson.core', name: 'jackson-databind', version: jacksonVersion
    runtime group: 'mysql', name:'mysql-connector-java', version: '5.1.26'

    ##Entity class
    I removed some extras to make this example clearer.

    @Entity
    @Configurable(preConstruction = true, autowire = Autowire.BY_TYPE)
    public class Invitation {

    @Id
    @GeneratedValue
    private int id;

    @Transient
    @Autowired
    private SecureRandom random;

    public Invitation(User owner, String inviteeEmail, String accessCode) {
    if(accessCode == null) {
    this.accessCode = generateAlphanumericAccessCode();
    } else {
    this.accessCode = accessCode;
    }
    this.owner = owner;
    this.inviteeEmail = inviteeEmail;
    }

    private String generateAlphanumericAccessCode() {
    return new BigInteger(130, random).toString(32);
    }

    }

    ##Java Configuration Class
    Again, removed extras to make this clearer.

    @Configuration
    @EnableWebMvc
    @ComponentScan(basePackageClasses = RootContextConfig.class)
    @EnableTransactionManagement
    @EnableWebSecurity
    @EnableAsync
    @EnableSpringConfigured
    @EnableLoadTimeWeaving(aspectjWeaving = EnableLoadTimeWeaving.AspectJWeaving.ENABLED)
    public class RootContextConfig extends WebMvcConfigurerAdapter {

    @Bean
    public SecureRandom secureRandom() {
    return new SecureRandom();
    }
    }

    ##Context.xml (located in webapp/META-INF)
    My webapp deploys to the root context in production and dev.

    <?xml version="1.0" encoding="UTF-8"?>
    <Context>
    <Loader loaderClass="org.springframework.instrument.classl oading.tomcat.TomcatInstrumentableClassLoader"/>
    </Context>

  • #2
    I am having more or less same problem.

    Exception in thread "main" org.springframework.beans.factory.BeanCreationExce ption: Error creating bean with name 'loadTimeWeaver': Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationExcepti on: Could not instantiate bean class [org.springframework.instrument.classloading.websph ere.WebSphereLoadTimeWeaver]: Constructor threw exception; nested exception is java.lang.IllegalStateException: Could not initialize WebSphere LoadTimeWeaver because WebSphere 7 API classes are not available
    at org.springframework.beans.factory.support.Abstract AutowireCapableBeanFactory.instantiateBean(Abstrac tAutowireCapableBeanFactory.java:965)
    at org.springframework.beans.factory.support.Abstract AutowireCapableBeanFactory.createBeanInstance(Abst ractAutowireCapableBeanFactory.java:911)
    at org.springframework.beans.factory.support.Abstract AutowireCapableBeanFactory.doCreateBean(AbstractAu towireCapableBeanFactory.java:485)
    at org.springframework.beans.factory.support.Abstract AutowireCapableBeanFactory.createBean(AbstractAuto wireCapableBeanFactory.java:456)
    at org.springframework.beans.factory.support.Abstract BeanFactory$1.getObject(AbstractBeanFactory.java:2 91)
    at org.springframework.beans.factory.support.DefaultS ingletonBeanRegistry.getSingleton(DefaultSingleton BeanRegistry.java:222)
    at org.springframework.beans.factory.support.Abstract BeanFactory.doGetBean(AbstractBeanFactory.java:288 )
    at org.springframework.beans.factory.support.Abstract BeanFactory.getBean(AbstractBeanFactory.java:190)
    at org.springframework.beans.factory.support.DefaultL istableBeanFactory.preInstantiateSingletons(Defaul tListableBeanFactory.java:580)
    at org.springframework.context.support.AbstractApplic ationContext.finishBeanFactoryInitialization(Abstr actApplicationContext.java:895)
    at org.springframework.context.support.AbstractApplic ationContext.refresh(AbstractApplicationContext.ja va:425)
    at org.springframework.context.support.FileSystemXmlA pplicationContext.<init>(FileSystemXmlApplicationC ontext.java:140)
    at org.springframework.context.support.FileSystemXmlA pplicationContext.<init>(FileSystemXmlApplicationC ontext.java:84)
    at com.hrg.capeengine.test.AutowireTest.main(Autowire Test.java:12)
    Caused by: org.springframework.beans.BeanInstantiationExcepti on: Could not instantiate bean class [org.springframework.instrument.classloading.websph ere.WebSphereLoadTimeWeaver]: Constructor threw exception; nested exception is java.lang.IllegalStateException: Could not initialize WebSphere LoadTimeWeaver because WebSphere 7 API classes are not available
    at org.springframework.beans.BeanUtils.instantiateCla ss(BeanUtils.java:141)
    at org.springframework.beans.factory.support.SimpleIn stantiationStrategy.instantiate(SimpleInstantiatio nStrategy.java:74)
    at org.springframework.beans.factory.support.Abstract AutowireCapableBeanFactory.instantiateBean(Abstrac tAutowireCapableBeanFactory.java:958)
    ... 13 more
    Caused by: java.lang.IllegalStateException: Could not initialize WebSphere LoadTimeWeaver because WebSphere 7 API classes are not available
    at org.springframework.instrument.classloading.websph ere.WebSphereClassLoaderAdapter.<init>(WebSphereCl assLoaderAdapter.java:63)
    at org.springframework.instrument.classloading.websph ere.WebSphereLoadTimeWeaver.<init>(WebSphereLoadTi meWeaver.java:54)
    at org.springframework.instrument.classloading.websph ere.WebSphereLoadTimeWeaver.<init>(WebSphereLoadTi meWeaver.java:43)
    at sun.reflect.NativeConstructorAccessorImpl.newInsta nce0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInsta nce(NativeConstructorAccessorImpl.java:56)
    at sun.reflect.DelegatingConstructorAccessorImpl.newI nstance(DelegatingConstructorAccessorImpl.java:39)
    at java.lang.reflect.Constructor.newInstance(Construc tor.java:527)
    at org.springframework.beans.BeanUtils.instantiateCla ss(BeanUtils.java:126)
    ... 15 more
    Caused by: java.lang.NoSuchMethodException: sun.misc.Launcher$AppClassLoader.<init>(com.ibm.ws .classloader.CompoundClassLoader)
    at java.lang.Class.throwNoSuchMethodException(Class.j ava:286)
    at java.lang.Class.getDeclaredConstructor(Class.java: 451)
    at org.springframework.instrument.classloading.websph ere.WebSphereClassLoaderAdapter.<init>(WebSphereCl assLoaderAdapter.java:54)
    ... 22 more



    <context:load-time-weaver weaver-class="org.springframework.instrument.classloading .websphere.WebSphereLoadTimeWeaver"/>
    <context:spring-configured/>
    <bean class="com.project.helper.MyTest"></bean>


    using the below jars

    spring-context-3.1.0.M2.jar
    spring-context-3.0.5.release.jar
    spring-context-support-3.0.5.release.jar

    Comment


    • #3
      You are mixing Spring jar versions. Remove the spring-context-3.1.0.M2 jar and try again.

      Also, please note that we are currently in the process of moving to stackoverflow.com for our forums. If you're still having this issue please consider asking it again at http://stackoverflow.com using the #spring tag. If you do post it there, please reply here with the link. Thanks!

      Comment


      • #4
        I ended up fixing it because I was missing an aspect j xml file.

        Comment

        Working...
        X