【问题标题】:Spring framework configurationSpring框架配置
【发布时间】:2014-03-02 09:58:26
【问题描述】:

自从我开始学习 Spring Framework 并尝试使用 MongoDB 和 Spring Framework 实现 RESTful Web 服务以来已经 3 天了。我还处于起步阶段并试图了解 Spring Framework 的配置。当我开始我的项目并点击所需的 URL 时,它不起作用。我也将我的项目上传到 github (here is the url)

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/j2ee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         id="WebApp_ID"
         version="2.4"
         xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
                             http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
    <servlet>
        <servlet-name>spring</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>spring</servlet-name>
        <url-pattern>/*</url-pattern>
    </servlet-mapping>
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
</web-app>

applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" 
       xmlns:context="http://www.springframework.org/schema/context" 
       xmlns:mvc="http://www.springframework.org/schema/mvc"   
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
       xsi:schemaLocation="http://www.springframework.org/schema/beans        
                           http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
                           http://www.springframework.org/schema/context
                           http://www.springframework.org/schema/context/spring-context-3.0.xsd
                           http://www.springframework.org/schema/mvc
                           http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
    <!-- Activates various annotations to be detected in bean classes -->
    <context:annotation-config />
    <!-- Scans the classpath for annotated components that will be auto-registered as Spring beans.
     For example @Controller and @Service. Make sure to set the correct base-package-->
    <context:component-scan base-package="shoponway.webservice" />
    <!-- Configures the annotation-driven Spring MVC Controller programming model.
    Note that, with Spring 3.0, this tag works in Servlet MVC only!  -->
    <mvc:annotation-driven />
    <!-- Loads MongoDB configuraton -->
    <import resource="mongo-config.xml" />
</beans>

spring-servlet.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" 
       xmlns:p="http://www.springframework.org/schema/p" 
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
                           http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
    <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver" p:prefix="/WEB-INF/jsp/" p:suffix=".jsp" />
</beans>

和我的控制器

@Controller
public class PersonController {
    protected Logger logger = Logger.getLogger(PersonController.class);

    @Resource(name = "personService")
    private PersonService personService;

    @RequestMapping(value = "/allpersons", method = RequestMethod.GET)
    public String getAllPersons(Model model){
        model.addAttribute("persons", personService.getAllPersons());
        return "personspage";
    }
}

我遇到的错误

 java.lang.ClassNotFoundException: org.springframework.web.context.ContextLoaderListener
    at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1718)
    at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1569)
    at org.apache.catalina.core.DefaultInstanceManager.loadClass(DefaultInstanceManager.java:529)
    at org.apache.catalina.core.DefaultInstanceManager.loadClassMaybePrivileged(DefaultInstanceManager.java:511)
    at org.apache.catalina.core.DefaultInstanceManager.newInstance(DefaultInstanceManager.java:139)
    at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4888)
    at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5467)
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
    at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1559)
    at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1549)
    at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:303)
    at java.util.concurrent.FutureTask.run(FutureTask.java:138)
    at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
    at java.lang.Thread.run(Thread.java:662)

【问题讨论】:

  • 您是否遇到任何错误。你的上下文是否正常?
  • 是的,我通过编辑问题插入错误
  • 嗯,错误信息很清楚:您没有在上下文中定义“personService”bean。
  • 我在哪里可以定义它以及如何定义?在 applicationContext.xml 或 spring-servlet.xml 中?
  • 您使用的是非常旧的 Spring 版本,我建议您查看 JavaConfig 选项和 Spring Boot。它们使配置变得更加简单,并且是一个更容易的起点。

标签: spring spring-mvc spring-mongo


【解决方案1】:

本例中存在多个问题,可以如下更正

  1. @Service 注释添加到您的 shoponway.webservice.services.PersonService 类中,您需要添加 @Service 以使其成为合适的注入候选者。

  2. 您的 'mongo-config.xml' 指向错误的 mongo 存储库位置,请将其编辑为 &lt;mongo:repositories base-package="shoponway.webservice.services" /&gt; 来自
    &lt;mongo:repositories base-package="org.krams.tutorial.repositories" /&gt;

  3. Mongo 模板应引用以下 bean &lt;bean id="mongoTemplate" class="org.springframework.data.mongodb.core.MongoTemplate"&gt; 而不是 class="org.springframework.data.document.mongodb.MongoTemplate"

4) 用

替换 mongo-config.xml 内容
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns:context="http://www.springframework.org/schema/context"
xmlns:mongo="http://www.springframework.org/schema/data/mongo"
xsi:schemaLocation="http://www.springframework.org/schema/context
     http://www.springframework.org/schema/context/spring-context-3.0.xsd
      http://www.springframework.org/schema/data/mongo
      http://www.springframework.org/schema/data/mongo/spring-mongo-1.0.xsd
      http://www.springframework.org/schema/beans
      http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">

<mongo:mongo host="localhost" port="27017" />
<mongo:db-factory dbname="shoponwaydb" />

<bean id="mongoTemplate" class="org.springframework.data.mongodb.core.MongoTemplate">
    <constructor-arg name="mongoDbFactory" ref="mongoDbFactory" />
</bean>

 </beans>

`

一旦你改正了这些,你就可以轻松地运行你的例子了:

【讨论】:

  • 我按照你说的做了。而且我仍然收到错误,但至少这个错误痕迹很小。我插入新的错误跟踪。
  • 在 target/WEB-INF/lib 文件夹中检查 spring-web.3.2.0.RELEASE jar
  • 它对我来说运行良好,只有这些更改我才能运行这个示例。请检查您在 WEB-INF/lib 文件夹中的 jars
  • 当我在 pom 中更新 spring 依赖项时,我在 target/WEB-INF/lib 文件夹中有 spring-web.4.0.2.RELEASE jar。
  • 真的很奇怪。在我应用您告诉的更改后,maven 抛出“无法配置”错误。虽然我可以在项目结构中看到 maven 依赖项,但在部署程序集中看不到它们
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-02-19
  • 1970-01-01
  • 2018-10-07
  • 1970-01-01
相关资源
最近更新 更多