我只想说我是Spring,JPA和hibernate的新手。所以我尽我所能。另外,我使用的是基于Java的配置,所以这里没有使用xml。
所以,我正在尝试在PersonDAOJpa类上运行集成测试。当我运行集成测试时,我收到bean“jpaDao”的NoSuchBeanDefinitionException错误。我已经在DatabaseConfig类中定义了bean。那么,即使我已经定义了它,为什么我会收到此错误?
以下是与此问题相关的代码和错误跟踪:
错误追踪:
org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'jpaDao' is defined
at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeanDefinition(DefaultListableBeanFactory.java:687)
at org.springframework.beans.factory.support.AbstractBeanFactory.getMergedLocalBeanDefinition(AbstractBeanFactory.java:1168)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:281)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:199)
at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:962)
at com.orgchart.PersonServiceJPAIntegrationTest.<init>(PersonServiceJPAIntegrationTest.java:34)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
at java.lang.reflect.Constructor.newInstance(Unknown Source)
at org.junit.runners.BlockJUnit4ClassRunner.createTest(BlockJUnit4ClassRunner.java:217)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.createTest(SpringJUnit4ClassRunner.java:199)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner$1.runReflectiveCall(SpringJUnit4ClassRunner.java:259)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.methodBlock(SpringJUnit4ClassRunner.java:261)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:219)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:83)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:68)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:163)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:675)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)
DatabaseConfig:
@Configuration
@PropertySource("classpath:application.properties")
@EnableTransactionManagement
public class DatabaseConfig{
@Bean(name="jpaDao")
public PersonDAOJpa jpadao(){
return new PersonDAOJpa();
}
@Bean(name="jdbcDao")
public PersonDAOJdbc dao(){
return new PersonDAOJdbc();
}
@Bean
public JdbcTemplate jdbc(DataSource ds){
JdbcTemplate jdbc= new JdbcTemplate(dataSource());
return jdbc;
}
@Bean
public PersonService ps(){
PersonService ps= new PersonServiceImpl();
return ps;
}
@Bean(name="ds")
public DataSource dataSource() {
DriverManagerDataSource ds = new DriverManagerDataSource();
ds.setDriverClassName("com.mysql.jdbc.Driver");
ds.setUrl("jdbc:mysql://localhost:3306/------");
ds.setUsername(" ");
ds.setPassword(" ");
return ds;
}
@Bean
public LocalContainerEntityManagerFactoryBean entityManagerFactoryBean() {
LocalContainerEntityManagerFactoryBean em =
new LocalContainerEntityManagerFactoryBean();
em.setDataSource(dataSource());
em.setPackagesToScan(new String[] {"com.orgchart"});
JpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
em.setJpaVendorAdapter(vendorAdapter);
em.setJpaProperties(additionalProperties());
return em;
}
@Bean
public PlatformTransactionManager transactionManager() {
JpaTransactionManager transactionManager = new JpaTransactionManager();
transactionManager.setEntityManagerFactory(entityManagerFactoryBean()
.getObject());
return transactionManager;
}
@Bean
public PersistenceExceptionTranslationPostProcessor exceptionTranslation() {
return new PersistenceExceptionTranslationPostProcessor();
}
Properties additionalProperties() {
return new Properties() {
private static final long serialVersionUID = 1L;
{ // Hibernate Specific:
setProperty("hibernate.hbm2ddl.auto", "update");
setProperty("hibernate.dialect",
"org.hibernate.dialect.MySQL5Dialect");
}
};
}
}
IntegrationTest:
@ContextConfiguration(classes = {IntegrationTestConfig.class})
@RunWith(SpringJUnit4ClassRunner.class)
public class PersonServiceJPAIntegrationTest {
ApplicationContext context= new AnnotationConfigApplicationContext(DatabaseConfig.class);
PersonDAO personDao = context.getBean("jpaDao", PersonDAO.class);
PersonServiceImpl ps = new PersonServiceImpl(personDao);
@Before
public void setUp() throws Exception {
}
@After
public void tearDown() throws Exception {
}
......
}
PersonDAOJpa:
@Component("jpaDao")
public class PersonDAOJpa implements PersonDAO{
@PersistenceContext
private EntityManager em;
public PersonDAOJpa(){
}
public EntityManager getEm() {
return em;
}
public void setEm(EntityManager em) {
this.em = em;
}
.....
}
**更新:
IntegrationTestConfig:
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
@Configuration
@ComponentScan
public class IntegrationTestConfig {
}
此外,我的应用程序中有一个服务层,我只是没有在下面添加它,因为我不想添加不相关的信息。但如果您确实需要它或者您需要更多代码或信息,我会添加它。
答案 0 :(得分:0)
问题在于您的.bat
注释。由于您尚未定义basepackages(要扫描的包),因此它将测试的当前包用作目标包。
将其更改为:
@ComponentScan
让它发挥作用。
答案 1 :(得分:0)
更改Bean定义以返回 PersonDAO
界面@Bean(name="jpaDao")
public PersonDAO jpadao(){
return new PersonDAOJpa();
}
这一行
PersonDAO personDao = context.getBean("jpaDao", PersonDAO.class);
将找到正确的bean类型。