title: Spring autowire byName giving NullPointer exception
body: <p>I am trying to integrate Apache Shiro into my Spring Web MVC project but having problems with appropriate bean definition. Here is my <em>applicationContext.xml</em>:</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:aop="http://www.springframework.org/schema/aop"
         xmlns:tx="http://www.springframework.org/schema/tx"
         xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
         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"&gt;       
  
      &lt;bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"&gt;
          &lt;property name="locations"&gt;
              &lt;list&gt;
                  &lt;value&gt;/WEB-INF/jdbc.properties&lt;/value&gt;    
                  &lt;value&gt;/WEB-INF/wmsauth.properties&lt;/value&gt;  
              &lt;/list&gt;    
          &lt;/property&gt;               
      &lt;/bean&gt;         
  
      &lt;bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"&gt;
          &lt;property name="driverClassName" value="${jdbc.driverClassName}"/&gt;
          &lt;property name="url" value="${jdbc.url}"/&gt;
          &lt;property name="username" value="${jdbc.username}" /&gt;
          &lt;property name="password" value="${jdbc.password}" /&gt;
      &lt;/bean&gt;      
  
      &lt;bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"&gt;
          &lt;property name="dataSource" ref="dataSource"/&gt;  
          &lt;property name="packagesToScan" value="com.smth.smth.model"/&gt;
  
          &lt;property name="hibernateProperties"&gt;
              &lt;props&gt;
                  &lt;prop key="hibernate.dialect"&gt;org.hibernate.dialect.MySQLDialect&lt;/prop&gt;
                  &lt;prop key="hibernate.current_session_context_class"&gt;thread&lt;/prop&gt;
                  &lt;prop key="hibernate.show_sql"&gt;true&lt;/prop&gt;
              &lt;/props&gt;
          &lt;/property&gt;     
      &lt;/bean&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="/login/index"/&gt;         
          &lt;property name="filterChainDefinitions"&gt;
              &lt;value&gt;
                  /login.jsp = authc                  
                  /login/** = authc   
                  /admin/** = authc
                  /logout.htm = logout  
              &lt;/value&gt;
          &lt;/property&gt;
      &lt;/bean&gt;          
  
      &lt;bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager"&gt;            
          &lt;property name="realm" ref="adminRealm"/&gt;
          &lt;property name="cacheManager" ref="cacheManager"/&gt;   
  
      &lt;bean id="lifecycleBeanPostProcessor" class="org.apache.shiro.spring.LifecycleBeanPostProcessor"/&gt; 
  
      &lt;bean id="adminRealm" class="com.smth.smth.model.admin.AdminRealm" autowire="byName"&gt;          
          &lt;property name="credentialsMatcher" ref = "sha256Matcher"/&gt;
          &lt;property name="authenticationQuery" value = "SELECT password, salt FROM admin WHERE email = ?"/&gt;
          &lt;property name="permissionsLookupEnabled" value = "true"/&gt;
          &lt;property name="userRolesQuery" value = "SELECT role_name FROM admin_role WHERE email = ?"/&gt;
          &lt;property name="permissionsQuery" value = "SELECT permission FROM roles_permission WHERE role_name = ?"/&gt;
          &lt;property name="dataSource" ref = "dataSource"/&gt;       
      &lt;/bean&gt;  
  
      &lt;bean id="sha256Matcher" class="org.apache.shiro.authc.credential.Sha256CredentialsMatcher" &gt;
          &lt;property name="storedCredentialsHexEncoded" value = "false"/&gt;
          &lt;property name="hashIterations" value = "1024"/&gt;
      &lt;/bean&gt; 
  
      &lt;bean id="cacheManager" class="org.apache.shiro.cache.MemoryConstrainedCacheManager" /&gt; 
  
      &lt;bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"&gt;
          &lt;property name="maxUploadSize" value="10000000"/&gt;
      &lt;/bean&gt;     
  &lt;/beans&gt;
  </code></pre>
  
  <p>As you can see I have <code>adminRealm</code> bean defined here with <code>autowire = "byName"</code>.</p>
  
  <p><code>AdminRealm</code> Class is as follows:</p>
  
  <pre><code>package com.smth.smth.model.admin;
  
  import com.smth.smth.service.AdminService;
  import org.apache.shiro.authc.AuthenticationException;
  import org.apache.shiro.authc.AuthenticationInfo;
  import org.apache.shiro.authc.AuthenticationToken;
  import org.apache.shiro.authc.SaltedAuthenticationInfo;
  import org.apache.shiro.authc.UsernamePasswordToken;
  import org.apache.shiro.realm.jdbc.JdbcRealm;
  import org.springframework.beans.factory.annotation.Autowired;
  
  public class AdminRealm extends JdbcRealm {
  
      @Autowired
      AdminService adminService;
  
      @Override
      protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {     
          UsernamePasswordToken userPassToken = (UsernamePasswordToken) token;
          final String username = userPassToken.getUsername();
  
          if (username == null) {
              System.out.println("Username is null.");
              return null;
          }            
          final Admin admin = adminService.getByEmail(username);
  
          if (admin == null) {
              System.out.println("User does not exist with principal: [" + username + "]");
              return null;
          }
              SaltedAuthenticationInfo info = new AdminAuthInfo(username, admin.getPassword(), admin.getSalt());
  
          return info;
      }
  }
  </code></pre>
  
  <p>Here we have <code>AdminService</code> which is <code>@Autowired</code> which I was expecting to be handled by Spring dependency injection through the configuration in the <em>applicationContext.xml</em> but I keep getting <code>java.lang.NullPointerException</code> at <code>adminService.getByEmail(username);</code>. Any information would be much appreciated.</p>
  
  <p>I don't think there should be any problem with component scanning. My <code>dispatcher-servlet</code> is as follows:</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:p="http://www.springframework.org/schema/p"
         xmlns:aop="http://www.springframework.org/schema/aop"
         xmlns:tx="http://www.springframework.org/schema/tx"
         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-4.0.xsd
         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/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
         http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"&gt;
  
      &lt;context:component-scan base-package="com.smth.smth"/&gt;
      &lt;mvc:resources mapping="/resources/**" location="/resources/" /&gt;
      &lt;mvc:annotation-driven/&gt;        
  
      &lt;bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping"&gt;
          &lt;property name="mappings"&gt;
              &lt;props&gt;
                  &lt;prop key="index.htm"&gt;indexController&lt;/prop&gt;
              &lt;/props&gt;
          &lt;/property&gt;
      &lt;/bean&gt;
  
      &lt;bean id="viewResolver"
            class="org.springframework.web.servlet.view.InternalResourceViewResolver"
            p:prefix="/WEB-INF/jsp/"
            p:suffix=".jsp" /&gt;
  
      &lt;bean name="indexController"
            class="org.springframework.web.servlet.mvc.ParameterizableViewController"
            p:viewName="index" /&gt;
  
  &lt;/beans&gt;
  </code></pre>