概述
配置springMVC
注解
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import redis.clients.jedis.JedisPoolConfig;
/**
* @author : Edward_wei
* @data: 2021/6/14 20:36
* @classNam: ControllerConfig
* @description:
* @kit:jdk-1.8
*/
@Configuration//表明这是一个配置类
@ComponentScan(basePackages = {"com.holy.cn.controller"})//配置controller层的包扫描
@EnableWebMvc //开启controller层的注解驱动
public class ControllerConfig implements WebMvcConfigurer {//实现这个接口才可以配置mvc特有的一些配置
/**
* 设置拦截器
* <mvc:interceptor/>
* @param registry
*/
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(new MyInterceptor())//添加拦截器
.addPathPatterns("/**")//添加匹配路径
.excludePathPatterns("/getCode")//排除路径
.excludePathPatterns("/verifyLogin");
}
/**
* 创建了StringRedisTemplate的模板对象,这玩意只能整字符串,但是放里面不乱吗,整其他的还得redisTemplate
* @return
*/
@Bean
public StringRedisTemplate getStringRedisTemplate(){
StringRedisTemplate stringRedisTemplate = new StringRedisTemplate();
stringRedisTemplate.setConnectionFactory(getRedisConnectionFactory());//把连接工厂给放进去
return stringRedisTemplate;
}
/**
* spring-data-redis的连接工厂,创建Redis必须得这个连接方式
* @return
*/
@Bean
public RedisConnectionFactory getRedisConnectionFactory(){
JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();//上连接池
jedisPoolConfig.setMaxIdle(20);
jedisPoolConfig.setMaxTotal(50);
JedisConnectionFactory jedisConnectionFactory = new JedisConnectionFactory(jedisPoolConfig);
jedisConnectionFactory.setPort(6379);
jedisConnectionFactory.setHostName("localhost");
return jedisConnectionFactory;
}
/**
* <mvc:cors/>
* @param registry
*/
@Override
public void addCorsMappings(CorsRegistry registry) {
registry.addMapping("/**")//匹配所有路径
.allowedOrigins("*")//匹配所有的请求源
.allowedHeaders("*")//匹配所有请求头
.allowedMethods("*")//匹配所有请求方法
.allowCredentials(true);//允许跨域请求携带cookie
}
}
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:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://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/mvc https://www.springframework.org/schema/mvc/spring-mvc.xsd">
<!--
扫描controller-->
<context:component-scan base-package="com.holy.controller"/>
<!--
开启注解驱动-->
<mvc:annotation-driven/>
<!--
开启静态资源不拦截-->
<mvc:default-servlet-handler/>
<!--
视图解析器-->
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="suffix" value=".html"/>
<property name="prefix" value="/WEB-INF/pages/"/>
</bean>
<!--
配置所有拦截器-->
<mvc:interceptors>
<mvc:interceptor>
<mvc:mapping path="/*"/>
<mvc:exclude-mapping path="/login.html"/>
<mvc:exclude-mapping path="/verify_login"/>
<mvc:exclude-mapping path="/toVerify/*"/>
<mvc:exclude-mapping path="/verify_login"/>
<mvc:exclude-mapping path="/sendActiveLink"/>
<mvc:exclude-mapping path="/successActiveAndJumpToLogin"/>
<mvc:exclude-mapping path="/toHome"/>
<mvc:exclude-mapping path="/css/*"/>
<mvc:exclude-mapping path="/fonts/*"/>
<mvc:exclude-mapping path="/js/*"/>
<mvc:exclude-mapping path="/layui/*"/>
<mvc:exclude-mapping path="/images/*"/>
<bean class="com.holy.controller.MyInterceptor"/>
</mvc:interceptor>
<!--
符合规则的干进来-->
</mvc:interceptors>
<!--
配置跨域-->
<mvc:cors>
<mvc:mapping path="/**"/>
</mvc:cors>
</beans>
配置service层spring
注解
import com.holy.cn.MapperConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;
/**
* @author : Edward_wei
* @data: 2021/6/14 20:22
* @classNam: ServiceConfig
* @description:
* @kit:jdk-1.8
*/
@Configuration
@ComponentScan(basePackages = {"com.holy.cn.service"})//开启service包扫描
@EnableTransactionManagement//开启事务注解驱动
public class ServiceConfig {
@Autowired
MapperConfig mapperConfig;
@Bean("transactionManager")//设置事务数据源
public DataSourceTransactionManager getDataSourceTransactionManager(){
DataSourceTransactionManager dataSourceTransactionManager = new DataSourceTransactionManager(mapperConfig.getDatasource());
return dataSourceTransactionManager;
}
}
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:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://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/tx http://www.springframework.org/schema/tx/spring-tx.xsd">
<context:property-placeholder location="classpath:mail.properties" ignore-unresolvable="true"/>
<!--
配置service层-->
<!--开启service包扫描-->
<context:component-scan base-package="com.holy.service"/>
<!-- 配置事务管理器-->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
<!--开启事务注解驱动-->
<tx:annotation-driven/>
<!--
配置邮件bean-->
<bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl">
<property name="username" value="${mail.username}"/>
<property name="password" value="${mail.password}"/>
<property name="host" value="${mail.host}"/>
</bean>
</beans>
mapper层spring
注解
import com.alibaba.druid.pool.DruidDataSource;
import com.github.pagehelper.PageInterceptor;
import org.apache.ibatis.logging.log4j.Log4jImpl;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.mapper.MapperScannerConfigurer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import java.util.ResourceBundle;
/**
* @author : Edward_wei
* @data: 2021/6/14 19:25
* @classNam: MapperConfig
* @description:
* @kit:jdk-1.8
*/
@Configuration
public class MapperConfig {
//配数据源
@Bean
public DruidDataSource getDatasource(){
ResourceBundle resourceBundle = ResourceBundle.getBundle("jdbc");
DruidDataSource druidDataSource = new DruidDataSource();
druidDataSource.setDriverClassName(resourceBundle.getString("mysql.Driver"));
druidDataSource.setUrl(resourceBundle.getString("mysql.url"));
druidDataSource.setUsername(resourceBundle.getString("mysql.username"));
druidDataSource.setPassword(resourceBundle.getString("mysql.password"));
return druidDataSource;
}
//配sqlsessionfactory
@Bean
public SqlSessionFactoryBean getSqlSessionFactoryBean(){
SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
sqlSessionFactoryBean.setDataSource(getDatasource());
//配置mybatis
org.apache.ibatis.session.Configuration configuration = new org.apache.ibatis.session.Configuration();
configuration.setMapUnderscoreToCamelCase(true);//设驼峰
configuration.setLogImpl(Log4jImpl.class);
sqlSessionFactoryBean.setConfiguration(configuration);
//配置分页
sqlSessionFactoryBean.setPlugins(new PageInterceptor());
return sqlSessionFactoryBean;
}
//配置mapper层包扫描
@Bean
public MapperScannerConfigurer setMapperScannerConfigurer(){
MapperScannerConfigurer mapperScannerConfigurer = new MapperScannerConfigurer();
mapperScannerConfigurer.setBasePackage("com.holy.cn.mapper");
return mapperScannerConfigurer;
}
}
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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">
<context:property-placeholder location="classpath:jdbc.properties" ignore-unresolvable="true"/>
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
<property name="driverClassName" value="${mysql.Driver}"/>
<property name="url" value="${mysql.url}"/>
<property name="password" value="${mysql.password}"/>
<property name="username" value="${mysql.username}"/>
</bean>
<!--配置mapper层-->
<bean id="sqlSessionFactoryBean" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<!--
引入mybatis的配置-->
<property name="configuration" ref="configuration"/>
<!--
分页-->
<property name="plugins">
<bean class="com.github.pagehelper.PageInterceptor">
</bean>
</property>
</bean>
<!--
原先为mybatis配置文件中的mapper层扫描-->
<bean
id="configurer" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.holy.mapper"/>
</bean>
<!--
mybatis的配置-->
<bean id="configuration" class="org.apache.ibatis.session.Configuration">
<!--
开启驼峰-->
<property name="mapUnderscoreToCamelCase" value="true"/>
</bean>
</beans>
最后
以上就是粗暴秀发为你收集整理的springMvc的web项目基础配置xml版和注解版的全部内容,希望文章能够帮你解决springMvc的web项目基础配置xml版和注解版所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复