子容器的配置:创建SpringMVCConfig类
父容器的配置:创建RootConfig、SpringDAOConfig、SpringServiceConfig
web.xml: 改为定义了一个WebConfig
SpringMVCConfig:
package com.wgc.persons.spring;import org.springframework.context.annotation.ComponentScan;import org.springframework.context.annotation.Configuration;import org.springframework.context.annotation.EnableAspectJAutoProxy;import org.springframework.web.servlet.config.annotation.DefaultServletHandlerConfigurer;import org.springframework.web.servlet.config.annotation.EnableWebMvc;import org.springframework.web.servlet.config.annotation.ViewResolverRegistry;import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;@Configuration //表示配置文件@EnableWebMvc //表示这是一个子容器@ComponentScan("com.wgc.persons.controller") //扫描controller层@EnableAspectJAutoProxy //扫描@Aspect注解public class SpringMVCConfig implements WebMvcConfigurer { @Override public void configureViewResolvers(ViewResolverRegistry registry) { registry.jsp("/WEB-IFN/jsp/", ".jsp"); } @Override public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) { configurer.enable(); }}
SpringDAOConfig:
package com.wgc.persons.spring.root;import com.mchange.v2.c3p0.ComboPooledDataSource;import org.mybatis.spring.SqlSessionFactoryBean;import org.mybatis.spring.mapper.MapperScannerConfigurer;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.beans.factory.annotation.Value;import org.springframework.context.EnvironmentAware;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;import org.springframework.core.env.Environment;import org.springframework.core.io.ClassPathResource;import org.springframework.core.io.support.PathMatchingResourcePatternResolver;import org.springframework.jdbc.datasource.DataSourceTransactionManager;import org.springframework.transaction.PlatformTransactionManager;import javax.sql.DataSource;import java.beans.PropertyVetoException;import java.io.IOException;@Configuration/*@ComponentScan(basePackages = "dynamicProxy.instanceJDBC7")*/ //测试AOP//@ComponentScan(basePackages = "transaction.instance1")//@EnableAspectJAutoProxy /*启用@Aspect类*///@EnableTransactionManagement/*启用事务*/public class SpringDAOConfig implements EnvironmentAware { @Value("${jdbc.driver}") private String driver; @Value("${jdbc.url}") private String url; @Value("${jdbc.user}") private String user; @Value("${jdbc.password}") private String password; @Autowired private Environment env; @Bean public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() { return new PropertySourcesPlaceholderConfigurer(); } @Bean public DataSource dataSource() throws PropertyVetoException { ComboPooledDataSource dataSource = new ComboPooledDataSource(); dataSource.setDriverClass(env.getProperty("jdbc.driver")); dataSource.setJdbcUrl(env.getProperty("jdbc.url")); dataSource.setUser("root"); dataSource.setPassword("123456"); return dataSource; } @Bean public SqlSessionFactoryBean sqlSessionFactoryBean() throws IOException, PropertyVetoException { SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean(); PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver(); factoryBean.setTypeAliasesPackage("com.wgc.persons.entity"); factoryBean.setMapperLocations(resolver.getResources("classpath:mapper/*.xml")); factoryBean.setConfigLocation(new ClassPathResource("mybatis-config.xml")); factoryBean.setDataSource(dataSource()); return factoryBean; } @Bean public MapperScannerConfigurer mapperScannerConfigurer() { MapperScannerConfigurer configurer = new MapperScannerConfigurer(); configurer.setBasePackage("com.wgc.persons.dao"); configurer.setSqlSessionFactoryBeanName("sqlSessionFactoryBean"); return configurer; } /*事务*/ @Bean public PlatformTransactionManager platformTransactionManager() throws PropertyVetoException { DataSourceTransactionManager manager = new DataSourceTransactionManager(); manager.setDataSource(dataSource()); return manager; } @Override public void setEnvironment(Environment environment) { this.env = environment; }}
SpringServiceConfig:
package com.wgc.persons.spring.root;import org.springframework.context.annotation.ComponentScan;import org.springframework.context.annotation.Configuration;@Configuration //配置文件@ComponentScan("com.wgc.persons.service") //扫描service层public class SpringServiceConfig {}
RootConfig:
package com.wgc.persons.spring.root;import org.springframework.context.annotation.ComponentScan;import org.springframework.context.annotation.Configuration;import org.springframework.context.annotation.Import;import org.springframework.context.annotation.PropertySource;@Configuration@ComponentScan(basePackages = "com.wgc.persons.spring.root")@PropertySource(value = "classpath:jdbc.properties") //获取resources包的下的jdbc连接方式@Import({SpringDAOConfig.class, SpringServiceConfig.class})public class RootConfig {}
WebConfig
package com.wgc.persons.spring.web;import com.wgc.persons.spring.SpringMVCConfig;import com.wgc.persons.spring.root.RootConfig;import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;public class WebConfig extends AbstractAnnotationConfigDispatcherServletInitializer { /*父容器*/ @Override protected Class [] getRootConfigClasses() { return new Class[]{RootConfig.class}; } /*子容器 */ @Override protected Class [] getServletConfigClasses() { return new Class[]{SpringMVCConfig.class}; } @Override protected String[] getServletMappings() { return new String[]{ "/" }; }}