spring 4 struts 1.x mixing xml and java config

1k views Asked by At

I am not very familiar with spring, so added everything related to spring.

I have a spring 4 struts 1.x application.

Spring context is loaded using struts plugin which points to spring-context.xml

<plug-in className="org.springframework.web.struts.ContextLoaderPlugIn">
    <set-property property="contextConfigLocation"
    value="/WEB-INF/spring-context.xml"/>
</plug-in>

In spring-context.xml I have configured Jpa's EntityManager like so:

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

I also have Spring Security integrated using

public class SecurityWebApplicationInitializer
        extends AbstractSecurityWebApplicationInitializer {

    public SecurityWebApplicationInitializer() {
        super(SecurityConfig.class);
    }
}

and

@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();
    }
}

AND IT WORKS WITHOUT ERRORS.

Now I decided to move the configuration of EntityManager to java Config. So I deleted bean definitions from spring-context.xml added those beans to 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;
}

But now I get this Exception:

Caused by: org.springframework.beans.factory.BeanCurrentlyInCreationException: Error creating bean with name 'entityManagerFactory': Requested bean is currently in creation: Is there an unresolvable circular reference?"

I think that happened because I use @PersistenceContext annotation in UserDaoImpl, before I imported it into Java Config.

So what is the most clean way to handle this?

Thanks

2

There are 2 answers

0
Mudassar On

One of the proposed solution is

@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;
    }

Here we pass the dependent bean via entityManagerFactory() Kindly let me know if this fulfills the solution

0
almeynman On

I made it work by creating a separate 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;
    }
}

and import Java Config to xml config (spring-context.xml):

<bean class="com.epam.testsystem.config.PersistenceConfig" />