1.springWeb之java配置
@Configuration
@ComponentScan(basePackages = "com.wise.tiger",excludeFilters = @ComponentScan.Filter(Controller.class))
@PropertySource(value = "classpath:dbcp-config.properties", encoding = "UTF-8")//导入配置文件
@EnableAspectJAutoProxy//打开自动代理
@EnableTransactionManagement//提供事务支持
public class SpringIoCConfig {
/**
* 配置数据源
*/
@Bean
public DataSource dataSource(
@Value("${driverClassName}") String driverClass, @Value("${url}") String url, @Value("${jdbc.username}") String username,
@Value("${password}") String password, @Value("${defaultAutoCommit}") boolean defaultAutoCommit,
@Value("${connectionProperties}") String connectionProperties) {
var dataSource = new DruidDataSource();
dataSource.setDriverClassName(driverClass);dataSource.setUrl(url);dataSource.setUsername(username);
dataSource.setPassword(password);dataSource.setDefaultAutoCommit(defaultAutoCommit);
dataSource.setConnectionProperties(connectionProperties);
return dataSource;
}
/**
* 配置SqlSessionFactory
* SqlSessionFactory是产生SqlSession的基础,在MyBatis-Spring项目中提供了SqlSessionFactoryBean去支持SqlSessionFactory的配置
*/
@Bean
public SqlSessionFactory sqlSessionFactory(DataSource dataSource) throws Exception {
var factoryBean = new SqlSessionFactoryBean();
//设置类型别名
factoryBean.setTypeAliasesPackage("com.wise.tiger.domain");
//设置数据源
factoryBean.setDataSource(dataSource);
//ResourcePatternResolver定义了getResources来查找资源,PathMatchingResourcePatternResolver提供了以classpath开头的通配符方式查询
var resourcePatternResolver = new PathMatchingResourcePatternResolver();
factoryBean.setMapperLocations(resourcePatternResolver.getResources("classpath*:com/wise/tiger/mapper/**/*.xml"));
return factoryBean.getObject();
}
/**
* 事务管理器(Mybaits采用DataSourceTransactionManger)
*/
@Bean
public DataSourceTransactionManager transactionManager(DataSource dataSource) {
return new DataSourceTransactionManager(dataSource);
}
}
@Configuration
@EnableWebMvc
@ComponentScan(basePackages = "com.wise.tiger.web")
public class SpringMVCConfig implements WebMvcConfigurer {
/**
* 配置视图解析器
* @return
*/
@Bean
public InternalResourceViewResolver viewResolver(){
var viewResolver =new InternalResourceViewResolver();
viewResolver.setViewClass(org.springframework.web.servlet.view.JstlView.class);
viewResolver.setPrefix("/WEB-INF/book/");
viewResolver.setSuffix(".jsp");
return viewResolver;
}
/**
* 添加视图映射静态过滤器
* @param registry
*/
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/static/**").addResourceLocations("/static/");
registry.addResourceHandler("/js/**").addResourceLocations("/js/");
registry.addResourceHandler("/css/**").addResourceLocations("/css/");
registry.addResourceHandler("/images/**").addResourceLocations("/images/");
}
/**
* 添加类型转换器
* @param registry
*/
@Override
public void addFormatters(FormatterRegistry registry) {
registry.addConverter(new StringArray2SetRoleConverter());
}
/**
* 添加表单校验器
*/
@Override
public Validator getValidator() {
var validator = new LocalValidatorFactoryBean();
validator.setProviderClass(HibernateValidator.class);
validator.setValidationMessageSource(messageResource());
return validator;
}
/**
* 添加拦截器
* @param registry
*/
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(new LoginInterceptor()).addPathPatterns("/user*").addPathPatterns("/content*");
}
/**
* 配置消息处理器
* @return
*/
@Bean
public ReloadableResourceBundleMessageSource messageResource(){
var messageResource = new ReloadableResourceBundleMessageSource();
// messageResource.setBasename("");
return messageResource;
}
/**
* 配置文件上传解析器
* @return
*/
@Bean(name = "multipartResolver")
public MultipartResolver multipartResolver() {
//若return new CommonsMultipartResolver(); 则需要apache的commons-upload.jar组件支持,可以定义文件上传属性,且无需再在ApplicationConfig中配置
return new StandardServletMultipartResolver();
}
}
@Configuration
public class ApplicationConfig extends AbstractAnnotationConfigDispatcherServletInitializer {
@Override
protected Class<?>[] getRootConfigClasses() {
return new Class[]{RootConfig.class};
}
@Override
protected Class<?>[] getServletConfigClasses() {
return new Class[]{ServletConfig.class};
}
@Override
protected String[] getServletMappings(){
return new String[]{"/"};
}
@Override
protected Filter[] getServletFilters() {
return new Filter[]{new CharacterEncodingFilter("utf-8", true)};
}
@Override
protected void customizeRegistration(ServletRegistration.Dynamic registration) {
registration.setMultipartConfig(
new MultipartConfigElement(null,30*1024*1024,100*1024*1024,0));
}
}
2.springWeb之xml配置
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
https://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop
https://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/mvc
https://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/tx
https://www.springframework.org/schema/tx/spring-tx.xsd">
<!-- 开启注解扫描-->
<context:component-scan base-package="com.wise.tiger">
<!--过滤掉@Controller注解-->
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
<bean class="com.wise.tiger.aop.BookAspect" name="bookAspect2"/>
<!-- 启动aop注解支持 -->
<aop:config>
<!--定义切入点 -->
<aop:pointcut expression="execution(* com.wise.tiger..*(..))" id="pointcut"/>
<aop:aspect ref="xmlAspect">
<!-- 前置通知 -->
<aop:before method="before" pointcut-ref="pointcut" />
<aop:after-returning method="afterReturn" pointcut-ref="pointcut"/>
<aop:after method="after" pointcut-ref="pointcut"/>
<aop:after-throwing method="throwing" pointcut-ref="pointcut" throwing="ex"/>
<aop:around method="around" pointcut="execution(* com.wise.tiger..*(..)) and @annotation(authority)" />
</aop:aspect>
</aop:config>
<context:property-placeholder location="classpath:dbcp-config.properties"/>
<!-- step1:配置数据源:DruidDataSource事务默认自动,需要关闭自动提交 -->
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" destroy-method="close" >
<property name="driverClassName" value="${driverClassName}" />
<property name="url" value="${url}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${password}" />
<property name="defaultAutoCommit" value="${defaultAutoCommit}"/>
<property name="connectionProperties" value="${connectionProperties}"/>
</bean>
<!-- step2:配置jdbc模板类 -->
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource"/>
</bean>
<!--配置数据源事务管理器 -->
<bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
<!--开启注解管理事务-->
<tx:annotation-driven transaction-manager="txManager"/>
</beans>
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
https://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop
https://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/mvc
https://www.springframework.org/schema/mvc/spring-mvc.xsd">
<!--开启mvc注解驱动-->
<mvc:annotation-driven/>
<!--mvc扫描的包-->
<context:component-scan base-package="com.wise.tiger.controller"/>
<!--配置视图解析器-->
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
<property name="prefix" value="/WEB-INF/"/>
<property name="suffix" value=".jsp"/>
</bean>
<!--配置拦截器-->
<mvc:interceptors>
<mvc:interceptor>
<mvc:mapping path="/controll/**"/>
<bean class="com.wise.bbs.interceptor.RoleInterceptor"/>
</mvc:interceptor>
</mvc:interceptors>
</beans>
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0">
<!--配置监听器-->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!--配置springIoC-->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:beans.xml</param-value>
</context-param>
<!--配置编码过滤器-->
<filter>
<filter-name>characterEncodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
<init-param>
<param-name>forceRequestEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>characterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!--配置处理器分发器-->
<servlet>
<servlet-name>dispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring-mvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcherServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>