我想将一个SecureRandom实例注入到@Entity类中,但是当我在运行Tomcat的调试版时检查该值时,我只看到一个空值。我没有收到任何错误消息。
我已将spring-instrument-tomcat-3.2.5.RELEASE.jar放入C:\ apache-tomcat-7.0.40 \ lib中,它看起来很好。
只是依赖
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);
}
}
再次,删除额外内容以使其更清晰。
@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();
}
}
我的webapp部署到生产和开发中的根上下文。
<?xml version="1.0" encoding="UTF-8"?>
<Context>
<Loader loaderClass="org.springframework.instrument.classloading.tomcat.TomcatInstrumentableClassLoader"/>
</Context>
答案 0 :(得分:0)
原来我需要一个aop.xml,它需要位于src / main / resources / META-INF / aop.xml中。我认为它首先从WEB-INF / classes / META-INF / aop.xml中获取aop.xml。我认为次要的是来自春天的依赖。
<!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>