Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/374.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Don';t work@Autowired注释线程异常;“主要”;java.lang.NullPointerException_Java_Spring Data_Autowired - Fatal编程技术网

Don';t work@Autowired注释线程异常;“主要”;java.lang.NullPointerException

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

我正在使用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 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
中使用它。