我对春天不太熟悉,所以添加了与春天相关的一切。
我有一个spring 4 struts 1.x应用程序。
使用struts插件加载Spring上下文,该插件指向spring-context.xml
<plug-in className="org.springframework.web.struts.ContextLoaderPlugIn">
<set-property property="contextConfigLocation"
value="/WEB-INF/spring-context.xml"/>
</plug-in>
在spring-context.xml
我已按照以下方式配置了Jpa的EntityManager
:
<context:component-scan base-package="com.epam.testsystem" />
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalEntityManagerFactoryBean">
<property name="persistenceUnitName" value="primary"/>
</bean>
<!-- Transactions -->
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory" />
</bean>
我还使用
集成了Spring Securitypublic class SecurityWebApplicationInitializer
extends AbstractSecurityWebApplicationInitializer {
public SecurityWebApplicationInitializer() {
super(SecurityConfig.class);
}
}
和
@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(securedEnabled = true)
@ImportResource("/WEB-INF/spring-context.xml")
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Autowired
private UserDetailsService userDetailsService;
@Override
public void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.userDetailsService(userDetailsService);
}
@Override
public void configure(HttpSecurity http) throws Exception {
http
.authorizeRequests()
.antMatchers("/welcome.do").hasRole("ADMIN")
.and()
.formLogin()
.loginPage("/login.do")
.loginProcessingUrl("/login")
.failureUrl("/login?error=1")
.permitAll()
.and()
.csrf();
}
@Bean
@Override
public AuthenticationManager authenticationManagerBean() throws Exception {
return super.authenticationManagerBean();
}
}
它的工作没有错误。
现在我决定将EntityManager
的配置移到java Config。所以我从spring-context.xml
中删除了bean定义,将这些bean添加到SecurityConfig
@Bean
public LocalContainerEntityManagerFactoryBean entityManagerFactory() {
LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean();
em.setPersistenceUnitName("primary");
return em;
}
@Bean
public PlatformTransactionManager transactionManager(EntityManagerFactory emf){
JpaTransactionManager transactionManager = new JpaTransactionManager();
transactionManager.setEntityManagerFactory(emf);
return transactionManager;
}
但现在我得到了这个例外:
引起:org.springframework.beans.factory.BeanCurrentlyInCreationException:创建名为'entityManagerFactory'的bean时出错:请求的bean当前正在创建:是否存在无法解析的循环引用?“
我认为发生这种情况是因为我在UserDaoImpl
中使用了@PersistenceContext注释,然后才将其导入Java Config。
那么处理这个问题最干净的方法是什么?
由于
答案 0 :(得分:0)
建议的解决方案之一是
@Bean
public LocalContainerEntityManagerFactoryBean entityManagerFactory() {
LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean();
em.setPersistenceUnitName("primary");
return em;
}
@Bean
public PlatformTransactionManager transactionManager(){
JpaTransactionManager transactionManager = new JpaTransactionManager();
// SOlution below
transactionManager.setEntityManagerFactory(entityManagerFactory());
return transactionManager;
}
这里我们通过entityManagerFactory()传递依赖bean 如果这符合解决方案,请告诉我
答案 1 :(得分:0)
我通过创建单独的PersistenceConfig
@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(basePackages="com.epam.testsystem.repository")
public class PersistenceConfig {
@Bean
public LocalContainerEntityManagerFactoryBean entityManagerFactory() {
LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean();
JpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
em.setJpaVendorAdapter(vendorAdapter);
em.setPersistenceUnitName("primary");
return em;
}
@Bean
public PlatformTransactionManager transactionManager(EntityManagerFactory emf){
JpaTransactionManager transactionManager = new JpaTransactionManager();
transactionManager.setEntityManagerFactory(emf);
return transactionManager;
}
}
并将Java Config导入xml config(spring-context.xml):
<bean class="com.epam.testsystem.config.PersistenceConfig" />