Don';t work@Autowired注释线程异常;“主要”;java.lang.NullPointerException
我正在使用spring数据,我使用@Bean、@Entity和Main.java创建配置类,但在运行project时,我遇到异常:Don';t work@Autowired注释线程异常;“主要”;java.lang.NullPointerException,java,spring-data,autowired,Java,Spring Data,Autowired,我正在使用spring数据,我使用@Bean、@Entity和Main.java创建配置类,但在运行project时,我遇到异常: Exception in thread "main" java.lang.NullPointerException @Autowired注释无效 Main.java public class Main { @Autowired private static TodoRepository todoRepository; public st
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
}
}
上下文类
@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> {
}
存储库
@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> {
}
您的主类不是托管Springbean。您需要创建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);
}
}
您的主类不是托管Springbean。您需要创建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);
}
}
我使用的是
ApplicationContext
版本,我需要使用@Autowired
版本,我需要使用@Autowired
的具体工具。我用ApplicationContext
编写过代码。我用ApplicationContext
编写过版本,我需要用@Autowired
编写版本,我需要用@Autowired
具体实现。我使用过ApplicationContext
编写代码。TodoRepository
的实现在哪里,您希望自动连线什么?没有实现TodoRepository
并且我想使用overide方法,我想获取TodoRepository
对象以用于overide方法,直到不知道您正在尝试做什么,但是删除这个静态
关键字。无法自动关联静态成员。不要键入某个static
以便可以在main
中使用它。TodoRepository
的实现在哪里,您希望自动连接什么?没有实现TodoRepository
,我想使用overide方法,我想获取TodoRepository
对象,以便在IDE方法上使用,直到不知道您要执行的操作为止,但请删除该static
关键字。无法自动关联静态成员。不要键入某个static
,只是为了在main
中使用它。