2015-09-02 70 views
1

我正在處理spring數據,我使用@Bean,@Entity和Main.java創建了配置類,但是當運行項目時我收到異常:不要工作@Autowired註釋線程「main」中的異常java.lang.NullPointerException

Exception in thread "main" java.lang.NullPointerException 

@Autowired註解不起作用!

Main.java

public class Main { 

    @Autowired 
    private static TodoRepository todoRepository; 

    public static void main(String[] args) { 
     Todo todo = new Todo(); 
     todo.setId(1l); 
     todo.setTitle("title"); 
     System.out.println(todoRepository); //null 
     todoRepository.save(todo);   //Exception in thread "main" java.lang.NullPointerException 
    } 
} 

Context類

@Configuration 
@EnableJpaRepositories(basePackages = {"repository"}, 
     entityManagerFactoryRef = "entityManagerFactory", 
     transactionManagerRef = "transactionManager") 
@EnableTransactionManagement 
@PropertySource("classpath:app.properties") 
public class PersistenceContext { 

    public PersistenceContext() { 
    } 

    /** 
    * The method that configures the datasource bean 
    * */ 

    @Resource 
    private Environment env; 

    @Bean(destroyMethod = "close") 
    DataSource dataSource() { 
     HikariConfig dataSourceConfig = new HikariConfig(); 
     dataSourceConfig.setJdbcUrl(env.getRequiredProperty("db.url")); 
     dataSourceConfig.setDriverClassName(env.getRequiredProperty("db.driver")); 
     dataSourceConfig.setUsername(env.getRequiredProperty("db.username")); 
     dataSourceConfig.setPassword(env.getRequiredProperty("db.password")); 
     return new HikariDataSource(dataSourceConfig); 
    } 

    /** 
    * The method that configures the entity manager factory 
    * */ 
    @Bean 
    LocalContainerEntityManagerFactoryBean entityManagerFactory(DataSource dataSource, Environment env) { 
     LocalContainerEntityManagerFactoryBean entityManagerFactoryBean = new LocalContainerEntityManagerFactoryBean(); 
     entityManagerFactoryBean.setDataSource(dataSource); 
     entityManagerFactoryBean.setJpaVendorAdapter(new HibernateJpaVendorAdapter()); 
     entityManagerFactoryBean.setPackagesToScan("entity"); 

     Properties jpaProperties = new Properties(); 

     jpaProperties.put("hibernate.dialect", env.getRequiredProperty("hibernate.dialect")); 
     jpaProperties.put("hibernate.hbm2ddl.auto", env.getRequiredProperty("hibernate.hbm2ddl.auto")); 
     jpaProperties.put("hibernate.show_sql", env.getRequiredProperty("hibernate.show_sql")); 
     jpaProperties.put("hibernate.format_sql", env.getRequiredProperty("hibernate.format_sql")); 

     entityManagerFactoryBean.setJpaProperties(jpaProperties); 

     return entityManagerFactoryBean; 
    } 

    /** 
    * The method that configures the transaction manager 
    * */ 
    @Bean 
    JpaTransactionManager transactionManager(EntityManagerFactory entityManagerFactory) { 
     JpaTransactionManager transactionManager = new JpaTransactionManager(); 
     transactionManager.setEntityManagerFactory(entityManagerFactory); 
     return transactionManager; 
    } 
} 

public interface TodoRepository extends CrudRepository<Todo, Long> { 

} 

堆棧跟蹤

null 
Exception in thread "main" java.lang.NullPointerException 
    at Main.main(Main.java:28) 
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) 
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 
    at java.lang.reflect.Method.invoke(Method.java:497) 
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:140) 

Process finished with exit code 1 
+1

「TodoRepository」的實現在哪裏,您希望自動裝配什麼? –

+0

沒有實現'TodoRepository'並且我想要使用overide方法,我想要使用'TodoRepository'對象來使用替代方法 –

+0

不知道你想要做什麼,但是刪除那個'static'關鍵字。您無法自動調用靜態成員。不要在'main'中輸入'static',以便可以使用它。 –

回答

0

你的主類是不是一個管理的Spring bean。您需要創建一個ApplicationContext,如下所示:

public class Main { 
    public static void main(String[] args) {public static void main(String[] args) { 
     ApplicationContext ctx = new AnnotationConfigApplicationContext(PersistenceContext.class); 
     TodoRepository todoRepository = ctx.getBean(TodoRepository.class); 
     Todo todo = new Todo(); 
     todo.setId(1l); 
     todo.setTitle("title"); 
     System.out.println(todoRepository); // not null 
     todoRepository.save(todo); 
    } 
} 
+0

我使用'ApplicationContext'工作過版本,我需要使用'@ Autowired'版本 –

+0

afaik這或多或少是正確答案 – ConMan

+0

我需要'@ Autowired'的具體實現。我用'ApplicationContext'工作過代碼。 –

相關問題