零配置整合初步整合ssm框架+junit测试的配置

由于看到一大堆xml文件就蛋疼,所以本人倾向于用注解来配置spring,经过对比确实也要简单方便一些,增加功能也可以通过重写方法来实现,不过这种好像只能在servlet3.0和spring3以上才能用.
首先在config包下一个类,继承AbstractAnnotationConfigDispatcherServletInitializer类…由于一系列的继承关系,总之Spring在启动时就会检查到我们继承了这个抽象类的类从而用来配置ServletContext.

 package cn.paul.config;

import org.springframework.context.annotation.Bean;
import org.springframework.web.filter.CharacterEncodingFilter;
import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;

import javax.servlet.MultipartConfigElement;
import javax.servlet.ServletRegistration;


/**
 * Created by paul on 2017-06-14.
 * 此乃系统入口
 */
 public class SpringInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {


    @Override
    protected Class<?>[] getRootConfigClasses() {
        return new Class<?>[]{RootConfig.class};
    }


    @Override
    protected Class<?>[] getServletConfigClasses() {
        return new Class<?>[]{WebMVCConfig.class};
    }

    @Override
    protected String[] getServletMappings() {
        //将DispatcherServlet映射到"/"
        return new String[]{"/"};
    }

    @Override
    protected void customizeRegistration(ServletRegistration.Dynamic registration) {
        registration.setMultipartConfig(new MultipartConfigElement(""));
    }
}

接下来需要写WebMVCConfig,由于功能简单只配置了jsp的处理

package cn.paul.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.web.multipart.MultipartResolver;
import org.springframework.web.multipart.support.StandardServletMultipartResolver;
import org.springframework.web.servlet.ViewResolver;
import org.springframework.web.servlet.config.annotation.DefaultServletHandlerConfigurer;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import org.springframework.web.servlet.view.InternalResourceViewResolver;
import org.springframework.web.servlet.view.JstlView;

/**
 * Created by paul on 2017-06-14.
 * 该文件用于配置Spring的视图解析器,为了解析jsp采用ViewResolver对象
 */
 //该注解表示这是一个配置类
@Configuration
//表示启用SpringMVC
@EnableWebMvc
//指定扫描的包,我们这里让他控制web下的控制器即可
@ComponentScan(basePackages = {"cn.paul.web"})
public class WebMVCConfig extends WebMvcConfigurerAdapter {
    @Bean
    public ViewResolver viewResolver() {
        //配置jsp视图解析器
        InternalResourceViewResolver resolver =
                new InternalResourceViewResolver();
        //这里是jsp文件的路径映射,我们把jsp文件放在WEB-INF下的views文件夹中
        resolver.setPrefix("/WEB-INF/views/");
        resolver.setSuffix(".jsp");
        resolver.setViewClass(JstlView.class);
        resolver.setExposeContextBeansAsAttributes(true);
        return resolver;
    }



    @Override
    public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
        //配置静态资源处理
        configurer.enable();
    } 
}

而RootConfig我打算用来配置一些其他的bean,比如mybatis相关的东西.

package cn.paul.config;

import cn.paul.dao.UserDao;
import cn.paul.dao.impl.UserDaoImpl;
import cn.paul.mapper.UserMapper;
import com.mchange.v2.c3p0.ComboPooledDataSource;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.annotation.MapperScan;
import org.mybatis.spring.mapper.MapperScannerConfigurer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.FilterType;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;

import javax.sql.DataSource;

/**
 * Created by paul on 2017-06-14.
 */
@EnableTransactionManagement
@Configuration
@MapperScan("cn.paul.mapper")
@ComponentScan(basePackages = { "cn.paul" },
        excludeFilters = { @ComponentScan.Filter(type = FilterType.ANNOTATION,
                value = EnableWebMvc.class) })
public class RootConfig {
    @Bean
    public DataSource dataSource() {
        return new ComboPooledDataSource();
    }

    @Bean
    public SqlSessionFactoryBean sqlSessionFactory() throws Exception {
        SqlSessionFactoryBean sqlSessionFactory = new SqlSessionFactoryBean();
        sqlSessionFactory.setDataSource(dataSource());
        sqlSessionFactory.setTypeAliasesPackage("cn.paul.domain");
        return sqlSessionFactory;
    }
 @Bean
    public DataSourceTransactionManager dataSourceTransactionManager() {
        return new DataSourceTransactionManager(dataSource());
    }



}

这里有一个坑就是@componentScan中设置扫描的包可以是根目录下所有的包,但是必须要排除有@enableMVC注解的类,不然一会儿Junit会报错.

一个简单的mapper类,用来做登录功能,那个@param必须要写不然会提示找不到参数…还有@repository也要写上不然 会报红,但好像不写也能通过…

package cn.paul.mapper;

import cn.paul.domain.User;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Repository;

/**
 * Created by dell1 on 2017-06-25.
 */
@Repository
public interface UserMapper {
    /**
     * 根据用户名密码查询,即登陆功能
     * @param  username
     * @param  password
     * @return 返回user对象
     * */
    @Select("select * from tb_user4 where username=#{username} and password=#{password}")
    User findUser( @Param("username")String username,@Param("password") String password);
}

猜你喜欢

转载自blog.csdn.net/sinat_21372989/article/details/73822686
今日推荐