Today, let’s record the problems I encountered during the integration of the framework:
1. When using redis to implement session sharing, the project start report No bean named 'springSessionRepositoryFilter' is defined exception
2. When calling the cache tool class, the injected JedisPool is Null (a detailed error related to spring scan)
OK, I've started to add the files I integrated
pom.xml depends on jar package
<!-- spring session begin --> <dependency> <groupId>redis.clients</groupId> <artifactId>jedis</artifactId> <version>2.9.0</version> </dependency> <dependency> <groupId>org.springframework.session</groupId> <artifactId>spring-session-data-redis</artifactId> <version>1.2.1.RELEASE</version> </dependency>
web.xml configuration
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0"> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:spring-cfg.xml</param-value> </context-param> <!--session filter placed in the filter head--> <filter> <filter-name>springSessionRepositoryFilter</filter-name> <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class> </filter> <filter-mapping> <filter-name>springSessionRepositoryFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <!-- Encoding filter--> <filter> <filter-name>encodingFilter</filter-name> <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> <async-supported>true</async-supported> <init-param> <param-name>encoding</param-name> <param-value>UTF-8</param-value> </init-param> </filter> <filter-mapping> <filter-name>encodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <!-- Spring Listener--> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <!-- Spring MVC--> <servlet> <servlet-name>SpringMVC</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> <async-supported>true</async-supported> </servlet> <servlet-mapping> <servlet-mapping> <servlet-name>SpringMVC</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> <!-- <servlet-mapping> <servlet-name>default</servlet-name> <url-pattern>/static/*</url-pattern> </servlet-mapping>--> </web-app>
spring-cfg.xml configuration file
<?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:tx="http://www.springframework.org/schema/tx" xmlns:util="http://www.springframework.org/schema/util" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd" > <!--Open the automatic proxy for the facet programming --> <aop:aspectj-autopproxy proxy-target-class="true"/> <!--Scan the annotation to generate bean--> <context:annotation-config/> <context:component-scan base-package="com.zyt"> <context:exclude-filter type="annotation" expression="org.springframework.steretype.Controller"/> </context:component-scan> <!--Read multiple properties configuration files--> <bean id="propertyConfigurer" > <property name="locations"> <list> <value>classpath:jdbc.properties</value> <value>classpath:redis.properties</value> </list> </property> </bean> <!-- Jedis connection pool--> <bean id="poolConfig"> <property name="maxIdle" value="${redis.maxIdle}"/> <property name="maxTotal" value="${redis.maxActive}"/> <property name="maxWaitMillis" value="${redis.maxWait}"/> <property name="testOnBorrow" value="${redis.testOnBorrow}"/> </bean> <!-- redis's connection pool pool is not a required option: timeout/password --> <bean id = "jedisPool"> <constructor-arg index="0" ref="poolConfig"/> <constructor-arg index="1" value="${redis.host}"/> <constructor-arg index="2" value="${redis.port}" type="int"/> <constructor-arg index="3" value="${redis.timeout}" type="int"/> <!-- <constructor-arg index="4" value="${redis.password}"/> </bean> <!-- Jedis Connection Factory --> <bean id="jedisConnectionFactory" > <property name="poolConfig" ref="poolConfig"/> <property name="port" value="${redis.port}"/> <property name="hostName" value="${redis.host}"/> <!-- <property name="password" value="${redis.pass}"/> --> </bean> <util:constant static-field="org.springframework.session.data.redis.config.ConfigureRedisAction.NO_OP"/> <!-- Spring Redis Template --> <bean id="redisTemplate"> <property name="connectionFactory" ref="jedisConnectionFactory"/> </bean> <!-- redis end --> <!-- Spring Session begin --> <bean id="redisHttpSessionConfiguration" > <property name="maxInactiveIntervalInSeconds" value="1800"/> </bean> <!--integration of mybatis--> <bean id="sqlSessionFactory"> <property name="dataSource" ref="dataSource"/> <property name="mapperLocations" value="classpath:com/zyt/**/**.xml"/> </bean> <bean> <property name="basePackage" value="com.zyt.*.dao"/> <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/> </bean> <!--Declare transaction management using annotation--> <tx:annotation-driven transaction-manager="transactionManager"/> <bean id="transactionManager"> <property name="dataSource" ref="dataSource"/> </bean> <!--Database Settings--> <bean id="dataSource" destroy-method="close" init-method="init"> <property name="url" value="${jdbc_url}"/> <property name="username" value="${jdbc_username}"/> <property name="password" value="${jdbc_password}"/> <!-- Initialize connection size --> <property name="initialSize" value="0"/> <!-- Maximum number of connections used by connection pool--> <property name="maxActive" value="20"/> <property name="minimum idle" value="property name="minIdle" value="0"/> <!-- Get the maximum connection waiting time --> <property name="maxWait" value="60000"/> <!-- <property name="poolPreparedStatements" value="true" /> <property name="maxPoolPreparedStatementPerConnectionSize" value="33" /> --> <property name="validationQuery" value="${validationQuery}"/> <property name="testOnBorrow" value="false"/> <property name="testOnBorrow" value="false"/> <property name="testWhileIdle" value="true"/> <!-- Configure how long it takes to perform a detection interval to detect idle connections that need to be closed, in milliseconds --> <property name="timeBetweenEvictionRunsMillis" value="60000"/> <!-- Configure the minimum time for a connection to survive in the pool, in milliseconds --> <property name="minEvictableIdleTimeMillis" value="25200000"/> <!-- Turn on removeAbandoned function --> <property name="removeAbandoned" value="true"/> <!-- 1800 seconds, that is, 30 minutes --> <property name="removeAbandonedTimeout" value="1800"/> <!-- Output error log when closing the abandoned connection --> <property name="logAbandoned" value="true"/> <!-- Monitoring database --> <!-- <property name="filters" value="stat" /> --> <property name="filters" value="mergeStat"/> </beans>jdbc.properties
driverClassName=com.mysql.jdbc.Driver validationQuery=SELECT 1 jdbc_url=jdbc:mysql://localhost:3306/zyt_demo?useUnicode=true&characterEncoding=UTF-8&zeroDateTimeBehavior=convertToNull jdbc_username=root jdbc_password=root
redis.properties
redis.isopen=on redis.host=127.0.0.1 redis.port=6379 redis.maxIdle=300 redis.maxActive=600 redis.maxWait=1000 redis.testOnBorrow=true redis.timeout=2000 #redis.password=
The above is the integrated configuration file, where the configuration about redis is the key to the success of integration
Question summary
1. The startup project was reported as an exception after integrating the integration because of the location of the configuration file, so the startup was unsuccessful. Try it several times. The above configuration file can be used.
2. I called the cache tool class before, and it showed that the injection of JedisPool was empty. Injected in the controller and there was a value. Because the way I called the tool class on the controller was new, so the spring lost Jedispool injection when scanning the tool class. It can be solved by using the injection tool class instead in the controller.
For example:
Summarize
The above is the process of Spring integrating redis (jedis) to realize session sharing, I hope it will be helpful to everyone. If you have any questions, please leave me a message and the editor will reply to everyone in time. Thank you very much for your support to Wulin.com website!