Spring Mixing XML and JavaConfig
By:Roy.LiuLast updated:2019-08-17
Spring examples to show you how to mix both Spring XML and JavaConfig together.
1. Load JavaConfig From Spring XML
A Spring MVC example, uses @Configuration to load everything, and you want integrate with web.xml
SpringWebConfig.java
package com.mkyong.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.mkyong.form.web", "com.mkyong.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
<web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5"> <display-name>Spring3 MVC Application</display-name> <servlet> <servlet-name>spring-web</servlet-name> <servlet-class> org.springframework.web.servlet.DispatcherServlet </servlet-class> <load-on-startup>1</load-on-startup> <init-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/spring-web-servlet-config.xml</param-value> </init-param> </servlet> <servlet-mapping> <servlet-name>spring-web</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> </web-app>
In the Spring XML file, just scan the Java @Configuration.
spring-web-servlet-config.xml
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <!-- Scan the JavaConfig --> <context:component-scan base-package="com.mkyong.form.config" /> </beans>
Done.
2. Load Spring XML From JavaConfig
This is more straightforward, just use the @ImportResource annotation.
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
<bean id="dataSource" class="org.apache.commons.dbcp2.BasicDataSource" destroy-method="close"> <property name="driverClassName" value="org.hsqldb.jdbcDriver" /> <property name="url" value="jdbc:hsqldb:mem:dataSource" /> <property name="username" value="sa" /> <property name="password" value="" /> </bean>
@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);
References
From:一号门
COMMENTS