SpringのXMLとJavaConfigを混在させる方法を示すSpringの例
1. Spring XMLからJavaConfigをロードする
Spring MVCの例では、
@Configuration`を使用してすべてをロードし、
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>
SpringのXMLファイルでは、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>
完了しました。
2. JavaConfigからSpring XMLをロードする
これはより簡単ですが、 `@ ImportResource`アノテーションを使用してください。
2.1 `spring-web-servlet.xml`ファイルを読み込みます。
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 XMLファイルからdataSource Beanをロードします。
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複数のSpring XMLファイルを読み込む
@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);
}
参考文献
設定スタイル]