博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Spring配置Java Style
阅读量:6657 次
发布时间:2019-06-25

本文共 6030 字,大约阅读时间需要 20 分钟。

子容器的配置:创建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[]{ "/" }; }}

 

 

转载于:https://www.cnblogs.com/YouAreABug/p/10128167.html

你可能感兴趣的文章
腾讯前端面试
查看>>
C++STL之algorithm(一)
查看>>
bzoj千题计划211:bzoj1996: [Hnoi2010]chorus 合唱队
查看>>
bzoj千题计划321:bzoj5251: [2018多省省队联测]劈配(网络流 + 二分)
查看>>
PHP通过串口发短信
查看>>
Objective-C数组和字典
查看>>
mysql登录基本语句
查看>>
废掉一个人最隐蔽的方式,是让他忙到没时间成长(转)
查看>>
二维数组的遍历
查看>>
页面布局(5)——三栏自适应布局(左右定宽中间自适应)
查看>>
【集成学习】sklearn中xgboost模块的XGBClassifier函数
查看>>
装系统遇到的那些问题
查看>>
修改文件
查看>>
成为七牛云 Contributor -如何贡献 logkit 代码
查看>>
apt-get upgarde 和dist-upgrade的差别
查看>>
Difference Between Arraylist And Vector : Core Java Interview Collection Question
查看>>
如何成为“10倍效率”开发者
查看>>
坑,坑,
查看>>
cookie,请求报文,
查看>>
navi push navi,
查看>>