我的主要课程@Service
名为RegionsServiceImpl
。我正在使用ApplicationContext.getBean初始化它,但我想使用@Autowired
。当我这样做时,@Autowired
并没有初始化它。
主要课程:
package com.rebel.shop;
public class JpaRepoTest {
//ApplicationContext ctx;
@Autowired
RegionsServiceImpl regionsServiceImpl;
public JpaRepoTest() {
// ctx = new AnnotationConfigApplicationContext(DataConfig.class);
// regionsServiceImpl = ctx.getBean("regionsServiceImpl", RegionsServiceImpl.class);
}
public static void main(String[] args) {
JpaRepoTest jpaRepoTest = new JpaRepoTest();
jpaRepoTest.testService();
}
private void testService() {
System.out.println(regionsServiceImpl.findById(3l).getName());
}
}
我的服务类:
package com.rebel.shop.persistence.jpa.service;
@Service
public class RegionsServiceImpl implements RegionsService {
@Resource
private RegionsRepository regionsRepository;
@Override
public Regions findById(long id) {
return regionsRepository.findOne(id);
}
}
它的界面:
package com.rebel.shop.persistence.jpa.service;
public interface RegionsService {
public Regions findById(long id);
}
回购:
package com.rebel.shop.persistence.jpa.repository;
public interface RegionsRepository extends JpaRepository<Regions, Long> {
}
Java Config For Spring:
package com.rebel.shop.persistence.jpa.config;
@Configuration
@EnableTransactionManagement
@PropertySource("classpath:app.properties")
@EnableJpaRepositories("com.rebel.shop.persistence.jpa.repository")
@ComponentScan("com.rebel.shop")
public class DataConfig {
private static final String PROP_DATABASE_DRIVER = "db.driver";
private static final String PROP_DATABASE_PASSWORD = "db.password";
private static final String PROP_DATABASE_URL = "db.url";
private static final String PROP_DATABASE_USERNAME = "db.username";
private static final String PROP_HIBERNATE_DIALECT = "db.hibernate.dialect";
private static final String PROP_HIBERNATE_SHOW_SQL = "db.hibernate.show_sql";
private static final String PROP_ENTITYMANAGER_PACKAGES_TO_SCAN = "db.entitymanager.packages.to.scan";
private static final String PROP_HIBERNATE_HBM2DDL_AUTO = "db.hibernate.hbm2ddl.auto";
@Resource
private Environment env;
@Bean
public DataSource dataSource() {
DriverManagerDataSource dataSource = new DriverManagerDataSource();
dataSource.setDriverClassName(env.getRequiredProperty(PROP_DATABASE_DRIVER));
dataSource.setUrl(env.getRequiredProperty(PROP_DATABASE_URL));
dataSource.setUsername(env.getRequiredProperty(PROP_DATABASE_USERNAME));
dataSource.setPassword(env.getRequiredProperty(PROP_DATABASE_PASSWORD));
return dataSource;
}
@Bean
public PlatformTransactionManager transactionManager() {
JpaTransactionManager txManager = new JpaTransactionManager();
txManager.setEntityManagerFactory(entityManagerFactory());
return txManager;
}
@Bean
public HibernateExceptionTranslator hibernateExceptionTranslator() {
return new HibernateExceptionTranslator();
}
@Bean
public EntityManagerFactory entityManagerFactory() {
HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
vendorAdapter.setShowSql(false);
vendorAdapter.setGenerateDdl(false);
LocalContainerEntityManagerFactoryBean factory = new LocalContainerEntityManagerFactoryBean();
factory.setJpaVendorAdapter(vendorAdapter);
factory.setPackagesToScan("com.rebel.shop.persistence.entity");
factory.setDataSource(dataSource());
factory.afterPropertiesSet();
return factory.getObject();
}
// @Bean
// RegionsServiceImpl regionsServiceImpl() {
// return new RegionsServiceImpl();
// }
}
提前致谢!
UPD1:
例外:
Exception in thread "main" java.lang.NullPointerException
at com.rebel.shop.JpaRepoTest.testService(JpaRepoTest.java:33)
at com.rebel.shop.JpaRepoTest.main(JpaRepoTest.java:29)
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:483)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:134)
答案 0 :(得分:0)
你没有使用spring容器,而是使用new实例化对象,因此没有自动装配的spring bean,修改你的bean如下所示
package com.rebel.shop;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(DataConfig.class)
public class JpaRepoTest {
//ApplicationContext ctx;
@Autowired
RegionsServiceImpl regionsServiceImpl;
public JpaRepoTest() {
// ctx = new AnnotationConfigApplicationContext(DataConfig.class);
// regionsServiceImpl = ctx.getBean("regionsServiceImpl", RegionsServiceImpl.class);
}
@Test
public void mainMethod() {
testService();
}
private void testService() {
System.out.println(regionsServiceImpl.findById(3l).getName());
}
}