Maven + Spring + Hibernate:java.lang.ClassNotFoundException:org.springframework.context.support.ClassPathXmlApplicationContext [英] Maven+Spring+Hibernate: java.lang.ClassNotFoundException: org.springframework.context.support.ClassPathXmlApplicationContext
问题描述
我正在尝试使用Maven并使Spring和Hibernate迭代,从而开发一个应用程序.我已经在Internet上搜索了一段时间,我想我添加了适当的依赖项,但是它不起作用.我使用netbeans,构建了项目等,因此所有依赖项都已下载并包含在内.我得到:java.lang.ClassNotFoundException: org.springframework.context.support.ClassPathXmlApplicationContext
.有人对此有任何想法吗?
pom.xml
I'm trying to get working an app using maven and itegrating spring and hibernate. I've been searching on the Internet for a while and I think I added proper dependencies, but it doesn't work. I use netbeans, I builded project etc. so all dependencies are downloaded and included. I'm getting: java.lang.ClassNotFoundException: org.springframework.context.support.ClassPathXmlApplicationContext
. Anybody has any ideas on this one?
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>local.bb</groupId>
<artifactId>MavenSpringMVC</artifactId>
<version>1.0</version>
<packaging>war</packaging>
<name>MavenSpringMVC</name>
<properties>
<endorsed.dir>${project.build.directory}/endorsed</endorsed.dir>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<org.springframework.version>4.1.6.RELEASE</org.springframework.version>
</properties>
<dependencies>
<dependency>
<groupId>javax</groupId>
<artifactId>javaee-web-api</artifactId>
<version>7.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.35</version>
<scope>provided</scope>
</dependency>
<!--
MAIN / SPRING
-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${org.springframework.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>${org.springframework.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-expression</artifactId>
<version>${org.springframework.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>${org.springframework.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>${org.springframework.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${org.springframework.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>${org.springframework.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${org.springframework.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${org.springframework.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>${org.springframework.version}</version>
<scope>provided</scope>
</dependency>
<!--
SPRING / HIBERNATE
-->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>5.0.0.Beta1</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-annotations</artifactId>
<version>3.5.6-Final</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator</artifactId>
<version>5.2.0.Beta1</version>
<scope>provided</scope>
</dependency>
<!--
HIBERNATE
-->
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
<compilerArguments>
<endorseddirs>${endorsed.dir}</endorseddirs>
</compilerArguments>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.3</version>
<configuration>
<failOnMissingWebXml>false</failOnMissingWebXml>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>2.6</version>
<executions>
<execution>
<phase>validate</phase>
<goals>
<goal>copy</goal>
</goals>
<configuration>
<outputDirectory>${endorsed.dir}</outputDirectory>
<silent>true</silent>
<artifactItems>
<artifactItem>
<groupId>javax</groupId>
<artifactId>javaee-endorsed-api</artifactId>
<version>7.0</version>
<type>jar</type>
</artifactItem>
</artifactItems>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
Main.java
Main.java
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Main {
public static void main(String args[]) {
ClassPathXmlApplicationContext appContext = new ClassPathXmlApplicationContext("spring.xml");//here's the error
System.out.println("Working!");
}
}
推荐答案
尝试将您的Maven依赖范围更改为compile
.
Try changing your maven dependency scopes to compile
.
这篇关于Maven + Spring + Hibernate:java.lang.ClassNotFoundException:org.springframework.context.support.ClassPathXmlApplicationContext的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!