美文网首页
SSM纯注解的方式配置思路总结

SSM纯注解的方式配置思路总结

作者: sanqi37 | 来源:发表于2018-05-04 11:33 被阅读0次
SSM纯注解的方式配置:
分析:
    dao
        底层:放所有的配置类
        创建接口:使用@select 跟 @update ..进行操作
            创建配置类 MySpringConfig 类 
                1.设置配置类  @Configurable
                2.开启mapperScan扫描  @MapperScan("cn.itcast.dao")  //扫描接口路径
                3.配置数据源  @Bean 
                    //配置DataSource
                    public DataSource getDataSource()
                    //配置sqlSessionFactory
                    public SqlSessionFactoryBean getSqlSessionFactoryBean() 
    service
        控制层:
            创建接口:
            创建实现类: 加@service注解 加@Transactional事务注解
                        @Autowired    //自动装配--连接dao层
                        private ProductDao productDao;
            在dao层中的配置类 MySpringConfig 上加注解:
                1.包扫描 @ComponentScan("cn.itcast.service")   //扫描接口实现类
                2.支持事务(配置事务驱动) @EnableTransactionManagement   //开启注解驱动
                3.配置自定义事务管理器
                    @Bean
                    public DataSourceTransactionManager getDataSourceTransactionManager()
                    连接DataSource
    web
        web层:
            创建Controller:
                1.配置@Controller
                2.配置@RequestMapping("/product/")
                3.@Autowired
                    private ProductService productService;
                4.@RequestMapping  方法上注解
            去dao层创建spring MVC 的配置类 MySpringMvcConfig 类  :
                1.@Configurable
                2.包扫描 @ComponentScan("cn.itcast.controller")
                3.spring MVC 注解驱动 (映射器跟适配器)  
                    @EnableWebMvc
                4.配置视图解析器
                @Bean
                public InternalResourceViewResolver getInternalResourceViewResolver()
                setPrefix前缀     setSuffix后缀
                
    web.xml(★★★)
        创建MyAppConfig 类 实现 WebApplicationInitializer 接口
            (在SSM框架中,提供了一个支持纯注解的接口)
            重写onStartup 方法:
            public class MyAppConfig implements WebApplicationInitializer {

                @Override
                public void onStartup(ServletContext servletContext) throws ServletException {
                    //spring提供的注解配置context对象 用于注册自己写的配置类
                    AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext();
                    context.register(MySpringConfig.class,MySpringMvcConfig.class);
                    //配置springMVC的核心servlet
                    javax.servlet.ServletRegistration.Dynamic dispatcherServlet = 
                            servletContext.addServlet("dispatcherServlet",new DispatcherServlet(context));
                    dispatcherServlet.addMapping("*.do");
                    //添加spring监听器
                    servletContext.addListener(new ContextLoaderListener(context));
                    //解决字符乱码的过滤器
                    javax.servlet.FilterRegistration.Dynamic encodingFilter = 
                            servletContext.addFilter("characterFilter", new CharacterEncodingFilter("utf-8"));
                    //借助枚举  对request  跟  forward  请求进行过滤
                    EnumSet<DispatcherType> es = EnumSet.noneOf(DispatcherType.class);
                    es.add(DispatcherType.REQUEST);
                    es.add(DispatcherType.FORWARD);
                    //添加过滤器的映射地址
                    encodingFilter.addMappingForUrlPatterns(es, true, "/*");
                }
            }

相关文章