title: Spring dependency injection not working
body: <p>I've got a problem in my standalone Java application. The thing is that I'm trying to Autowire both my Services and my DAOs, but I get a <code>NullPointerException</code> when I invoke service methods from the UI since dependency injection is not working properly. I've tried a lot of things, many of them from similar questions, but the problem is still there. I'm using Spring 4.0.6.RELEASE and Hibernate 4.3.11.Final. Here is my code:</p>
  
  <p>1 - Invocation of service:</p>
  
  <pre><code>public class LoginView {
  
      @Autowired
      private UsuarioService usuarioService;
      ...
      ...
      JButton btnAutenticarse = new JButton("Autenticar");
      btnAutenticarse.addActionListener(new ActionListener() {
          public void actionPerformed(ActionEvent e) {
              try {
                  Usuario usuario = usuarioService.login(textField.getText(),
                          String.valueOf(passwordField.getPassword()), false); // NullPointerException
  
              } catch (InstanceNotFoundException e1) {
       ...
  </code></pre>
  
  <p>2 - Definition of service:</p>
  
  <pre><code>@Service("usuarioService")
  public class UsuarioServiceImpl implements UsuarioService {
  
      @Autowired
      private UsuarioDao usuarioDao;
      ...
  </code></pre>
  
  <p>3 - Definition of DAO:</p>
  
  <pre><code>@Repository("usuarioDao")
  public class UsuarioDaoHibernate extends GenericDaoHibernate &lt;Usuario, Long&gt;
      implements UsuarioDao {
      ...
  </code></pre>
  
  <p>4 - Definition of GenericDAO:</p>
  
  <pre><code>public class GenericDaoHibernate&lt;E, PK extends Serializable&gt; implements
      GenericDao&lt;E, PK&gt; {
  
  @Autowired
  private SessionFactory sessionFactory;
  ....
  </code></pre>
  
  <p>5 - <code>AppConfig.java</code>:</p>
  
  <pre><code>@Configuration
  @ComponentScan(basePackages = "org.example.model")
  public class AppConfig {
  
  @Bean(name = "usuarioService")
  public UsuarioService usuarioService() {
      return new UsuarioServiceImpl();
  }
  
  @Bean(name = "usuarioDao")
  public UsuarioDao usuarioDao() {
      return new UsuarioDaoHibernate();
  }
  </code></pre>
  
  <p>6 - <code>spring-config.xml</code>:</p>
  
  <pre><code>&lt;!-- Enable usage of @Autowired. --&gt;
  &lt;context:annotation-config/&gt;
  
  &lt;!-- Enable component scanning for defining beans with annotations. --&gt;
  &lt;context:component-scan base-package="org.example.model"/&gt;
  
  &lt;!--  For translating native persistence exceptions to Spring's 
        DataAccessException hierarchy. --&gt;
  &lt;bean class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor"/&gt;
  
  &lt;bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"&gt;
      &lt;property name="driverClassName" value="com.mysql.jdbc.Driver" /&gt;
      &lt;property name="url" value="jdbc:mysql://localhost:3306/appdb" /&gt;
      &lt;property name="username" value="username" /&gt;
      &lt;property name="password" value="password"/&gt;
  &lt;/bean&gt;
  
  &lt;bean id="dataSourceProxy" class="org.springframework.jdbc.datasource.LazyConnectionDataSourceProxy"
      p:targetDataSource-ref="dataSource"/&gt;
  
  &lt;bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean" &gt;
      &lt;property name="dataSource" ref="dataSource"/&gt;
      &lt;property name="packagesToScan"&gt;
          &lt;list&gt;
              &lt;value&gt;org.example.model&lt;/value&gt;
          &lt;/list&gt;
      &lt;/property&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.show_sql"&gt;false&lt;/prop&gt;
              &lt;prop key="hibernate.format_sql"&gt;false&lt;/prop&gt;
          &lt;/props&gt;
      &lt;/property&gt;       
  &lt;/bean&gt;
  
  &lt;bean id="transactionManager"  class="org.springframework.orm.hibernate4.HibernateTransactionManager"&gt;
      &lt;property name="sessionFactory" ref="sessionFactory" /&gt;
  &lt;/bean&gt;
  
  &lt;bean id="persistenceExceptionTranslationPostProcessor"
      class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor"/&gt;
  
  &lt;!-- Enable the configuration of transactional behavior based on
       annotations. --&gt;
  &lt;tx:annotation-driven transaction-manager="transactionManager" /&gt;
  </code></pre>