Spring - Mélanger XML et JavaConfig

Spring - Mélanger XML et JavaConfig

Exemples Spring pour vous montrer comment mélanger Spring XML et JavaConfig ensemble.

1. Charger JavaConfig depuis Spring XML

Un exemple Spring MVC, utilise@Configuration pour tout charger, et vous voulez intégrer avecweb.xml

SpringWebConfig.java

package com.example.form.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import org.springframework.web.servlet.view.InternalResourceViewResolver;
import org.springframework.web.servlet.view.JstlView;

@EnableWebMvc
@Configuration
@ComponentScan({ "com.example.form.web", "com.example.form.core" })
public class SpringWebConfig extends WebMvcConfigurerAdapter {

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/resources/**")
                        .addResourceLocations("/resources/");
    }

    @Bean
    public InternalResourceViewResolver viewResolver() {
        InternalResourceViewResolver viewResolver =
                        new InternalResourceViewResolver();
        viewResolver.setViewClass(JstlView.class);
        viewResolver.setPrefix("/WEB-INF/views/jsp/");
        viewResolver.setSuffix(".jsp");
        return viewResolver;
    }

}

web.xml



    Spring3 MVC Application

    
        spring-web
        
                       org.springframework.web.servlet.DispatcherServlet
                
        1
        
            contextConfigLocation
            /WEB-INF/spring-web-servlet-config.xml
        
    

    
        spring-web
        /
    

Dans le fichier XML Spring, scannez simplement les@Configuration Java.

spring-web-servlet-config.xml



        
    

Terminé.

2. Charger Spring XML à partir de JavaConfig

C'est plus simple, utilisez simplement l'annotation@ImportResource.

2.1 Loads a spring-web-servlet.xml file.

import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.ImportResource;
import org.springframework.context.annotation.Import;

@Configuration
@Import({ AppConfigOthers.class }) //loads another JavaConfig
@ImportResource("classpath:/config/spring-web-servlet.xml")
public class AppConfigCore {
    //...
}

2.2 Loads a dataSource bean from XML file.

database-config.xml

    
        
        
        
        
    
@Configuration
@Import({ AppConfigOthers.class })
@ImportResource("classpath:/config/database-config.xml")
public class AppConfigCore {

    @Autowired
    DataSource dataSource;

    @Bean
    public JdbcTemplate getJdbcTemplate() {
        return new JdbcTemplate(dataSource);
    }

2.3 Loads multiple Spring XML files

@Configuration
@Import({ AppConfigOthers.class })
@ImportResource({
     "classpath:/config/spring-web-servlet.xml",
     "classpath:/config/database-config.xml"
})
public class AppConfigCore {

    @Autowired
    DataSource dataSource;

    @Bean
    public JdbcTemplate getJdbcTemplate() {
        return new JdbcTemplate(dataSource);
    }