繁体   English   中英

Spring 3.2 @Configurable和@Autowire无法注入任何东西

[英]Spring 3.2 @Configurable and @Autowire fails to inject anything

我想将SecureRandom实例注入@Entity类,但是当我在运行Tomcat的调试器版本时检查该值时,我仅在其中看到一个空值。 我没有收到任何错误消息。

我已经将spring-instrument-tomcat-3.2.5.RELEASE.jar放到C:\\ apache-tomcat-7.0.40 \\ lib中,似乎可以正常使用了。

Gradle.build文件

只是依赖

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
@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
@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(位于webapp / META-INF中)

我的webapp部署到生产和开发中的根上下文。

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

原来,我需要一个aop.xml,它需要位于src / main / resources / META-INF / aop.xml中。 我认为它首先是从WEB-INF / classes / META-INF / aop.xml中选择aop.xml。 我认为第二个源于spring-aop依赖。

<!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>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM