Frage

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.classloading.tomcat.TomcatInstrumentableClassLoader"/>
</Context>
War es hilfreich?

Lösung

Turns out I needed an aop.xml, and it needed to be located in src/main/resources/META-INF/aop.xml. I think it was picking up the aop.xml from WEB-INF/classes/META-INF/aop.xml first. I think the secondary one was coming from the spring-aop dependency.

<!DOCTYPE aspectj PUBLIC
        "-//AspectJ//DTD//EN" "http://www.eclipse.org/aspectj/dtd/aspectj.dtd">
<aspectj>
    <weaver options="-verbose -Xset:weaveJavaPackages=true,weaveJavaxPackages=true">
        <include within="com.companyname.dirtylibs..*"/>
  </weaver>

</aspectj>
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top