我在哪里弄错'创建名称为'userService'的bean时出错:自动连接依赖项的注入失败'?

时间:2019-03-02 13:04:54

标签: spring hibernate

我使用基于xml的配置将冬眠的Spring和jsf集成到我的项目中。我在做什么?这是我的控制器,服务类,applicationcontext和控制台错误消息。

这是userService类

package com.namvertech.atasis.service.impl;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.namvertech.atasis.entity.User;
import com.namvertech.atasis.service.IUserService;

@Component
public class UserService implements IUserService {

@Autowired
private SessionFactory sessionFactory;


public SessionFactory getSessionFactory() {
    return sessionFactory;
}


public void setSessionFactory(SessionFactory sessionFactory) {
    this.sessionFactory = sessionFactory;
}


@Transactional
public void register(User emp){
    // Acquire session
    Session session = sessionFactory.getCurrentSession();
    // Save employee, saving behavior get done in a transactional manner
    session.save(emp);
}
}

她是userController类

package com.namvertech.atasis.controller;

import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;

import com.namvertech.atasis.entity.User;
import com.namvertech.atasis.service.impl.UserService;

@ManagedBean
@SessionScoped
public class UserController  {

@ManagedProperty("#{userService}")
private UserService userService;

private User user = new User();

public UserService getUserService() {
    return userService;
}

public void setUserService(UserService userService) {
    this.userService = userService;
}

public User getUser() {
    return user;
}

public void setUser(User user) {
    this.user = user;
}

public String register() {
    // Calling Business Service
    userService.register(user);
    // Add message
    FacesContext.getCurrentInstance().addMessage(null, 
            new FacesMessage("The Employee "+this.user.getFirstName()+" Is Registered Successfully"));
    return "";
}

}

这是applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/context  http://www.springframework.org/schema/context/spring-context-3.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd">
<!-- Enable Spring Annotation Configuration -->
<context:annotation-config />
<!-- Scan for all of Spring components such as Spring Service -->
<context:component-scan base-package="com.namvertech.atasis.controller"></context:component-scan>
<context:component-scan base-package="com.namvertech.atasis.service.impl"></context:component-scan>
<!-- Create Data Source bean -->

<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
    destroy-method="close">
    <property name="driverClassName" value="oracle.jdbc.OracleDriver" />
    <property name="url" value="jdbc:oracle:thin:@//localhost:1521/ORACLE" />
    <property name="username" value="atasis" />
    <property name="password" value="gtveren45" />
</bean>
<!-- Define SessionFactory bean -->
<bean id="sessionFactory"
    class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
    <property name="dataSource" ref="dataSource" />
    <property name="mappingResources">
        <list>
            <value>domain-classes.hbm.xml</value>
        </list>
    </property>
    <property name="configLocation">
        <value>classpath:hibernate.cfg.xml</value>
    </property>
</bean>
<!-- Transaction Manager -->
<bean id="transactionManager"
    class="org.springframework.orm.hibernate3.HibernateTransactionManager">
    <property name="sessionFactory" ref="sessionFactory" />
</bean>
<!-- Detect @Transactional Annotation -->
<tx:annotation-driven transaction-manager="transactionManager" />
</beans>

这是错误消息

SEVERE: Context initialization failed
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'userService': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private org.hibernate.SessionFactory com.namvertech.atasis.service.impl.UserService.sessionFactory; nested exception is java.lang.NoClassDefFoundError: [Lorg/hibernate/engine/FilterDefinition;
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:292)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1185)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:537)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:475)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:304)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:228)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:300)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:195)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:703)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:760)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:482)
at org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:403)
at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:306)
at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:106)
at weblogic.servlet.internal.EventsManager$FireContextListenerAction.run(EventsManager.java:705)
at weblogic.security.acl.internal.AuthenticatedSubject.doAs(AuthenticatedSubject.java:326)
at weblogic.security.service.SecurityManager.runAsForUserCode(SecurityManager.java:197)
at weblogic.servlet.provider.WlsSecurityProvider.runAsForUserCode(WlsSecurityProvider.java:203)
at weblogic.servlet.provider.WlsSubjectHandle.run(WlsSubjectHandle.java:71)
at weblogic.servlet.internal.EventsManager.executeContextListener(EventsManager.java:251)
at weblogic.servlet.internal.EventsManager.notifyContextCreatedEvent(EventsManager.java:204)
at weblogic.servlet.internal.EventsManager.notifyContextCreatedEvent(EventsManager.java:189)
at weblogic.servlet.internal.WebAppServletContext.preloadResources(WebAppServletContext.java:1921)
at weblogic.servlet.internal.WebAppServletContext.start(WebAppServletContext.java:3101)
at weblogic.servlet.internal.WebAppModule.startContexts(WebAppModule.java:1843)
at weblogic.servlet.internal.WebAppModule.start(WebAppModule.java:884)
at weblogic.application.internal.ExtensibleModuleWrapper$StartStateChange.next(ExtensibleModuleWrapper.java:360)
at weblogic.application.internal.ExtensibleModuleWrapper$StartStateChange.next(ExtensibleModuleWrapper.java:356)
at weblogic.application.utils.StateMachineDriver.nextState(StateMachineDriver.java:45)
at weblogic.application.internal.ExtensibleModuleWrapper.start(ExtensibleModuleWrapper.java:138)
at weblogic.application.internal.flow.ModuleListenerInvoker.start(ModuleListenerInvoker.java:124)
at weblogic.application.internal.flow.ModuleStateDriver$3.next(ModuleStateDriver.java:233)
at weblogic.application.internal.flow.ModuleStateDriver$3.next(ModuleStateDriver.java:228)
at weblogic.application.utils.StateMachineDriver.nextState(StateMachineDriver.java:45)
at weblogic.application.internal.flow.ModuleStateDriver.start(ModuleStateDriver.java:78)
at weblogic.application.internal.flow.StartModulesFlow.activate(StartModulesFlow.java:52)
at weblogic.application.internal.BaseDeployment$2.next(BaseDeployment.java:752)
at weblogic.application.utils.StateMachineDriver.nextState(StateMachineDriver.java:45)
at weblogic.application.internal.BaseDeployment.activate(BaseDeployment.java:262)
at weblogic.application.internal.EarDeployment.activate(EarDeployment.java:66)
at weblogic.application.internal.DeploymentStateChecker.activate(DeploymentStateChecker.java:165)
at weblogic.deploy.internal.targetserver.AppContainerInvoker.activate(AppContainerInvoker.java:90)
at weblogic.deploy.internal.targetserver.operations.AbstractOperation.activate(AbstractOperation.java:631)
at weblogic.deploy.internal.targetserver.operations.ActivateOperation.activateDeployment(ActivateOperation.java:171)
at weblogic.deploy.internal.targetserver.operations.ActivateOperation.doCommit(ActivateOperation.java:121)
at weblogic.deploy.internal.targetserver.operations.StartOperation.doCommit(StartOperation.java:151)
at weblogic.deploy.internal.targetserver.operations.AbstractOperation.commit(AbstractOperation.java:348)
at weblogic.deploy.internal.targetserver.DeploymentManager.handleDeploymentCommit(DeploymentManager.java:907)
at weblogic.deploy.internal.targetserver.DeploymentManager.activateDeploymentList(DeploymentManager.java:1468)
at weblogic.deploy.internal.targetserver.DeploymentManager.handleCommit(DeploymentManager.java:459)
at weblogic.deploy.internal.targetserver.DeploymentServiceDispatcher.commit(DeploymentServiceDispatcher.java:181)
at weblogic.deploy.service.internal.targetserver.DeploymentReceiverCallbackDeliverer.doCommitCallback(DeploymentReceiverCallbackDeliverer.java:217)
at weblogic.deploy.service.internal.targetserver.DeploymentReceiverCallbackDeliverer.access$100(DeploymentReceiverCallbackDeliverer.java:14)
at weblogic.deploy.service.internal.targetserver.DeploymentReceiverCallbackDeliverer$2.run(DeploymentReceiverCallbackDeliverer.java:69)
at weblogic.work.SelfTuningWorkManagerImpl$WorkAdapterImpl.run(SelfTuningWorkManagerImpl.java:670)
at weblogic.invocation.ComponentInvocationContextManager._runAs(ComponentInvocationContextManager.java:352)
at weblogic.invocation.ComponentInvocationContextManager.runAs(ComponentInvocationContextManager.java:337)
at weblogic.work.LivePartitionUtility.doRunWorkUnderContext(LivePartitionUtility.java:57)
at weblogic.work.PartitionUtility.runWorkUnderContext(PartitionUtility.java:41)
at weblogic.work.SelfTuningWorkManagerImpl.runWorkUnderContext(SelfTuningWorkManagerImpl.java:644)
at weblogic.work.ExecuteThread.execute(ExecuteThread.java:415)
at weblogic.work.ExecuteThread.run(ExecuteThread.java:355)
Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: private org.hibernate.SessionFactory com.namvertech.atasis.service.impl.UserService.sessionFactory; nested exception is java.lang.NoClassDefFoundError: [Lorg/hibernate/engine/FilterDefinition;
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:508)
at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:87)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:289)
... 61 more
 Caused by: java.lang.NoClassDefFoundError: 
[Lorg/hibernate/engine/FilterDefinition;
at java.lang.Class.getDeclaredFields0(Native Method)
at java.lang.Class.privateGetDeclaredFields(Class.java:2583)
at java.lang.Class.getDeclaredFields(Class.java:1916)
at 

这是我的pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 
http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.namver</groupId>
<artifactId>SpringBootProject</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<name>SpringBootProject</name>
<description>Demo project for Spring Boot</description>


<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <configuration>
                <source>1.6</source>
                <target>1.6</target>
            </configuration>
        </plugin>
    </plugins>
</build>
<url>http://maven.apache.org</url>

<dependencies>
    <!-- Servlet -->
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>servlet-api</artifactId>
        <version>2.5</version>
        <scope>provided</scope>
    </dependency>
    <!-- Faces Implementation -->
    <dependency>
        <groupId>com.sun.faces</groupId>
        <artifactId>jsf-impl</artifactId>
        <version>2.2.4</version>
    </dependency>
    <!-- Faces Library -->
    <dependency>
        <groupId>com.sun.faces</groupId>
        <artifactId>jsf-api</artifactId>
        <version>2.2.4</version>
    </dependency>
    <!-- JSP Library -->
    <dependency>
        <groupId>javax.servlet.jsp</groupId>
        <artifactId>javax.servlet.jsp-api</artifactId>
        <version>2.3.1</version>
    </dependency>
    <!-- JSTL Library -->
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>jstl</artifactId>
        <version>1.1.2</version>
    </dependency>
    <!-- Primefaces -->
    <dependency>
        <groupId>org.primefaces</groupId>
        <artifactId>primefaces</artifactId>
        <version>6.1</version>
    </dependency>
    <dependency>
        <groupId>org.primefaces.omegamenu</groupId>
        <artifactId>omega-menu</artifactId>
        <version>1.1.4</version>
    </dependency>
    <!-- Hibernate library -->
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-core</artifactId>
        <version>4.3.6.Final</version>
    </dependency>
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-entitymanager</artifactId>
        <version>4.3.6.Final</version>
    </dependency>
    <!-- Spring ORM -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-orm</artifactId>
        <version>4.0.3.RELEASE</version>
    </dependency>
    <!-- Spring Web -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-webmvc</artifactId>
        <version>4.0.3.RELEASE</version>
    </dependency>
    <!-- Required By Hibernate -->
    <dependency>
        <groupId>commons-dbcp</groupId>
        <artifactId>commons-dbcp</artifactId>
        <version>1.4</version>
    </dependency>

</dependencies>
</project>

mvn依赖项:tree -Dverbose

    [INFO] +- javax.servlet:servlet-api:jar:2.5:provided
    [INFO] +- com.sun.faces:jsf-impl:jar:2.2.4:compile
    [INFO] +- com.sun.faces:jsf-api:jar:2.2.4:compile
    [INFO] +- javax.servlet.jsp:javax.servlet.jsp-api:jar:2.3.1:compile
    [INFO] +- javax.servlet:jstl:jar:1.1.2:compile
    [INFO] +- org.primefaces:primefaces:jar:6.1:compile
    [INFO] +- org.primefaces.omegamenu:omega-menu:jar:1.1.4:compile
    [INFO] +- org.hibernate:hibernate-core:jar:4.3.6.Final:compile
    [INFO] |  +- org.jboss.logging:jboss-logging:jar:3.1.3.GA:compile
    [INFO] |  +- org.jboss.logging:jboss-logging-annotations:jar:1.2.0.Beta1:compile
    [INFO] |  +- org.jboss.spec.javax.transaction:jboss-transaction-api_1.2_spec:jar:1.0.0.Final:compile
    [INFO] |  +- dom4j:dom4j:jar:1.6.1:compile
    [INFO] |  |  \- xml-apis:xml-apis:jar:1.0.b2:compile
    [INFO] |  +- org.hibernate.common:hibernate-commons-annotations:jar:4.0.5.Final:compile
    [INFO] |  |  +- (org.jboss.logging:jboss-logging:jar:3.1.3.GA:compile - omitted for duplicate)
    [INFO] |  |  \- (org.jboss.logging:jboss-logging-annotations:jar:1.2.0.Beta1:compile - omitted for duplicate)
    [INFO] |  +- org.hibernate.javax.persistence:hibernate-jpa-2.1-api:jar:1.0.0.Final:compile
    [INFO] |  +- org.javassist:javassist:jar:3.18.1-GA:compile
    [INFO] |  +- antlr:antlr:jar:2.7.7:compile
    [INFO] |  \- org.jboss:jandex:jar:1.1.0.Final:compile
    [INFO] +- org.hibernate:hibernate-entitymanager:jar:4.3.6.Final:compile
    [INFO] |  +- (org.jboss.logging:jboss-logging:jar:3.1.3.GA:compile - omitted for duplicate)
    [INFO] |  +- (org.jboss.logging:jboss-logging-annotations:jar:1.2.0.Beta1:compile - omitted for duplicate)
    [INFO] |  +- (org.hibernate:hibernate-core:jar:4.3.6.Final:compile - omitted for duplicate)
    [INFO] |  +- (dom4j:dom4j:jar:1.6.1:compile - omitted for duplicate)
    [INFO] |  +- (org.hibernate.common:hibernate-commons-annotations:jar:4.0.5.Final:compile - omitted for duplicate)
    [INFO] |  +- (org.hibernate.javax.persistence:hibernate-jpa-2.1-api:jar:1.0.0.Final:compile - omitted for duplicate)
    [INFO] |  +- (org.jboss.spec.javax.transaction:jboss-transaction-api_1.2_spec:jar:1.0.0.Final:compile - omitted for duplicate)
    [INFO] |  \- (org.javassist:javassist:jar:3.18.1-GA:compile - omitted for duplicate)
    [INFO] +- org.springframework:spring-orm:jar:4.0.3.RELEASE:compile
    [INFO] |  +- org.springframework:spring-beans:jar:4.0.3.RELEASE:compile
    [INFO] |  |  \- (org.springframework:spring-core:jar:4.0.3.RELEASE:compile - omitted for duplicate)
    [INFO] |  +- org.springframework:spring-core:jar:4.0.3.RELEASE:compile
    [INFO] |  |  \- commons-logging:commons-logging:jar:1.1.3:compile
    [INFO] |  +- org.springframework:spring-jdbc:jar:4.0.3.RELEASE:compile
    [INFO] |  |  +- (org.springframework:spring-beans:jar:4.0.3.RELEASE:compile - omitted for duplicate)
    [INFO] |  |  +- (org.springframework:spring-core:jar:4.0.3.RELEASE:compile - omitted for duplicate)
    [INFO] |  |  \- (org.springframework:spring-tx:jar:4.0.3.RELEASE:compile - omitted for duplicate)
    [INFO] |  \- org.springframework:spring-tx:jar:4.0.3.RELEASE:compile
    [INFO] |     +- (org.springframework:spring-beans:jar:4.0.3.RELEASE:compile - omitted for duplicate)
    [INFO] |     \- (org.springframework:spring-core:jar:4.0.3.RELEASE:compile - omitted for duplicate)
    [INFO] +- org.springframework:spring-webmvc:jar:4.0.3.RELEASE:compile
    [INFO] |  +- (org.springframework:spring-beans:jar:4.0.3.RELEASE:compile - omitted for duplicate)
    [INFO] |  +- org.springframework:spring-context:jar:4.0.3.RELEASE:compile
    [INFO] |  |  +- org.springframework:spring-aop:jar:4.0.3.RELEASE:compile
    [INFO] |  |  |  +- aopalliance:aopalliance:jar:1.0:compile
    [INFO] |  |  |  +- (org.springframework:spring-beans:jar:4.0.3.RELEASE:compile - omitted for duplicate)
    [INFO] |  |  |  \- (org.springframework:spring-core:jar:4.0.3.RELEASE:compile - omitted for duplicate)
    [INFO] |  |  +- (org.springframework:spring-beans:jar:4.0.3.RELEASE:compile - omitted for duplicate)
    [INFO] |  |  +- (org.springframework:spring-core:jar:4.0.3.RELEASE:compile - omitted for duplicate)
    [INFO] |  |  \- (org.springframework:spring-expression:jar:4.0.3.RELEASE:compile - omitted for duplicate)
    [INFO] |  +- (org.springframework:spring-core:jar:4.0.3.RELEASE:compile - omitted for duplicate)
    [INFO] |  +- org.springframework:spring-expression:jar:4.0.3.RELEASE:compile
    [INFO] |  |  \- (org.springframework:spring-core:jar:4.0.3.RELEASE:compile - omitted for duplicate)
    [INFO] |  \- org.springframework:spring-web:jar:4.0.3.RELEASE:compile
    [INFO] |     +- (org.springframework:spring-aop:jar:4.0.3.RELEASE:compile - omitted for duplicate)
    [INFO] |     +- (org.springframework:spring-beans:jar:4.0.3.RELEASE:compile - omitted for duplicate)
    [INFO] |     +- (org.springframework:spring-context:jar:4.0.3.RELEASE:compile - omitted for duplicate)
    [INFO] |     \- (org.springframework:spring-core:jar:4.0.3.RELEASE:compile - omitted for duplicate)
    [INFO] \- commons-dbcp:commons-dbcp:jar:1.4:compile
    [INFO]    \- commons-pool:commons-pool:jar:1.5.4:compile
    [INFO] --------------------------------
[INFO] BUILD SUCCESS

1 个答案:

答案 0 :(得分:1)

这里的问题是您使用的Hibernate Core版本太新或太旧。 从4.0模块的版本Hibernate Core开始,

org.hibernate.engine.FilterDefinition

定义现在位于

org.hibernate.engine.spi.FilterDefinition

Hibernate EntityManager依赖项

<dependency>
    <groupId>org.hibernate</groupId>
    <artifactId>hibernate-entitymanager</artifactId>
    <version>4.0.1.Final</version>
</dependency>

正在导入Hibernate Core 4.0.1.Final作为传递依赖。

尽管,从SessionFactory 4.0.1.Final的反编译版本看,它似乎已正确编译。

enter image description here

可能是您通过另一个传递依赖项导入了较旧的版本。
要发现这一点,请让Maven显示依赖关系树。

mvn dependency:tree -Dverbose

无论如何,我的建议是第一步,升级Hibernate EntityManager模块

<dependency>
    <groupId>org.hibernate</groupId>
    <artifactId>hibernate-entitymanager</artifactId>
    <version>4.3.6.Final</version>
</dependency>