SPRING 学习


以下为学习的相关章节
一、SPRING JAVA EE配置
二、SPRING Hibernate的使用配置
三、Hibernate使用


SPRING JAVA EE配置

服务器启动加载IOC容器,加载配置有两种方法,以下AppCfg都是基于注解的方式进行的处理

1>.AbstractAnnotationConfigDispatcherServletInitializer,继承该方法,可以实现相关配置的加载。具体的方法如下

public class AppInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {

    @Override
    protected Class<?>[] getRootConfigClasses() {
    //此处将返回AppConfig.class标注的相关配置
        return new Class[] { AppConfig.class };
    }

    @Override
    protected Class<?>[] getServletConfigClasses() {
        // TODO 自动生成的方法存根
        return null;
    }

    @Override
    protected String[] getServletMappings() {
    //此处用于拦截请求,"/"用于拦截所有的请求
        return new String[] { "/" };
    }
}

2.—–待补充

3.APPCFG类
该类继承于WebMvcConfigurerAdapter ,可以对MVC添加静态的资源处理,也可以添加相关的拦截器
@Configuration
- 标注了该类属于配置类;
@EnableWebMvc
该注解等同于相当于注册了DefaultAnnotationHandlerMapping和AnnotationMethodHandlerAdapter两个bean到IOC容器
- DefaultAnnotationHandlerMapping完成Url/Controller的映射
- AnnotationMethodHandlerAdapter完成Url/Controller.方法的映射
@ComponentScan({ “com.Ctl”, “com.Dao” })
扫描的路径,将扫描一系列的注解组件并按照配置注入容器


import org.springframework.context.MessageSource;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.support.ResourceBundleMessageSource;
import org.springframework.web.servlet.ViewResolver;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import org.springframework.web.servlet.view.InternalResourceViewResolver;
import org.springframework.web.servlet.view.JstlView;

@Configuration
@EnableWebMvc
@ComponentScan({ "com.Ctl", "com.Dao" })

public class AppConfig extends WebMvcConfigurerAdapter {
    @Bean
    public ViewResolver viewResolver() {
        InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
        viewResolver.setViewClass(JstlView.class);
        viewResolver.setPrefix("/WEB-INF/jsp/");
        viewResolver.setSuffix(".jsp");
        return viewResolver;
    }

    @Bean
    public MessageSource messageSource() {
        ResourceBundleMessageSource messageSource = new ResourceBundleMessageSource();
        messageSource.setBasename("messages");
        return messageSource;
    }

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/resources/**").addResourceLocations("/public-resources/");
    }

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值