org.springframework.beans.factory.BeanCreationException:使用名称' userManagerREST创建bean时出错':init方法的调用失败

时间:2016-04-17 14:38:15

标签: apache web-services maven spring-mvc cxf

这是我得到的错误的堆栈跟踪:

SEVERE: Exception sending context initialized event to listener instance of class org.springframework.web.context.ContextLoaderListener
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'userManagerREST': Invocation of init method failed; nested exception is org.apache.cxf.service.factory.ServiceConstructionException
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1574)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:539)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:476)
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:303)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:299)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:755)
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:757)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:480)
    at org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:403)
Caused by: org.apache.cxf.service.factory.ServiceConstructionException
    at org.apache.cxf.jaxrs.JAXRSServerFactoryBean.create(JAXRSServerFactoryBean.java:215)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeCustomInitMethod(AbstractAutowireCapableBeanFactory.java:1702)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1641)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1570)
    ... 21 more

这是我的代码

UserManagerDao.java

  public interface UserManagerDao
{
    public User fetchUserById(int id);
    public List<User> fetchAllUsers();
    public void insertUser(User user);
    public void updateUser(User user);
    public void deleteUser(User user);
}

UserManager.java

@Service
@Consumes("application/json")
@Produces("application/json")
public interface UserManager {

    @POST
    @Path("/insertUser/")
    public UserResponse insertUser(UserRequest request);

}

UserManagerService.java

public class UserManagerService implements UserManager{

    public UserManagerService(){
    }

    @Autowired
    private UserManagerDaoImpl userDao;

    public UserManagerDaoImpl getUserDao() {
        return userDao;
    }

    public void setUserDao(UserManagerDaoImpl userDao) {
        this.userDao = userDao;
    }



    @Override
    public UserResponse insertUser(UserRequest request) {
        UserResponse response = new UserResponse();
        try{
            getUserDao().insertUser(request.getUser());
        }
        catch(Exception e){
            response.setSuccess(false);
            response.setErrorMessage(e.getClass() + ":" + e.getMessage());
        }

        return response;
    }

}

UserManagerDaoImpl.java

public class UserManagerDaoImpl implements UserManagerDao {

    public UserManagerDaoImpl(){
    }

    private int nextUserId=0;
    List<User> userList= new ArrayList<User>();

    @Override
    public User fetchUserById(int id) {
        for(User user : userList){
            if(user.getId()==id){
                return user;
            }
        }

        throw new RuntimeException("User not found : " + id);

    }

    @Override
    public List<User> fetchAllUsers() {
        return userList;
    }

    @Override
    public void insertUser(User user) {
        user.setId(nextUserId++);
        userList.add(user);
    }

    @Override
    public void updateUser(User user) {
        User editUser = fetchUserById(user.getId());
        editUser.setBirthDate(user.getBirthDate());
        editUser.setCity(user.getCity());
        editUser.setEmail(user.getEmail());
        editUser.setName(user.getName());
        editUser.setState(user.getName());

    }

    @Override
    public void deleteUser(User user) {
        User delUser = fetchUserById(user.getId());
        userList.remove(delUser);
    }

}

的web.xml

<?xml version="1.0" encoding="ISO-8859-1"?>
<web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
    http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
    metadata-complete="true">


    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/*-context.xml</param-value>
    </context-param>

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

    <servlet>
        <servlet-name>CXFServlet</servlet-name>
        <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>CXFServlet</servlet-name>
        <url-pattern>/services/*</url-pattern>
    </servlet-mapping>
</web-app>

springrest-context.xml中

<?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:cxf="http://cxf.apache.org/core"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:jaxws="http://cxf.apache.org/jaxws" xmlns:jaxrs="http://cxf.apache.org/jaxrs"
    xsi:schemaLocation="
            http://cxf.apache.org/core http://cxf.apache.org/schemas/core.xsd
            http://www.springframework.org/schema/beans 
            http://www.springframework.org/schema/beans/spring-beans.xsd
            http://cxf.apache.org/jaxrs http://cxf.apache.org/schemas/jaxrs.xsd
            http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd
            http://www.springframework.org/schema/context
            http://www.springframework.org/schema/context/spring-context-4.1.xsd">

    <import resource="classpath:META-INF/cxf/cxf.xml" />
    <import resource="classpath:META-INF/cxf/cxf-servlet.xml" />
    <context:component-scan base-package="com.ihc.*" />
    <cxf:bus>
        <cxf:features>
            <cxf:logging />
        </cxf:features>
    </cxf:bus>

    <bean id="userDao" class="com.ihc.bg.dao.impl.UserManagerDaoImpl">
    </bean>

    <bean id="userManagerService" class="com.ihc.bg.services.impl.UserManagerService">
        <property name="userDao" ref="userDao" />
    </bean>

    <bean id="jsonProvider" class="org.codehaus.jackson.jaxrs.JacksonJsonProvider" />

    <jaxrs:server id="userManagerREST" address="/UserManager">
        <jaxrs:serviceBeans>
            <ref bean="userManagerService"/>
        </jaxrs:serviceBeans>
        <jaxrs:providers>
            <ref bean='jsonProvider' />
        </jaxrs:providers>
    </jaxrs:server>

</beans>

错误来自springrest-context.xml的<jaxrs:server>标记部分

有人可以告诉我为什么会收到此错误吗?

1 个答案:

答案 0 :(得分:0)

请使用双引号正确包含提供商。这就是它现在的样子:

 <ref bean='jsonProvider' />

应该是

<ref bean="jsonProvider" />