在服务器启动时获得spring自动装配错误

时间:2012-12-22 08:46:55

标签: java spring

我正在通过创建spring3 restful project.i来处理测试示例。配置并创建了所有必需的文件,但是当我启动服务器时,我得到以下错误-----

    org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:486)
        at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:92)
        Truncated. see log file for complete stacktrac



    --------------------applicationContext.xml file--------------------------
  below is my applicationContext.xml file

    <?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:context="http://www.springframework.org/schema/context"
        xmlns:mvc="http://www.springframework.org/schema/mvc"   
        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="org.nea.rest.abc" />
    <context:component-scan base-package="org.nea.spring.services.interfaces.test" />


     <bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping" />  

    <!-- 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 />

    </beans>

    -------------------------<abc>-servlet.xml file(spring bean) file-------------------

    <?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:p="http://www.springframework.org/schema/p"
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">

    <!-- Specify a view resolver for JSP files-->
    <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"
    p:prefix="/WEB-INF/jsp/" p:suffix=".jsp" />

    </beans>


    -----------------------java file ---------------------------

下面是我制作我的方法的文件,我必须通过宁静的方式公开

    package org.nea.rest.unsr;

    import java.util.ArrayList;
    import java.util.List;

    import org.apache.commons.logging.Log;
    import org.apache.commons.logging.LogFactory;

    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Component;
    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.PathVariable;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RequestMethod;
    import org.springframework.web.bind.annotation.ResponseBody;

    @Controller

1 个答案:

答案 0 :(得分:1)

扩展@jbnizet评论...... “实现类必须是一个Spring bean。要么在XML文件中声明它,要么用Spring注释(例如组件或服务)注释它。”

您可以将其添加到appContext:

<context:component-scan base-package="com.yourbasepackage"/>

将允许您添加annoation

@Service("uniservService")
public class UniservServiceJPAImpl implements UniservSearchInterface {
}

然后你可能还想要这些来进行更全面的注释处理:

<!--Indicates that transaction configuration is defined by Java 5
    annotations on bean classes -->
<tx:annotation-driven transaction-manager="yourtransactionManager"/>

 <!-- Activates various annotations to be detected in bean classes: Spring's @Required and
    @Autowired, as well as JSR 250's @PostConstruct, @PreDestroy and @Resource (if available) -->
    <context:annotation-config/>