我正在尝试将应用程序与我的数据库连接,但是当我尝试运行它时,我收到此错误:
创建名为'barcodeIngDao'的bean时出错:通过方法setSessionFactory'参数0表示不满意的依赖关系;嵌套异常是org.springframework.beans.factory.UnsatisfiedDependencyException:创建在server.Application中定义名为'sessionFactory'的bean时出错:通过方法'sessionFactory'参数0表示不满意的依赖关系;嵌套异常是org.springframework.beans.factory.BeanCurrentlyInCreationException:创建名为'sessionFactory'的bean时出错:请求的bean当前正在创建:是否存在无法解析的循环引用?
barcodeIngDao只是一个带有@Repository的类,它扩展了GenericDaoHibernate:
package server.util;
import java.io.Serializable;
import java.lang.reflect.ParameterizedType;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import server.exceptions.InstanceNotFoundException;
public class GenericDaoHibernate<E, PK extends Serializable> implements
GenericDao<E, PK> {
private SessionFactory sessionFactory;
private Class<E> entityClass;
@SuppressWarnings("unchecked")
public GenericDaoHibernate() {
this.entityClass = (Class<E>) ((ParameterizedType) getClass()
.getGenericSuperclass()).getActualTypeArguments()[0];
}
@Autowired
public void setSessionFactory(SessionFactory sessionFactory) {
this.sessionFactory = sessionFactory;
}
protected Session getSession() {
return sessionFactory.getCurrentSession();
}
public void save(E entity) {
getSession().saveOrUpdate(entity);
}
public E find(PK id) throws InstanceNotFoundException {
E entity = (E) getSession().get(entityClass, id);
if (entity == null) {
throw new InstanceNotFoundException(id, entityClass.getName());
}
return entity;
}
public void remove(PK id) throws InstanceNotFoundException {
getSession().delete(find(id));
}
}
这是我的Application.java:
package server;
import javax.persistence.EntityManagerFactory;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.orm.jpa.vendor.HibernateJpaSessionFactoryBean;
@SpringBootApplication(exclude=HibernateJpaAutoConfiguration.class)
public class Application {
@Bean
public HibernateJpaSessionFactoryBean sessionFactory(EntityManagerFactory emf) {
HibernateJpaSessionFactoryBean fact = new HibernateJpaSessionFactoryBean();
fact.setEntityManagerFactory(emf);
return fact;
}
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
这是我的application.properties:
spring.datasource.url=jdbc:postgresql://localhost:5432/AppRecipe?useSSL=false
spring.datasource.username=uibRT
spring.datasource.password=uibRT
spring.datasource.driver-class-name=org.postgresql.Driver
spring.jpa.properties.hibernate.current_session_context_class=org.springframework.orm.hibernate5.SpringSessionContext
据我所知,我的Application.java与@SpringBootApplication中的@ComponentScan一起尝试为@Repository创建barcodeIngDao的bean,而barcodeIngDao扩展了GenericDaoHibernate,因此当应用程序尝试为SessionFactory创建bean时.java它仍然在扫描步骤中,所以它还没有为SessionFactory创建bean。
问题是我不知道是否有办法在扫描所有类之前为SessionFactory创建bean,或者如果我做错了什么并且它不是我之前所说的
修改 如果我试着这样做:
package server.barcodeing;
import javax.persistence.EntityManagerFactory;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import server.util.GenericDaoHibernate;
@Repository("barcodeIngDao")
public class BarcodeIngDaoHibernate extends
GenericDaoHibernate<BarcodeIng, Long> implements BarcodeIngDao {
@Autowired
private EntityManagerFactory entityManagerFactory;
public SessionFactory getSessionFactory(){
return this.entityManagerFactory.unwrap(SessionFactory.class);
}
}
我收到此错误:
创建名为'barcodeIngDao'的bean时出错:通过方法'setSessionFactory'参数0表示不满意的依赖关系;嵌套异常是org.springframework.beans.factory.NoSuchBeanDefinitionException:没有类型'org.hibernate.SessionFactory'的限定bean可用:预计至少有1个bean可以作为autowire候选者。
EDIT2
如果删除Application.java中的exclude = HibernateJpaAutoConfiguration.class,我会收到此错误:
创建名为'org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration'的bean时出错:通过构造函数参数0表示不满意的依赖关系;嵌套异常是org.springframework.beans.factory.NoSuchBeanDefinitionException:没有'javax.sql.DataSource'类型的限定bean可用:预计至少有一个bean可以作为autowire候选者。
EDIT3
如果我将Application.java中的SessionFactory方法替换为:
@Bean
public SessionFactory sessionFactory(@Qualifier("entityManagerFactory") EntityManagerFactory emf) {
return emf.unwrap(SessionFactory.class);
}
我收到此错误:
创建名为'barcodeIngDao'的bean时出错:通过方法'setSessionFactory'参数0表示不满意的依赖关系;嵌套异常是org.springframework.beans.factory.UnsatisfiedDependencyException:创建在server.Application中定义名为'sessionFactory'的bean时出错:通过方法'sessionFactory'参数0表示不满意的依赖关系;嵌套异常是org.springframework.beans.factory.NoSuchBeanDefinitionException:没有类型为'javax.persistence.EntityManagerFactory'的限定bean可用:预期至少有1个bean可以作为autowire候选者。 依赖注释:{@ org.springframework.beans.factory.annotation.Qualifier (值=的entityManagerFactory)}
Edit4
在将此依赖项放入pom之后使用edit和edit2,我可以运行服务器:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
问题现在是我尝试执行请求时出现此错误:
servlet [dispatcherServlet]的Servlet.service()与path []的上下文发生异常[请求处理失败;嵌套异常是java.lang.NullPointerException],其根本原因是java.lang.NullPointerException,位于server.util.GenericDaoHibernate.getSession(GenericDaoHibernate.java:33)
完整错误:
Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is java.lang.NullPointerException] with root cause java.lang.NullPointerException
at server.util.GenericDaoHibernate.getSession(GenericDaoHibernate.java:33)
at server.util.GenericDaoHibernate.find(GenericDaoHibernate.java:41)
at server.util.GenericDaoHibernate$$FastClassBySpringCGLIB$$eda6dc96.invoke(<generated>)
at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:204)
at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:721)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:157)
at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:136)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:656)
at server.recipe.RecipeDaoHibernate$$EnhancerBySpringCGLIB$$1857692b.find(<generated>)
at server.recipeservice.RecipeServiceImpl.findRecipeById(RecipeServiceImpl.java:26)
at server.recipeservice.RecipeServiceImpl$$FastClassBySpringCGLIB$$1b7b075.invoke(<generated>)
at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:204)
at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:721)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:157)
at org.springframework.transaction.interceptor.TransactionInterceptor$1.proceedWithInvocation(TransactionInterceptor.java:99)
at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:282)
at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:96)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:656)
at server.recipeservice.RecipeServiceImpl$$EnhancerBySpringCGLIB$$14ef69a4.findRecipeById(<generated>)
at server.controllers.RecipeController.recipeId(RecipeController.java:33)
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:498)
at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:205)
at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:133)
at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:116)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:827)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:738)
at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:85)
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:963)
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:897)
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:970)
at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:861)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:635)
at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:846)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:742)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.springframework.web.filter.RequestContextFilter.doFilterInternal(RequestContextFilter.java:99)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.springframework.web.filter.HttpPutFormContentFilter.doFilterInternal(HttpPutFormContentFilter.java:105)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.springframework.web.filter.HiddenHttpMethodFilter.doFilterInternal(HiddenHttpMethodFilter.java:81)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:197)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:199)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:478)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:140)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:81)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:87)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:342)
at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:803)
at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66)
at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:868)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1459)
at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Thread.java:745)
答案 0 :(得分:1)
您可以尝试在每个DAO类中展开SessionFactory。
它为我工作..
@Autowired private EntityManagerFactory entityManagerFactory;
public SessionFactory getSessionFactory(){
return this.entityManagerFactory.unwrap(SessionFactory.class);
}
您正试图在setSessionFactory方法中生成@autowired
并抛出
通过方法&#39; setSessionFactory&#39;
表达不满意的依赖关系删除它并注释SessionFactory Bean定义。
我发现你没有使用Jpa自动配置
@SpringBootApplication(exclude=HibernateJpaAutoConfiguration.class)
也删除了排除参数。
编辑2: 在MainClass中添加它
@PropertySource({"classpath:application.properties"})
是可行的,它不会读取application.properties
文件。
答案 1 :(得分:0)
您应该返回SessionFactory
个实例而不是HibernateJpaSessionFactoryBean
。尝试按如下方式替换会话工厂方法。
@Bean
public SessionFactory sessionFactory(@Qualifier("entityManagerFactory") EntityManagerFactory emf) {
return emf.unwrap(SessionFactory.class);
}
Here是相关文档。
答案 2 :(得分:0)
我终于解决了删除GenericDaoHibernate并将barcodeIngDao的扩展名更改为CrudRepository的问题:
public interface BarcodeIngRepository extends CrudRepository<BarcodeIng, Long>{
}