I tried integrating spring boot + redis in my application.
The related settings in pom.xml are given below,
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> </dependency>
The main application, as shown below,
@SpringBootApplication @EnableTransactionManagement @ImportResource({"classpath*:applicationContext.xml"}) public class ExamsCenterApplication { public static void main(String[] args) { SpringApplication.run(ExamsCenterApplication.class, args); } }
The contents of applicationContext.xml are as follows:
<?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:mvc="http://www.springframework.org/schema/mvc" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:util="http://www.springframework.org/schema/util" xmlns:mybatis="http://mybatis.org/schema/mybatis-spring" xmlns:p="http://www.springframework.org/schema/p" xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://mybatis.org/schema/mybatis-spring http://mybatis.org/schema/mybatis-spring.xsd"> <bean id="shiroFilter" class="org.apache.shiro.spring.web.ShiroFilterFactoryBean"> <property name="securityManager" ref="securityManager" /> <property name="loginUrl" value="/login.jsp" /> <property name="successUrl" value="/index.jsp" /> <property name="unauthorizedUrl" value="/unauthorized.jsp" /> <property name="filters"> <util:map> <entry key="authc"> <bean class="org.apache.shiro.web.filter.authc.PassThruAuthenticationFilter" /> </entry> </util:map> </property> <property name="filterChainDefinitions"> <value> /marktask/list/ = authc, perms[scoretask:view] /marktask/view/ = authc, perms[scoretask:view] /** = anon </value> </property> </bean> <bean id="examCenter" class="org.apache.tomcat.jdbc.pool.DataSource" destroy-method="close"> <property name="poolProperties"> <bean class="org.apache.tomcat.jdbc.pool.PoolProperties"> <property name="url" value="jdbc:mysql://192.168.100.21:13306/ustudy?characterEncoding=UTF-8&serverTimezone=UTC" /> <property name="driverClassName" value="com.mysql.jdbc.Driver" /> <property name="username" value="root" /> <property name="password" value="mysql" /> <property name="testWhileIdle" value="true" /> <property name="testOnBorrow" value="true" /> <property name="validationInterval" value="30000" /> <property name="testOnReturn" value="false" /> <property name="validationQuery" value="/* ping */" /> <property name="timeBetweenEvictionRunsMillis" value="30000" /> <property name="maxActive" value="100" /> <property name="initialSize" value="10" /> <property name="maxWait" value="10000" /> </bean> </property> </bean> <bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager"> <property name="realm" ref="authRealm" /> </bean> <bean id="lifecycleBeanPostProcessor" class="org.apache.shiro.spring.LifecycleBeanPostProcessor" /> <bean id="authRealm" class="org.apache.shiro.realm.jdbc.JdbcRealm"> <property name="dataSource" ref="examCenter" /> <property name="permissionsLookupEnabled" value="true" /> <property name="authenticationQuery" value="select passwd from ustudy.teacher where teacid = ?" /> <property name="userRolesQuery" value="select role_name from ustudy.teacherroles where teac_id = ?" /> <property name="permissionsQuery" value="select perm from ustudy.perms where role_name = ?" /> </bean> <bean class="org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator" depends-on="lifecycleBeanPostProcessor" /> <bean class="org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor"> <property name="securityManager" ref="securityManager" /> </bean> <bean id="secureRemoteInvocationExecutor" class="org.apache.shiro.spring.remoting.SecureRemoteInvocationExecutor"> <property name="securityManager" ref="securityManager" /> </bean> <tx:annotation-driven transaction-manager="txManager" /> <bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="examCenter" /> </bean> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="examCenter" /> <property name="mapperLocations" value="classpath:com/ustudy/exam/mapping/*.xml"></property> </bean> <mybatis:scan base-package="com.ustudy.exam.mapper" /> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="com.ustudy.exam.dao" /> <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" /> </bean> <bean id="jedisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory" p:use-pool="true" p:database="0" p:host-name="192.168.100.21" p:port="6379" p:timeout="100" /> <bean id="stringSerializer" class="org.springframework.data.redis.serializer.StringRedisSerializer" /> <bean id="jdkSerializer" class="org.springframework.data.redis.serializer.JdkSerializationRedisSerializer" /> <bean id="jsonSerializer" class="org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer" /> <bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate" p:connection-factory-ref="jedisConnectionFactory"> <property name="keySerializer" ref="stringSerializer" /> <property name="valueSerializer" ref="jsonSerializer" /> </bean> <context:annotation-config /> <bean class="org.springframework.session.data.redis.config.annotation.web.http.RedisHttpSessionConfiguration" />
My service code is below:
@Service public class MetaInfo { private static final Logger logger = LogManager.getLogger(MetaInfo.class); @Autowired @Qualifier("redisTemplate") private RedisTemplate<String, Object> redisT; public QuesMeta getMetaTaskInfo(String quesid) { logger.debug("getMetaTaskInfo() hitted"); QuesMeta qm = new QuesMeta("redistest", "auto"); redisT.opsForValue().set("001", qm); if (redisT == null) { logger.debug("getMetaTaskInfo(), redisTemplate is not initialized."); return null; } redisT.opsForValue().set("redistest", "hello"); logger.debug("getMetaTaskInfo(), stored data in redis"); return null; }
Then run the program and get the following exception:
java.lang.NullPointerException: null at com.ustudy.cache.MetaInfo.getMetaTaskInfo(MetaInfo.java:42) ~[classes/:0.0.1-SNAPSHOT] at com.ustudy.exam.controller.MarkTaskController.getMarkTask(MarkTaskController.java:37) ~[classes/:0.0.1-SNAPSHOT] at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_151] at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_151] at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_151] at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_151]
The following code seemed to go wrong.
@Autowired private RedisTemplate<String, String> redisT;
redisT is null and does not initialize as expected.
How can I do to fix this problem?
Thank you very much.