spring mvc(2)

代码配置:

/*
 *Servlet3.0提供了ServletContainerInitializer来实现容器启动时注册
 *Servlet、Filter、Listener,它可以用来替代web.xml文件,而Spring mvc
 *提供了更为简单的WebApplicationInitializer接口,并提供了一个
 *AbstractContextLoaderInitializer,它可以动态注册DispatcherServlet
 *为了实现零配置,Spring直接给我们提供了一个现成的类
  */
org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer

例如:

public class MyWebAppInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {

    @Override
    protected Class[] getRootConfigClasses() {
        //返回配置Ioc容器的类的类对象
        return new Class[]{AppConfig.class};
    }

    @Override
    protected Class[] getServletConfigClasses() {
        //返回配置Spring MVC的类的类对象
        return new Class[]{WebConfig.class};
    }

    @Override
    protected String[] getServletMappings() {
        //返回配置的urlPatterns
        return new String[] {"/"};
    }
}

接着是Ioc容器的配置,给出以下模板

@Configuration //表示这是一个配置
@ComponentScan(basePackages = "com.qfedu.springmvc") //扫描该包下面的所有打了spring注解的类
@EnableAspectJAutoProxy //允许AspectJ自动生成代理
@EnableTransactionManagement //启用注解事务,即可以使用@Transactional注解来控制事务
public class AppConfig {
    @Bean
    public DataSource dataSource() {
        BasicDataSource source = new BasicDataSource();
        source.setDriverClassName("com.mysql.jdbc.Driver");
        source.setUsername("root");
        source.setPassword("ily");
        source.setUrl("jdbc:mysql://localhost:8080/springmvc?"
            + "useUnicode=true&characterEncoding=utf8");
        source.setInitialSize(10);
        return source;
    }

    @Bean
    public LocalSessionFactoryBean sessionFactory(DataSource source) {
        LocalSessionFactoryBean bean = new LocalSessionFactoryBean();
        bean.setDataSource(source);
        bean.setHibernateProperties(getHibernateProperties());
        bean.setAnnotatedPackages("com.qfedu.springmvc.entiy");
        return bean;
    }

    @Bean
    public HibernateTransactionManager transactionManager(SessionFactory sessionFactory) {
        return new HibernateTransactionManager(sessionFactory);
    }

    private Properties getHibernateProperties() {
        Properties prop = new Properties();
        prop.setProperty("hibernate.dialect", "org.hibernate.dialect.MySQL57InnoDBDialect");
        prop.setProperty("hibernate.show_sql", "true");
        prop.setProperty("hibernate.format_sql", "true");
        return prop;
    }
}

spring mvc配置,作用与*-servlet.xml文件相当

@Configuration
@EnableWebMvc
@ComponentScan(basePackages = "com.qfedu.springmvc.controller")
/**
 * 继承 WebMvcConfigurerAdapter,实现静态资源的处理
 */
public class WebConfig extends WebMvcConfigurerAdapter {

    @Bean
    public ViewResolver internalResourceViewResolver() {
        InternalResourceViewResolver viewResolver 
          = new InternalResourceViewResolver("/WEB-INF/jsp/", ".jsp");
        viewResolver.setViewClass(JstlView.class);
        return viewResolver;
    }
    /**
     *配置一个处理程序来转发未处理的请求,转发到Servlet容器的“默认”servlet。
     */
    @Override
    public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
        configurer.enable();
    }
}

你可能感兴趣的:(spring mvc(2))