springmvc 排除redis扫描
时间: 2023-11-03 12:05:22 浏览: 47
要在Spring MVC中排除Redis扫描,可以使用@ComponentScan注解来指定组件扫描的范围,从而排除Redis相关的包。以下是一个示例:
```java
@Configuration
@ComponentScan(basePackages = {"com.example"},
excludeFilters = {@ComponentScan.Filter(type = FilterType.REGEX, pattern = "com\\.example\\.redis\\..*")})
public class AppConfig {
// ...
}
```
在上述示例中,通过`excludeFilters`参数指定了要排除的包的正则表达式。此示例将排除以`com.example.redis`开头的所有包。
请注意,这只是一个简单示例,实际情况可能需要根据项目的实际结构和需求进行调整。
相关问题
springmvc整合redis使用
引用\[1\]:在Spring MVC中整合Redis的使用可以通过配置Jedis连接池来实现。首先需要在Spring配置文件中配置JedisPool bean,指定Redis服务的IP地址和端口号。然后可以创建一个JedisClientPool bean来使用连接池。\[1\]引用\[2\]:使用连接池的方式可以预先初始化好Jedis连接,每次只需要从连接池中借用连接即可,避免了每次都需要建立TCP连接的开销。这种方式还可以有效地保护和控制资源的使用。\[2\]引用\[3\]:另外,可以在Spring MVC中直接使用Jedis对象进行Redis操作,但这种方式每次都需要构建TCP连接,使用完后再关闭连接,效率较低。\[3\]因此,推荐使用连接池的方式来整合Redis在Spring MVC中的使用。
#### 引用[.reference_title]
- *1* [SpringMVC整合Redis实战](https://blog.csdn.net/QQ1941638512/article/details/108344598)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insert_down1,239^v3^insert_chatgpt"}} ] [.reference_item]
- *2* *3* [SpringMVC整合Redis(包含java工具类)](https://blog.csdn.net/qq_40739917/article/details/107003722)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insert_down1,239^v3^insert_chatgpt"}} ] [.reference_item]
[ .reference_list ]
SpringMVC使用Redis配置session共享
1. 添加Redis依赖库
在项目的pom.xml文件中添加Redis的依赖库。
```xml
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-redis</artifactId>
<version>2.3.2.RELEASE</version>
</dependency>
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>3.0.0</version>
</dependency>
```
2. 配置Redis连接池
在SpringMVC的配置文件中添加Redis的连接池配置。
```xml
<bean id="jedisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
<property name="hostName" value="localhost"/>
<property name="port" value="6379"/>
<<property name="password" value="password"/>>
<property name="poolConfig">
<bean class="redis.clients.jedis.JedisPoolConfig">
<property name="maxTotal" value="1000"/>
<property name="maxIdle" value="100"/>
<property name="minIdle" value="10"/>
<property name="maxWaitMillis" value="10000"/>
</bean>
</property>
</bean>
<bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate">
<property name="connectionFactory" ref="jedisConnectionFactory"/>
</bean>
```
3. 配置Session共享
在SpringMVC的配置文件中添加Session共享的配置。
```xml
<bean id="redisHttpSessionConfiguration" class="org.springframework.session.data.redis.config.annotation.web.http.RedisHttpSessionConfiguration">
<property name="maxInactiveIntervalInSeconds" value="1800"/>
</bean>
<bean class="org.springframework.session.web.http.CookieHttpSessionIdResolver">
<constructor-arg name="cookieName" value="SESSION_ID"/>
<property name="cookiePath" value="/"/>
<property name="cookieMaxAge" value="-1"/>
</bean>
```
4. 启用Session共享
在SpringMVC的配置文件中启用Session共享。
```xml
<bean class="org.springframework.session.web.http.SessionRepositoryFilter">
<constructor-arg>
<bean class="org.springframework.session.data.redis.RedisOperationsSessionRepository">
<constructor-arg ref="redisTemplate"/>
</bean>
</constructor-arg>
</bean>
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
<property name="sessionAttributeStore">
<bean class="org.springframework.session.web.http.SessionRepositoryFilter">
<constructor-arg>
<bean class="org.springframework.session.data.redis.RedisOperationsSessionRepository">
<constructor-arg ref="redisTemplate"/>
</bean>
</constructor-arg>
</bean>
</property>
</bean>
```
5. 测试
启动项目后,访问任意一个页面,然后在Redis中就可以看到Session的信息了。
```shell
redis-cli
127.0.0.1:6379> keys *
1) "spring:session:sessions:5b5d4b4f-9a9f-4e4e-bd56-4b4c7f1d8f0b"
127.0.0.1:6379> get spring:session:sessions:5b5d4b4f-9a9f-4e4e-bd56-4b4c7f1d8f0b
"\xac\xed\x00\x05sr\x00\x17java.util.HashMap\x85\x95\x1d\x9b\x94\xee\x0c\x00\x00\x03I\x00\x05valuexr\x00\x10java.lang.String\x86\xac\xed\x00\x05sr\x00\x11java.lang.String\x12\xe2\xa0\xa4\xf7\x81\x878\x02\x00\x01Ljava/lang/String;L\x00\x04namet\x00\x12Ljava/lang/String;L\x00\x08usernameq\x00~\x00\x01sq\x00~\x00\x0bpasswordq\x00~\x00\x0b"
```