在上下文初始化期间遇到异常 - 取消 刷新尝试: org.springframework.beans.factory.UnsatisfiedDependencyException: 创建名称为' categoryDAO'的bean时出错定义于 com.niit.shoppingcart.config.ApplicationContextConfig:不满意 通过方法表达的依赖性' getCategoryDao'参数0: 创建名称为' sessionFactory'的bean时出错定义于 com.niit.shoppingcart.config.ApplicationContextConfig:Bean 通过工厂方法实例化失败;嵌套异常是 org.springframework.beans.BeanInstantiationException:失败 实例化[org.hibernate.SessionFactory]:工厂方法 '提供了getSessionFactory'抛出异常;嵌套异常是 java.lang.NoSuchMethodError: javax.persistence.Table.indexes()[Ljavax /持久/索引;;嵌套 异常是org.springframework.beans.factory.BeanCreationException: 创建名称为' sessionFactory'的bean时出错定义于 com.niit.shoppingcart.config.ApplicationContextConfig:Bean 通过工厂方法实例化失败;嵌套异常是 org.springframework.beans.BeanInstantiationException:失败 实例化[org.hibernate.SessionFactory]:工厂方法 '提供了getSessionFactory'抛出异常;嵌套异常是 java.lang.NoSuchMethodError: javax.persistence.Table.indexes()[Ljavax /持久/索引;例外 in thread" main" org.springframework.beans.factory.UnsatisfiedDependencyException: 创建名称为' categoryDAO'的bean时出错定义于 com.niit.shoppingcart.config.ApplicationContextConfig:不满意 通过方法表达的依赖性' getCategoryDao'参数0: 创建名称为' sessionFactory'的bean时出错定义于 com.niit.shoppingcart.config.ApplicationContextConfig:Bean 通过工厂方法实例化失败;嵌套异常是 org.springframework.beans.BeanInstantiationException:失败 实例化[org.hibernate.SessionFactory]:工厂方法 '提供了getSessionFactory'抛出异常;嵌套异常是 java.lang.NoSuchMethodError: javax.persistence.Table.indexes()[Ljavax /持久/索引;;嵌套 异常是org.springframework.beans.factory.BeanCreationException: 创建名称为' sessionFactory'的bean时出错定义于 com.niit.shoppingcart.config.ApplicationContextConfig:Bean 通过工厂方法实例化失败;嵌套异常是 org.springframework.beans.BeanInstantiationException:失败 实例化[org.hibernate.SessionFactory]:工厂方法 '提供了getSessionFactory'抛出异常;嵌套异常是 java.lang.NoSuchMethodError: javax.persistence.Table.indexes()[Ljavax /持久/索引;在 org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:749) 在 org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:467) 在 org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateUsingFactoryMethod(AbstractAutowireCapableBeanFactory.java:1123) 在 org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1018) 在 org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:510) 在 org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:482) 在 org.springframework.beans.factory.support.AbstractBeanFactory $ 1.getObject(AbstractBeanFactory.java:306) 在 org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230) 在 org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302) 在 org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197) 在 org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:776) 在 org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:861) 在 org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:541) 在com.niit.shoppingcart.CategoryTest.main(CategoryTest.java:13) 引起:org.springframework.beans.factory.BeanCreationException: 创建名称为' sessionFactory'的bean时出错定义于 com.niit.shoppingcart.config.ApplicationContextConfig:Bean 通过工厂方法实例化失败;嵌套异常是 org.springframework.beans.BeanInstantiationException:失败 实例化[org.hibernate.SessionFactory]:工厂方法 '提供了getSessionFactory'抛出异常;嵌套异常是 java.lang.NoSuchMethodError: javax.persistence.Table.indexes()[Ljavax /持久/索引;在 org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:599) 在 org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateUsingFactoryMethod(AbstractAutowireCapableBeanFactory.java:1123) 在 org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1018) 在 org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:510) 在 org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:482) 在 org.springframework.beans.factory.support.AbstractBeanFactory $ 1.getObject(AbstractBeanFactory.java:306) 在 org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230) 在 org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302) 在 org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:202) 在 org.springframework.beans.factory.config.DependencyDescriptor.resolveCandidate(DependencyDescriptor.java:207) 在 org.springframework.beans.factory.support.DefaultListableBeanFactory.findAutowireCandidates(DefaultListableBeanFactory.java:1214) 在 org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1054) 在 org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1019) 在 org.springframework.beans.factory.support.ConstructorResolver.resolveAutowiredArgument(ConstructorResolver.java:835) 在 org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:741) ... 13更多引起: org.springframework.beans.BeanInstantiationException:失败 实例化[org.hibernate.SessionFactory]:工厂方法 '提供了getSessionFactory'抛出异常;嵌套异常是 java.lang.NoSuchMethodError: javax.persistence.Table.indexes()[Ljavax /持久/索引;在 org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:189) 在 org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:588) ... 27更多引起:java.lang.NoSuchMethodError: javax.persistence.Table.indexes()[Ljavax /持久/索引;在 org.hibernate.cfg.annotations.EntityBinder.processComplementaryTableDefinitions(EntityBinder.java:1106) 在 org.hibernate.cfg.AnnotationBinder.bindClass(AnnotationBinder.java:774) 在 org.hibernate.boot.model.source.internal.annotations.AnnotationMetadataSourceProcessorImpl.processEntityHierarchies(AnnotationMetadataSourceProcessorImpl.java:245) 在 org.hibernate.boot.model.process.spi.MetadataBuildingProcess $ 1.processEntityHierarchies(MetadataBuildingProcess.java:222) 在 org.hibernate.boot.model.process.spi.MetadataBuildingProcess.complete(MetadataBuildingProcess.java:265) 在 org.hibernate.boot.model.process.spi.MetadataBuildingProcess.build(MetadataBuildingProcess.java:83) 在 org.hibernate.boot.internal.MetadataBuilderImpl.build(MetadataBuilderImpl.java:418) 在 org.hibernate.boot.internal.MetadataBuilderImpl.build(MetadataBuilderImpl.java:87) 在 org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:691) 在 org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:723) 在 org.springframework.orm.hibernate4.LocalSessionFactoryBuilder.buildSessionFactory(LocalSessionFactoryBuilder.java:372) 在 com.niit.shoppingcart.config.ApplicationContextConfig.getSessionFactory(ApplicationContextConfig.java:56) 在 com.niit.shoppingcart.config.ApplicationContextConfig $$ EnhancerBySpringCGLIB $$ 449f4899.CGLIB $了getSessionFactory $ 1() 在 com.niit.shoppingcart.config.ApplicationContextConfig $$ EnhancerBySpringCGLIB $$ 449f4899 $$ FastClassBySpringCGLIB $$ 7c760c88.invoke() 在 org.springframework.cglib.proxy.MethodProxy.invokeSuper(MethodProxy.java:228) 在 org.springframework.context.annotation.ConfigurationClassEnhancer $ BeanMethodInterceptor.intercept(ConfigurationClassEnhancer.java:356) 在 com.niit.shoppingcart.config.ApplicationContextConfig $$ EnhancerBySpringCGLIB $$ 449f4899.getSessionFactory() at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) 在 sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 在java.lang.reflect.Method.invoke(Method.java:498)at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:162) ......还有28个
这是ApplicationContextConfig.java文件
package com.niit.shoppingcart.config;
import java.util.Properties;
import javax.sql.DataSource;
import org.apache.commons.dbcp2.BasicDataSource;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.orm.hibernate4.HibernateTransactionManager;
import org.springframework.orm.hibernate4.LocalSessionFactoryBuilder;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import com.niit.shoppingcart.dao.CategoryDAO;
import com.niit.shoppingcart.dao.CategoryDAOImpl;
import com.niit.shoppingcart.model.Category;
import com.niit.shoppingcart.model.Product;
import com.niit.shoppingcart.model.Supplier;
import com.niit.shoppingcart.model.UserDetails;
@Configuration
@ComponentScan("com.niit.shoppingcart")
@EnableTransactionManagement
public class ApplicationContextConfig {
@Bean(name = "dataSource")
public DataSource getDataSource() {
BasicDataSource dataSource = new BasicDataSource();
dataSource.setDriverClassName("org.h2.Driver");
dataSource.setUrl("jdbc:h2:tcp://localhost/~/test");
dataSource.setUsername("sa");
dataSource.setPassword("");
return dataSource;
}
private Properties getHibernateProperties() {
Properties properties = new Properties();
properties.put("hibernate.hbm2ddl.auto","update");
properties.put("hibernate.show_sql", "true");
properties.put("hibernate.dialect", "org.hibernate.dialect.MySQLDialect");
return properties;
}
@Autowired
@Bean(name = "sessionFactory")
public SessionFactory getSessionFactory(DataSource dataSource) {
LocalSessionFactoryBuilder sessionBuilder = new LocalSessionFactoryBuilder(dataSource);
sessionBuilder.addProperties(getHibernateProperties());
sessionBuilder.addAnnotatedClasses(Category.class);
sessionBuilder.addAnnotatedClasses(Supplier.class);
sessionBuilder.addAnnotatedClasses(UserDetails.class);
sessionBuilder.addAnnotatedClasses(Product.class);
return sessionBuilder.buildSessionFactory();
}
@Autowired
@Bean(name = "transactionManager")
public HibernateTransactionManager getTransactionManager(
SessionFactory sessionFactory) {
HibernateTransactionManager transactionManager = new HibernateTransactionManager(
sessionFactory);
return transactionManager;
}
@Autowired
@Bean(name = "categoryDAO")
public CategoryDAO getCategorDAO(SessionFactory sessionFactory) {
return new CategoryDAOImpl(sessionFactory);
}
}
这是发生异常的java测试文件
package com.niit.shoppingcart;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import com.niit.shoppingcart.dao.CategoryDAO;
import com.niit.shoppingcart.model.Category;
public class CategoryTest {
public static void main(String[] args) {
AnnotationConfigApplicationContext context=new AnnotationConfigApplicationContext();
context.scan("com.niit.shoppingcart");
context.refresh();
CategoryDAO categoryDAO = (CategoryDAO) context.getBean("categoryDAO");
Category category = (Category) context.getBean("category");
category.setC_id("cg1234");
category.setC_name("phone");
category.setC_desc("This is the first category");
if (categoryDAO.save(category) == true) {
System.out.println("Category created");
} else {
System.out.println("Unable to create category");
}
}
}
答案 0 :(得分:0)
测试类没有正确加载Spring Boot上下文。您所做的就是根据您正在扫描的包com.niit.shoppingcart
加载bean。如果你想"测试"你的应用程序就像你做的那样我会建议像:
@SpringBootApplication
public class TestApplication {
public static void main(String[] args) {
SpringApplication.run(TestApplication.class, args);
}
@Component
public class CategoryTest implements ApplicationRunner {
private CategoryDAO categoryDAO;
// Spring Boot 1.4 autowires on constructors automatically
public CategoryTest(CategoryDAO categoryDAO) {
this.categoryDAO= categoryDAO;
}
public void run(String[] args) {
// This doesn't need to be a bean as it is just a pojo state object
Category category = new Category();
category.setC_id("cg1234");
category.setC_name("phone");
category.setC_desc("This is the first category");
if (categoryDAO.save(category) == true) {
System.out.println("Category created");
} else {
System.out.println("Unable to create category");
}
}
}
}
更好的方法是使用JUnit进行测试。以下是代码看起来像JUnit测试的方式:
@RunWith(SpringRunner.class)
@SpringBootTest
public class CategoryTest {
@Autowired
private CategoryDAO categoryDAO;
@Test
public testSave() {
Category category = new Category();
category.setC_id("cg1234");
category.setC_name("phone");
category.setC_desc("This is the first category");
assertThat(categoryDAO.save(category)).isTrue();
}
}