Java – spring boot – environment @ Autowired throws NullPointerException
I have one that uses spring boot 0.5 0.m5 project settings
In one of my profiles, I tried to @ autowire environment, but failed with NullPointerException
This is me so far
Application. java
@EnableAutoConfiguration @Configuration @ComponentScan public class Application { public static void main(String[] args) { SpringApplication.run(Application.class,args); } }
JpaConfig. Where is Java? I tried to @ autowire environment
@Configuration @EnableTransactionManagement @EnableJpaRepositories(basePackages = "com.ui.persistence.repository") public class JpaConfig { private static final String DATABASE_DRIVER = "db.driver"; private static final String DATABASE_PASSWORD = "db.password"; private static final String DATABASE_URL = "db.url"; private static final String DATABASE_USERNAME = "db.username"; private static final String HIBERNATE_DIALECT = "hibernate.dialect"; private static final String HIBERNATE_SHOW_sql = "hibernate.show_sql"; private static final String ENTITYMANAGER_PACKAGES_TO_SCAN = "entitymanager.packages.to.scan"; @Autowired private Environment env; @Bean public DataSource dataSource() { DriverManagerDataSource dataSource = new DriverManagerDataSource(); dataSource.setDriverClassName(env.getProperty(DATABASE_DRIVER)); dataSource.setUrl(env.getProperty(DATABASE_URL)); dataSource.setUsername(env.getProperty(DATABASE_USERNAME)); dataSource.setPassword(env.getProperty(DATABASE_PASSWORD)); return dataSource; } @Bean public LocalContainerEntityManagerfactorybean entityManagerFactory() { LocalContainerEntityManagerfactorybean entityManagerfactorybean = new LocalContainerEntityManagerfactorybean(); entityManagerfactorybean.setDataSource(dataSource()); entityManagerfactorybean.setPersistenceProviderClass( HibernatePersistence.class); entityManagerfactorybean.setPackagesToScan( env.getProperty(ENTITYMANAGER_PACKAGES_TO_SCAN)); entityManagerfactorybean.setJpaProperties(hibernateProperties()); return entityManagerfactorybean; } }
I am trying to load the database properties configured in the properties file However, the environment will not be injected, and the code and NullPointerException fail I don't have any configuration in the XML file
For the property file, I configured the propertysourcesplaceholderconfigurer in this way:
@Configuration @PropertySource("classpath:database.properties") public class PropertyConfig { @Bean public static PropertySourcesPlaceholderConfigurer propertyPlaceHolderConfigurer() { return new PropertySourcesPlaceholderConfigurer(); } }
I've tried to exchange @ Autowired, @ resource and @ inject, but I haven't worked so far Will appreciate any help thank you.
Solution
I believe spring and entitymanagerfactory have some life cycle problems. You may already have these errors (fixed in 4.0.0.rc1) – if your @ configuration class is timed out in advance, it may not meet the conditions of automatic assembly If so, you can learn from the log output
Just out of interest, did you know that if you use @ enableautoconfiguration (as long as your @ componentscan package defines your repository), the functions provided by your jpaconfig and propertyconfig have been preset to be out of the box? Take JPA sample in spring boot as an example