Spring 3.2 @Configurable และ @Autowire ล้มเหลวในการฉีดสิ่งใด ๆ

ฉันต้องการแทรกอินสแตนซ์ SecureRandom ลงในคลาส @Entity แต่เมื่อฉันตรวจสอบค่าขณะเรียกใช้ Tomcat เวอร์ชันดีบักเกอร์ ฉันเห็นเฉพาะค่า Null ที่นั่นเท่านั้น ฉันไม่ได้รับข้อความแสดงข้อผิดพลาดใดๆ

ฉันได้วาง 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>

person Jazzepi    schedule 16.02.2014    source แหล่งที่มา


คำตอบ (1)


ปรากฎว่าฉันต้องการ aop.xml และจำเป็นต้องอยู่ใน src/main/resources/META-INF/aop.xml ฉันคิดว่ามันดึง aop.xml จาก WEB-INF/classes/META-INF/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>
person Jazzepi    schedule 23.02.2014