设置spring + hibernate maven项目时遇到问题?

时间:2016-06-10 13:03:59

标签: java spring hibernate maven spring-boot

我试图设置一个简单的spring + hibernate项目,但不断获得以下堆栈跟踪......我也在下面粘贴了我的pom配置。

 .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v1.3.5.RELEASE)

2016-06-10 23:01:12.744  INFO 19668 --- [           main] com.package.tests.Test                : Starting Test on JamesWMAC.local with PID 19668 (/Users/james/apps/xx/target/classes started by james in /Users/james/apps/xx)
2016-06-10 23:01:12.747  INFO 19668 --- [           main] com.package.tests.Test                : No active profile set, falling back to default profiles: default
2016-06-10 23:01:12.762  INFO 19668 --- [           main] ationConfigEmbeddedWebApplicationContext : Refreshing org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@70a9f84e: startup date [Fri Jun 10 23:01:12 EST 2016]; root of context hierarchy
2016-06-10 23:01:12.771 ERROR 19668 --- [           main] o.s.boot.SpringApplication               : Application startup failed


java.lang.NoClassDefFoundError: org/springframework/beans/factory/config/EmbeddedValueResolver
    at org.springframework.context.support.ApplicationContextAwareProcessor.<init>(ApplicationContextAwareProcessor.java:72) ~[spring-context-4.3.0.RELEASE.jar:4.3.0.RELEASE]
    at org.springframework.context.support.AbstractApplicationContext.prepareBeanFactory(AbstractApplicationContext.java:632) ~[spring-context-4.3.0.RELEASE.jar:4.3.0.RELEASE]
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:516) ~[spring-context-4.3.0.RELEASE.jar:4.3.0.RELEASE]
    at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:118) ~[spring-boot-1.3.5.RELEASE.jar:1.3.5.RELEASE]
    at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:766) [spring-boot-1.3.5.RELEASE.jar:1.3.5.RELEASE]
    at org.springframework.boot.SpringApplication.createAndRefreshContext(SpringApplication.java:361) [spring-boot-1.3.5.RELEASE.jar:1.3.5.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:307) [spring-boot-1.3.5.RELEASE.jar:1.3.5.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1191) [spring-boot-1.3.5.RELEASE.jar:1.3.5.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1180) [spring-boot-1.3.5.RELEASE.jar:1.3.5.RELEASE]
    at com.package.tests.Test.main(Test.java:33) [classes/:na]
Caused by: java.lang.ClassNotFoundException: org.springframework.beans.factory.config.EmbeddedValueResolver
    at java.net.URLClassLoader$1.run(URLClassLoader.java:372) ~[na:1.8.0_05]
    at java.net.URLClassLoader$1.run(URLClassLoader.java:361) ~[na:1.8.0_05]
    at java.security.AccessController.doPrivileged(Native Method) ~[na:1.8.0_05]
    at java.net.URLClassLoader.findClass(URLClassLoader.java:360) ~[na:1.8.0_05]
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424) ~[na:1.8.0_05]
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308) ~[na:1.8.0_05]
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357) ~[na:1.8.0_05]
    ... 10 common frames omitted

这是我的pom.xml文件:

&LT;

?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>org.package.api</groupId>
    <artifactId>package</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <properties>
        <org.springframework-version>4.3.0.RELEASE</org.springframework-version>
    </properties>

        <parent>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-parent</artifactId>
            <version>1.3.5.RELEASE</version>
            <relativePath/> <!-- lookup parent from repository -->
        </parent>
    <dependencies>
        <!-- http://mvnrepository.com/artifact/org.hibernate/hibernate-core -->
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-core</artifactId>
            <version>5.2.0.Final</version>
        </dependency>
        <!-- http://mvnrepository.com/artifact/mysql/mysql-connector-java -->
        <!-- <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> 
            <version>6.0.2</version> </dependency> -->

        <!-- http://mvnrepository.com/artifact/mysql/mysql-connector-java -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.39</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>${org.springframework-version}</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>${org.springframework-version}</version>
        </dependency>



        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-orm</artifactId>
            <version>${org.springframework-version}</version>
            <type>jar</type>
            <scope>compile</scope>
        </dependency>
        <!-- JAVA servlet dependcies -->
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>3.1.0</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>javax.servlet.jsp</groupId>
            <artifactId>javax.servlet.jsp-api</artifactId>
            <version>2.3.1</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-dbcp2</artifactId>
            <version>2.0</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>

                <dependency>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-starter-web</artifactId>
                </dependency>
    </dependencies>

        <build>
            <plugins>
                <plugin>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-maven-plugin</artifactId>
                </plugin>
                <plugin>
      <artifactId>maven-assembly-plugin</artifactId>
      <configuration>
        <archive>
          <manifest>
            <mainClass>com.package.tests.Test</mainClass>
          </manifest>
        </archive>
        <descriptorRefs>
          <descriptorRef>jar-with-dependencies</descriptorRef>
        </descriptorRefs>
      </configuration>
    </plugin>
            </plugins>
        </build>
</project>

我的servlet-context.xml,我不确定这是否会导致任何错误:

<?xml version="1.0" encoding="UTF-8"?>
<mvc:annotation-driven />
<mvc:resources mapping="/resources/**" location="/resources/" />
<context:component-scan base-package="com.package.spring" />

<bean id="dataSource" class="org.apache.commons.dbcp2.BasicDataSource" destroy-method="close">
    <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
    <property name="url" value="jdbc:mysql://localhost:3306/db"/>
    <property name="username" value="root"/>
    <property name="password" value="root"/>
</bean>
<bean id="sessionFactory"
    class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
    <property name="dataSource" ref="dataSource" />
    <property name="configLocation" value="classpath:hibernate.cfg.xml" />
</bean>
<tx:annotation-driven />
<bean id="transactionManager"
    class="org.springframework.orm.hibernate5.HibernateTransactionManager">
    <property name="sessionFactory" ref="sessionFactory" />
</bean>

 Injects DAO into spring controller  
<bean id="userDAO" class="com.package.dao.userDAO">
    <constructor-arg>
        <ref bean="sessionFactory" />
    </constructor-arg>
</bean>

<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="prefix" value="/webapp/views/" />
    <property name="suffix" value=".jsp" />
</bean>

我花了太多时间试图让它发挥作用......

1 个答案:

答案 0 :(得分:3)

首先你的pom有点乱。使用spring-boot-starter项目而不是单个jar。

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-jpa</artifactId>
    </dependency>

     <!-- http://mvnrepository.com/artifact/mysql/mysql-connector-java -->
     <dependency>
         <groupId>mysql</groupId>
         <artifactId>mysql-connector-java</artifactId>
     </dependency>

</dependencies>

应该是你所需要的一切。我想要一个特定版本的hibernate使用hibernate.version属性。

<properties>
    <hibernate.version>5.2.0.Final</hibernate.version>
</properties>

注意: Spring(Boot)尚不适用于hibernate 5.2!

接下来删除Spring xml配置文件。

将数据库的凭据放在application.properties中,而不是普通的hibernate使用JPA代替。

另外在你的pom中删除了程序集插件,因为Spring Boot Plugin已经处理了这个问题,而程序集插件生成了一个错误的jar文件。