Я хочу внедрить экземпляр 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)
Мое веб-приложение развертывается в корневом контексте в производстве и разработке.
<?xml version="1.0" encoding="UTF-8"?>
<Context>
<Loader loaderClass="org.springframework.instrument.classloading.tomcat.TomcatInstrumentableClassLoader"/>
</Context>