简体   繁体   English

Spring Application Context配置:找不到类异常

[英]Spring Application Context configuration: class not found exception

When I start my web application, I get from init(): 启动Web应用程序时,我从init()获得:

    java.lang.ClassNotFoundException:org.springframework.context.ApplicationContext

maybe I miss something I my configuration files. 也许我错过了我的配置文件。

In pom.xml I have this 2 spring dependencies: 在pom.xml中,我具有以下两个spring依赖项:

[...]
    <dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring</artifactId>
  <version>2.5.6.SEC03</version>
</dependency>
    <dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-context</artifactId>
  <version>4.1.6.RELEASE</version>
</dependency>
<dependency>
[...]

and in Datasource.xml I have beans and listener declared: 在Datasource.xml中,我声明了bean和侦听器:

<bean id="dataSource" destroy-method="close"
    class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost/Rubrica" />
<property name="username" value="user" />
<property name="password" value="pass" />
  </bean>

  <bean id="JDBC_Spring_EntryDAO" class="net.tirasa.jdbc_spring_addressbook.JDBC_Spring_EntryDAO">  
<property name="JDBC_Spring_EntryDAO" ref="JDBC_Spring_EntryDAO" />
<property name="dataSource" ref="dataSource" />
  </bean>

</beans>

<listener>
  <listenerclass>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

The log message switched misteriously to 日志消息错误地切换到

class path resource [Spring-Datasource.xml] cannot be opened because it does not exist

That is good to me, since it looks like I have to put the Spring-Datasource.xml file into the right path, or I have to specify the right path in the command line 这对我很好,因为看起来我必须将Spring-Datasource.xml文件放入正确的路径,或者必须在命令行中指定正确的路径。

    ApplicationContext context = new ClassPathXmlApplicationContext("Spring-Datasource.xml");
    dataSource = (DataSource) context.getBean("JDBC_Spring_EntryDAO");

Use Spring 4 core and context: 使用Spring 4的核心和上下文:

<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-context</artifactId>
    <version>4.0.0.RELEASE</version>
</dependency>

<dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-core</artifactId>
  <version>4.0.0.RELEASE</version>
</dependency>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM