title: Spring Boot redisTemplate autowiring failed
body: <p>I tried to integrate spring boot + redis into my application.</p>
  
  <p>Related settings in pom.xml is as below,</p>
  
  <pre><code>    &lt;dependency&gt;
          &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
          &lt;artifactId&gt;spring-boot-starter-data-redis&lt;/artifactId&gt;
      &lt;/dependency&gt;
  </code></pre>
  
  <p>Application main is as below,</p>
  
  <pre><code>@SpringBootApplication
  @EnableTransactionManagement
  @ImportResource({"classpath*:applicationContext.xml"})
  public class ExamsCenterApplication {
  
      public static void main(String[] args) {
          SpringApplication.run(ExamsCenterApplication.class, args);
      }
  }
  </code></pre>
  
  <p>Contents of applicationContext.xml is as below,</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;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"&gt;
  
  &lt;bean id="shiroFilter" class="org.apache.shiro.spring.web.ShiroFilterFactoryBean"&gt;
      &lt;property name="securityManager" ref="securityManager" /&gt;
      &lt;property name="loginUrl" value="/login.jsp" /&gt;
      &lt;property name="successUrl" value="/index.jsp" /&gt;
      &lt;property name="unauthorizedUrl" value="/unauthorized.jsp" /&gt;
      &lt;property name="filters"&gt;
          &lt;util:map&gt;
              &lt;entry key="authc"&gt;
                  &lt;bean
                      class="org.apache.shiro.web.filter.authc.PassThruAuthenticationFilter" /&gt;
              &lt;/entry&gt;
          &lt;/util:map&gt;
      &lt;/property&gt;
      &lt;property name="filterChainDefinitions"&gt;
          &lt;value&gt;
              /marktask/list/ = authc, perms[scoretask:view]
              /marktask/view/ = authc, perms[scoretask:view]
  
              /** = anon
          &lt;/value&gt;
      &lt;/property&gt;
  &lt;/bean&gt;
  
  &lt;bean id="examCenter" class="org.apache.tomcat.jdbc.pool.DataSource"
      destroy-method="close"&gt;
      &lt;property name="poolProperties"&gt;
          &lt;bean class="org.apache.tomcat.jdbc.pool.PoolProperties"&gt;
              &lt;property name="url"
                  value="jdbc:mysql://192.168.100.21:13306/ustudy?characterEncoding=UTF-8&amp;amp;serverTimezone=UTC" /&gt;
              &lt;property name="driverClassName" value="com.mysql.jdbc.Driver" /&gt;
              &lt;property name="username" value="root" /&gt;
              &lt;property name="password" value="mysql" /&gt;
              &lt;property name="testWhileIdle" value="true" /&gt;
              &lt;property name="testOnBorrow" value="true" /&gt;
              &lt;property name="validationInterval" value="30000" /&gt;
              &lt;property name="testOnReturn" value="false" /&gt;
              &lt;property name="validationQuery" value="/* ping */" /&gt;
              &lt;property name="timeBetweenEvictionRunsMillis" value="30000" /&gt;
              &lt;property name="maxActive" value="100" /&gt;
              &lt;property name="initialSize" value="10" /&gt;
              &lt;property name="maxWait" value="10000" /&gt;
              &lt;!-- &lt;property name="removeAbandonedTimeout" value="60"/&gt; &lt;property name="minEvictableIdleTimeMillis" 
                  value="30000"/&gt; &lt;property name="minIdle" value="10"/&gt; &lt;property name="logAbandoned" 
                  value="true"/&gt; &lt;property name="removeAbandoned" value="true"/&gt; &lt;property 
                  name="jdbcInterceptors" value="org.apache.tomcat.jdbc.pool.interceptor.ConnectionState;org.apache.tomcat.jdbc.pool.interceptor.StatementFinalizer"/&gt; --&gt;
          &lt;/bean&gt;
      &lt;/property&gt;
  &lt;/bean&gt;
  
  &lt;bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager"&gt;
      &lt;!-- Single realm app. If you have multiple realms, use the 'realms' property 
          instead. --&gt;
      &lt;property name="realm" ref="authRealm" /&gt;
      &lt;!-- By default the servlet container sessions will be used. Uncomment 
          this line to use shiro's native sessions (see the JavaDoc for more): --&gt;
      &lt;!-- &lt;property name="sessionMode" value="native"/&gt; --&gt;
  &lt;/bean&gt;
  &lt;bean id="lifecycleBeanPostProcessor" class="org.apache.shiro.spring.LifecycleBeanPostProcessor" /&gt;
  
  &lt;!-- Define the Shiro Realm implementation you want to use to connect to 
      your back-end --&gt;
  &lt;!-- security datasource: --&gt;
  &lt;bean id="authRealm" class="org.apache.shiro.realm.jdbc.JdbcRealm"&gt;
      &lt;property name="dataSource" ref="examCenter" /&gt;
      &lt;property name="permissionsLookupEnabled" value="true" /&gt;
      &lt;property name="authenticationQuery"
          value="select passwd from ustudy.teacher where teacid = ?" /&gt;
      &lt;property name="userRolesQuery"
          value="select role_name from ustudy.teacherroles where teac_id = ?" /&gt;
      &lt;property name="permissionsQuery"
          value="select perm from ustudy.perms where role_name = ?" /&gt;
  &lt;/bean&gt;
  &lt;bean
      class="org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator"
      depends-on="lifecycleBeanPostProcessor" /&gt;
  &lt;bean
      class="org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor"&gt;
      &lt;property name="securityManager" ref="securityManager" /&gt;
  &lt;/bean&gt;
  
  &lt;!-- Secure Spring remoting: Ensure any Spring Remoting method invocations 
      can be associated with a Subject for security checks. --&gt;
  &lt;bean id="secureRemoteInvocationExecutor"
      class="org.apache.shiro.spring.remoting.SecureRemoteInvocationExecutor"&gt;
      &lt;property name="securityManager" ref="securityManager" /&gt;
  &lt;/bean&gt;
  
  &lt;!-- Noted: Two methods for enabling spring transaction managent with jdbc. 
      Above is XML based configuration. Declaring with @Transactional in java source 
      code is also very flexible. --&gt;
  &lt;tx:annotation-driven transaction-manager="txManager" /&gt;
  &lt;bean id="txManager"
      class="org.springframework.jdbc.datasource.DataSourceTransactionManager"&gt;
      &lt;property name="dataSource" ref="examCenter" /&gt;
  &lt;/bean&gt;
  
  &lt;!-- sql session factory for mybatis --&gt;
  &lt;bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"&gt;
      &lt;property name="dataSource" ref="examCenter" /&gt;
      &lt;property name="mapperLocations" value="classpath:com/ustudy/exam/mapping/*.xml"&gt;&lt;/property&gt;  
  &lt;/bean&gt;
  
  &lt;!-- scan for mappers and make them autowired --&gt;
  &lt;mybatis:scan base-package="com.ustudy.exam.mapper" /&gt;
  &lt;bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"&gt;
      &lt;property name="basePackage" value="com.ustudy.exam.dao" /&gt;
      &lt;property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" /&gt;
  &lt;/bean&gt;
  
  &lt;!-- spring data redis related configurations --&gt;
  &lt;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" /&gt;
  
  &lt;!-- redis template definition --&gt;
  &lt;bean id="stringSerializer"
      class="org.springframework.data.redis.serializer.StringRedisSerializer" /&gt;
  &lt;bean id="jdkSerializer"
      class="org.springframework.data.redis.serializer.JdkSerializationRedisSerializer" /&gt;
  &lt;bean id="jsonSerializer"
      class="org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer" /&gt;
  &lt;bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate"
      p:connection-factory-ref="jedisConnectionFactory"&gt;
      &lt;property name="keySerializer" ref="stringSerializer" /&gt;
      &lt;property name="valueSerializer" ref="jsonSerializer" /&gt;
  &lt;/bean&gt;
  
  &lt;context:annotation-config /&gt;
  &lt;bean
      class="org.springframework.session.data.redis.config.annotation.web.http.RedisHttpSessionConfiguration" /&gt;
  </code></pre>
  
  <p></p>
  
  <p>My service code is as below,</p>
  
  <pre><code>@Service
  public class MetaInfo {
  
      private static final Logger logger = LogManager.getLogger(MetaInfo.class);
  
      @Autowired
      @Qualifier("redisTemplate")
      private RedisTemplate&lt;String, Object&gt; redisT;
  
      /*
       * only need basic information about this question, assign mode. not sure whether other info needed.
       */
      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;
      }
  </code></pre>
  
  <p>Then run the program and got the following exception,</p>
  
  <pre><code>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]
  </code></pre>
  
  <p>It seemed that following code went wrong.</p>
  
  <pre><code>@Autowired
  private RedisTemplate&lt;String, String&gt; redisT;
  </code></pre>
  
  <p>redisT is null and not initialized as expected.</p>
  
  <p>How should I do to fix this issue?</p>
  
  <p>Thanks a lot.</p>