@Autowired并不是春天的宁静服务

时间:2014-10-16 07:54:46

标签: spring rest autowired

这是映射部分:

@Path("/hello")
public class BookRestController {
    @Autowired
    BookService service;

    @GET
    public String getMsg() {

        System.out.println("in controller");
        List<BookDto> dto=service.loadAll();
        System.out.println(dto);
        return "dto";

    }

}

web.xml代码:

<?xml version="1.0" encoding="UTF-8"?>

<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
    <display-name>springJdbc</display-name>
    <welcome-file-list>
        <welcome-file>index.html</welcome-file>
        <welcome-file>index.htm</welcome-file>
        <welcome-file>index.jsp</welcome-file>
        <welcome-file>default.html</welcome-file>
        <welcome-file>default.htm</welcome-file>
        <welcome-file>default.jsp</welcome-file>
    </welcome-file-list>
    <servlet>
        <description></description>
        <servlet-name>dispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>dispatcher</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
    <servlet>
        <servlet-name>jersey-serlvet</servlet-name>
        <servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
        <load-on-startup>2</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>jersey-serlvet</servlet-name>
        <url-pattern>/rest/*</url-pattern>
    </servlet-mapping>
</web-app>

servlet-dispatcher.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:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:p="http://www.springframework.org/schema/p"
       xsi:schemaLocation="http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc.xsd
        http://www.springframework.org/schema/beans     
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context 
        http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/tx 
        http://www.springframework.org/schema/tx/spring-tx.xsd">
    <!-- controller package detail  -->
    <context:component-scan base-package="com.app.controller,com.app.service,com.app.dao.hib" />
    <!-- -->
    <mvc:annotation-driven/>
    <!--   -->
    <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver" p:maxUploadSize="1500000"/>
    <!--  -->
    <mvc:resources mapping="/static/**" location="/static/" />
    <!-- view resolver  -->
    <bean id="viewResolver1" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix">
            <value>/WEB-INF/jsps/</value>
        </property>
        <property name="suffix">
            <value>.jsp</value>
        </property>
    </bean>
    <!-- creating connection detail of hibernate (connection pooling) -->
    <bean id="myDataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
        <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
        <property name="url" value="jdbc:mysql://localhost:3306/db1"/>
        <property name="username" value="root"/>
        <property name="password" value="root"/>
    </bean>
    <!-- Hibernate mapping and configuration file details (Session factory details)  -->
    <bean id="mySessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
        <property name="dataSource" ref="myDataSource" />
        <property name="annotatedClasses">
            <list>
                <value>com.app.entity.BookEntity</value>
            </list>
        </property>
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
                <prop key="hibernate.show_sql">true</prop>
                <!--                <prop key="hibernate.hbm2ddl.auto">create</prop> -->
            </props>
        </property>
    </bean>
    <tx:annotation-driven transaction-manager="transactionManager"/>
    <bean id="transactionManager"
          class="org.springframework.orm.hibernate4.HibernateTransactionManager">
        <property name="sessionFactory" ref="mySessionFactory" />
    </bean>
</beans>

它在服务上提供空指针异常或我使用的任何自动装配,即它有自动装配问题。任何人都可以帮忙吗?我想我错过了一些用于实现restFull服务的东西,因为应用程序可以正常使用简单的spring应用程序

2 个答案:

答案 0 :(得分:1)

界面:

  

public interface BookService {

     

}

实现接口BookService的类:

  

@服务
  公共类BookServiceImpl实现BookService {

     

}

要自动装配BookServiceImpl类,必须在BookRestController类的接口名称和控制器类的@Controller注释上放置@Autowired注释:

  

@Controller
  公共类BookRestController {

     

@Autowired
   私人BookService bookService;

     

}

您还必须处理配置,例如对于组件扫描,您必须在spring配置文件中添加以下行。放

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

在spring配置xml中,将com.org.dao更改为你的包名,其中dao文件放在应用程序中。

答案 1 :(得分:0)

在此主题中回答了这个问题 - Spring DI - Autowired property is null in a REST service

基本上,泽西岛并没有自动为你做Spring自动装配。你需要使用Jersey servlet对象来获得Spring的东西。