关于 SpringMVC+Spring+MyBatis 基于注解整合(2)

/**
    * 设置统一错误处理要跳转的视图
    *
    * @return
    */
    @Bean
    public SimpleMappingExceptionResolver simpleMappingExceptionResolver() {
        SimpleMappingExceptionResolver simpleMappingExceptionResolver = new SimpleMappingExceptionResolver();
        Properties properties = new Properties();
        properties.getProperty("java.lang.Exception", "error");
        simpleMappingExceptionResolver.setExceptionMappings(properties);
        return simpleMappingExceptionResolver;
    }

/**
    * 添加静态资源
    *
    * @param registry
    */
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler(propertyConfig.getWebStaticHandler()).addResourceLocations(propertyConfig.getWebStaticResource()).setCachePeriod(propertyConfig.getWebStaticCachedPeriod());
    }

/**
    * 添加拦截器
    *
    * @param registry
    */
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        super.addInterceptors(registry);
    }
}

此处配置SpringMVC的视图解析器,静态资源等,依旧照搬配置文件中的代码

  3.PropertyConfig

package com.bdqn.lyrk.ssm.study.config;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;

@Configuration
@PropertySource("classpath:application.properties")
public class PropertyConfig {

@Value("${spring.datasource.url}")
    private String url;
    @Value("${spring.datasource.driver}")
    private String driver;
    @Value("${spring.datasource.user}")
    private String user;
    @Value("${spring.datasource.password}")
    private String password;
    @Value("${spring.web.view.prefix}")
    private String webViewPrefix;
    @Value("${spring.web.view.suffix}")
    private String webViewSuffix;
    @Value("${spring.web.static.handler}")
    private String webStaticHandler;
    @Value("${spring.web.static.resource}")
    private String webStaticResource;
    @Value("${spring.web.static.cache.period}")
    private Integer webStaticCachedPeriod;
    @Value("${mybatis.type.alias.package}")
    private String mybatisTypeAliasPackage;

public String getWebViewPrefix() {
        return webViewPrefix;
    }

public String getWebViewSuffix() {
        return webViewSuffix;
    }

public String getWebStaticHandler() {
        return webStaticHandler;
    }

public String getWebStaticResource() {
        return webStaticResource;
    }

public Integer getWebStaticCachedPeriod() {
        return webStaticCachedPeriod;
    }

public String getMybatisTypeAliasPackage() {
        return mybatisTypeAliasPackage;
    }

public String getUrl() {
        return url;
    }

public String getDriver() {
        return driver;
    }

public String getUser() {
        return user;
    }

public String getPassword() {
        return password;
    }

@Bean
    public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() {
        return new PropertySourcesPlaceholderConfigurer();
    }
}

此处用于读取application.properties的文件内容 注意@Value与@PropertySource的含义

4.MyWebAppInitializer

package com.bdqn.lyrk.ssm.study.config;

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/3d7705eee9b36ce602f85539651659ac.html