全代码配置SSH框架

全代码配置web.xml

编写类WebConfig.java

import javax.servlet.Filter;
import org.springframework.web.filter.CharacterEncodingFilter;
import org.springframework.web.filter.HiddenHttpMethodFilter;
import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;
//Servlet3开始可以用代码取代web.xml的配置
public class WebConfig extends AbstractAnnotationConfigDispatcherServletInitializer {
    //Spring IOC容器的配置类
    @Override
    protected Class[] getRootConfigClasses() {
        return new Class[]{AppConfig.class};
    }
    //Spring MVC的配置类
    @Override
    protected Class[] getServletConfigClasses() {
        return new Class[]{MvcConfig.class};
    }
    //前端控制器的映射
    @Override
    protected String[] getServletMappings() {
        return new String[]{"/"};
    }
    //过滤器配置
    @Override
    protected Filter[] getServletFilters() {
        CharacterEncodingFilter encodingFilter = new CharacterEncodingFilter("utf-8");
        encodingFilter.setForceEncoding(true);
        HiddenHttpMethodFilter restFilter = new HiddenHttpMethodFilter();
        return new Filter[]{encodingFilter,restFilter};
    }

}

配置SpringMVC

编写MvcConfig.java

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
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;
//相当于是springmvc-servlet.xml
@Configuration
@EnableWebMvc
@ComponentScan("com.chenx.controller")//写单例对象的前提是无状态对象
public class MvcConfig 
    extends WebMvcConfigurerAdapter {
    //静态资源交给DefaultServlet处理
    @Override
    public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
        // TODO Auto-generated method stub
        configurer.enable();
    }
    //视图解析器
    @Bean//@Scope(创建对象的方式)@Primary(首选项)
    public ViewResolver jspViewResolver(){
        InternalResourceViewResolver viewResolver = 
                new InternalResourceViewResolver();
        viewResolver.setViewClass(JstlView.class);
        viewResolver.setPrefix("/WEB-INF/jsp/");
        viewResolver.setSuffix(".jsp");
        return viewResolver;
    }
}

配置Spring

编写类AppConfig.java

import java.util.Properties;

import javax.sql.DataSource;

import org.hibernate.SessionFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.orm.hibernate5.HibernateTransactionManager;
import org.springframework.orm.hibernate5.LocalSessionFactoryBean;
import org.springframework.transaction.annotation.EnableTransactionManagement;

import com.alibaba.druid.pool.DruidDataSource;

@Configuration
@EnableTransactionManagement
@ComponentScan("com.qfedu")
public class AppConfig {
    //DataSource 对象
    @Bean
    public DataSource getDataSource() {
        DruidDataSource dataSource = new DruidDataSource();
        dataSource.setUrl("jdbc:mysql://localhost:3306/hr?useUnicode=true&characterEncoding=utf8");
        dataSource.setUsername("root");
        dataSource.setPassword("123456");
        dataSource.setMinIdle(10);
        dataSource.setInitialSize(10);
        dataSource.setMaxActive(100);
        return dataSource;
    }
    //SessionFactory对象
    @Bean
    public LocalSessionFactoryBean getSessionFactory(DataSource dataSource) {
        LocalSessionFactoryBean bean = new LocalSessionFactoryBean();
        bean.setDataSource(dataSource);
        bean.setPackagesToScan("com.chenx.model");
        Properties hibernateProperties = new Properties();
        hibernateProperties.setProperty("hibernate.dialect", "org.hibernate.dialect.MySQL5Dialect");
        hibernateProperties.setProperty("hibernate.show_sql", "true");
        hibernateProperties.setProperty("hibernate.format_sql", "true");
        bean.setHibernateProperties(hibernateProperties);
        return bean;
    }
    //配置事务
    @Bean
    public HibernateTransactionManager getTxManager(SessionFactory sessionFactory) {
        return new HibernateTransactionManager(sessionFactory);
    }
}
  1. @Configuration:相当于xml文件中的
  2. @Bean:相当于xml文件中的
  3. @EnableWebMvc 启用Spring MVC支持
  4. @ComponentScan("com.qfedu")--注解类查找规则定义
  5. @EnableTransactionManagement开启注解式事务的支持

你可能感兴趣的:(全代码配置SSH框架)