无法使用Controller Spring自动连接服务

时间:2013-07-16 12:33:12

标签: spring controller

我已经尝试过搜索这个特定问题但得到了很多答案,但仍然无法弄清楚isse。

控制器类:

package com.springforbegineers.controller;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

import com.springforbegineers.domain.Search;
import com.springforbegineers.service.BookService;
@Controller
public class BookController {

    private BookService booksService;

    @Autowired
    public void setBookService(BookService bookService){
        this.booksService = bookService;
    }

    @RequestMapping("/performSearch")
    public String findBooks(Map<String,Object> map , @ModelAttribute("search") Search search){
        map.put("search", search);
        map.put("searchResults", booksService.findBooks(search.getSearchCriteria()));
        return "searchBooks";
    }
}

我无法将BookService接口连接到它。

以下是代码清单:

bookService的:

package com.springforbegineers.service;

import java.util.List;

import com.springforbegineers.domain.Books;

public interface BookService {
    void saveBook(Books books);
    List<Books> listBooks();
    List<Books> findBooks(String searchCriteria);
    void removeBook(Integer bookNo);
    Books editBook(Integer bookNo);
}

BookService实现类:

package com.springforbegineers.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.springforbegineers.dao.BookDAO;
import com.springforbegineers.domain.Books;

@Service
public class BookServiceImpl implements BookService{

    private BookDAO bookDAO;

    @Autowired
    public void setBookDAO(BookDAO bookDAO){
        this.bookDAO = bookDAO;
    }


    @Override
    public void saveBook(Books books) {
        bookDAO.saveBook(books);
    }

    @Override
    public List<Books> listBooks() {
        return bookDAO.listBooks();
    }

    @Override
    public List<Books> findBooks(String searchCriteria) {
        return bookDAO.findBooks(searchCriteria);
    }

    @Override
    public void removeBook(Integer bookNo) {
        bookDAO.removeBook(bookNo); 
    }

    @Override
    public Books editBook(Integer bookNo) {
        return bookDAO.editBook(bookNo);
    }

}

BookDAO界面:

package com.springforbegineers.dao;

import java.util.List;

import com.springforbegineers.domain.Books;

public interface BookDAO {

    void saveBook(Books books);
    List<Books> listBooks();
    List<Books> findBooks(String searchCriteria);
    void removeBook(Integer bookNo);
    Books editBook(Integer bookNo);
}

BookDAO实施班:

package com.springforbegineers.dao;

import java.util.List;

import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import com.springforbegineers.domain.Books;

@Repository
public class BookDAOImpl implements BookDAO {

    private SessionFactory sessionFactory;
    @Autowired
    public void setSessionFactory(final SessionFactory sessionFactory) {
      this.sessionFactory = sessionFactory;
    }



    @Override
    public void saveBook(Books books) {
        // TODO Auto-generated method stub

    }

    @Transactional
    @Override
    public List<Books> listBooks() {
        return null;
    }

    @Transactional
    @Override
    public List<Books> findBooks(String searchCriteria) {

         return sessionFactory.getCurrentSession().createQuery("select b from Books b where b.bookName like '%' "+searchCriteria).list();
    }

    @Transactional
    @Override
    public void removeBook(Integer bookNo) {
        // TODO Auto-generated method stub

    }

    @Transactional
    @Override
    public Books editBook(Integer bookNo) {
        // TODO Auto-generated method stub
        return null;
    }

}

以下是配置文件:

bookshopdispatcher-servlet.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:context="http://www.springframework.org/schema/context"
    xmlns:aop = "http://www.springframework.org/schema/aop"
    xmlns:tx = "http://www.springframework.org/schema/tx"
    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.1.xsd
        http://www.springframework.org/schema/context 
        http://www.springframework.org/schema/context/spring-context-3.1.xsd
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
        http://www.springframework.org/schema/tx
        http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd">

    <context:annotation-config/>

    <context:component-scan base-package="com.springforbegineers.service" />
    <context:component-scan base-package="com.springforbegineers.controller" />

    <mvc:annotation-driven />


    <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="viewClass" value="org.springframework.web.servlet.view.JstlView" />
        <property name="prefix" value="/WEB-INF/jsp/" />
        <property name="suffix" value=".jsp" />
    </bean>

    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="com.mysql.jdbc.Driver" />
        <property name="url" value="jdbc:mysql://localhost:8889/SpringDemo" />
        <property name="username" value="root" />
        <property name="password" value="root" />
   </bean>

    <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">  <!-- org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean -->
        <property name="dataSource" ref="dataSource" />
        <property name="hibernateProperties">
            <props>
                <prop key="hibernet.dialect">org.hibernate.dialect.MySQL5InnoDBDialect</prop>   <!-- MySQLDialect -->
                <prop key="hibernate.show_sql">true</prop>
            </props>
        </property>
    </bean>

    <tx:annotation-driven transaction-manager="transactionManager" />  <!-- transaction-manager="transactionManager" --> 

    <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactory" />
    </bean>

    <!-- <bean class="com.springforbegineers.validator.BookValidator" />

    <bean class="com.springforbegineers.validator.SystemUserValidator" /> -->

    <!-- <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean" /> -->

    <!-- <bean id="myTxManager" class="org.springframework.transaction.jta.JtaTransactionManager" /> -->



</beans>       

web.xml:

<?xml version="1.0" encoding="UTF-8"?>
<web-app 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" version="3.0">

    <!-- servlet for book dispatcher -->
    <servlet>
        <servlet-name>bookshopdispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>1</load-on-startup>    
    </servlet>

    <servlet-mapping> 
        <servlet-name>bookshopdispatcher</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>


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

    <context-param> 
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/bookshopdispatcher-servlet.xml,/WEB-INF/spring-security.xml,/WEB-INF/databaseConnection.xml</param-value>
    </context-param>

    <!-- Spring Security -->
    <filter>
        <filter-name>springSecurityFilterChain</filter-name>
        <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
    </filter>

    <filter-mapping>
        <filter-name>springSecurityFilterChain</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>


    <welcome-file-list>
        <welcome-file>/welcome</welcome-file> 
    </welcome-file-list>

    <session-config>
        <session-timeout>30</session-timeout>
    </session-config>

</web-app>

spring-security.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app 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" version="3.0">

    <!-- servlet for book dispatcher -->
    <servlet>
        <servlet-name>bookshopdispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>1</load-on-startup>    
    </servlet>

    <servlet-mapping> 
        <servlet-name>bookshopdispatcher</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>


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

    <context-param> 
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/bookshopdispatcher-servlet.xml,/WEB-INF/spring-security.xml,/WEB-INF/databaseConnection.xml</param-value>
    </context-param>

    <!-- Spring Security -->
    <filter>
        <filter-name>springSecurityFilterChain</filter-name>
        <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
    </filter>

    <filter-mapping>
        <filter-name>springSecurityFilterChain</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>


    <welcome-file-list>
        <welcome-file>/welcome</welcome-file> 
    </welcome-file-list>

    <session-config>
        <session-timeout>30</session-timeout>
    </session-config>

</web-app>

最后是pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.springforbegineers</groupId>
  <artifactId>BookApplication</artifactId>
  <packaging>war</packaging>
  <version>0.0.1-SNAPSHOT</version>
  <name>BookApplication Maven Webapp</name>
  <url>http://maven.apache.org</url>

  <properties>
        <spring.version>3.1.1.RELEASE</spring.version>
        <mysql.connector.version>5.1.21</mysql.connector.version>
        <org.springframework-version>3.1.0.RELEASE</org.springframework-version>
        <hibernate.version>4.2.0.Final</hibernate.version>
  </properties>



  <dependencies>




    <!-- Security -->
        <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-core</artifactId>
            <version>${org.springframework-version}</version>
            <scope>compile</scope>
        </dependency>

        <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-web</artifactId>
            <version>${org.springframework-version}</version>
            <scope>compile</scope>
        </dependency>

        <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-config</artifactId>
            <version>${org.springframework-version}</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-taglibs</artifactId>
            <version>${org.springframework-version}</version>
        </dependency>

    <!-- Servlet -->
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>servlet-api</artifactId>
        <version>2.5</version>
        <scope>provided</scope>
    </dependency>

    <!-- Servlet -->
    <dependency>
        <groupId>javax.servlet.jsp</groupId>
        <artifactId>jsp-api</artifactId>
        <version>2.1</version>
        <scope>provided</scope>
    </dependency>


  <!-- JSTL -->
    <dependency>
        <groupId>jstl</groupId>
        <artifactId>jstl</artifactId>
        <version>1.2</version>
    </dependency>


    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>


    <!-- DB related dependencies -->
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>${mysql.connector.version}</version>
    </dependency>

    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-core</artifactId>
        <version>${hibernate.version}</version>
    </dependency>

    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-entitymanager</artifactId>
        <version>${hibernate.version}</version>
    </dependency>   

    <!-- Spring related -->


        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-aop</artifactId>
            <version>${org.springframework-version}</version>
            <type>jar</type>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-asm</artifactId>
            <version>${org.springframework-version}</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-beans</artifactId>
            <version>${org.springframework-version}</version>
            <type>jar</type>
        </dependency>


        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context-support</artifactId>
            <version>${org.springframework-version}</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>${org.springframework-version}</version>
            <exclusions>
                <!-- Exclude Commons Logging in favor of SLF4j -->
                <exclusion>
                    <groupId>commons-logging</groupId>
                    <artifactId>commons-logging</artifactId>
                </exclusion>
            </exclusions>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>${org.springframework-version}</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-expression</artifactId>
            <version>${org.springframework-version}</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jdbc</artifactId>
            <version>${org.springframework-version}</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-orm</artifactId>
            <version>${org.springframework-version}</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-transaction</artifactId>
            <version>${org.springframework-version}</version>
        </dependency> 

        <dependency>
          <groupId>org.springframework</groupId>
          <artifactId>org.springframework.web.servlet</artifactId>
          <version>${org.springframework-version}</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
            <version>${org.springframework-version}</version>
        </dependency>


        <!--  AOP Alliance -->
        <dependency>
            <groupId>org.aopalliance</groupId>
            <artifactId>com.springsource.org.aopalliance</artifactId>
            <version>1.0.0</version>
        </dependency>

    <!-- Spring Web MVC Features -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>${org.springframework-version}</version>
        </dependency>

     <!-- Hibernate JTA Jar --> 
        <dependency>
            <groupId>javax.transaction</groupId>
            <artifactId>jta</artifactId>
            <version>1.1</version>
        </dependency>


    <!-- @Inject -->
    <dependency>
        <groupId>javax.inject</groupId>
        <artifactId>javax.inject</artifactId>
        <version>1</version>
    </dependency>


  </dependencies>
  <build>
    <finalName>BookApplication</finalName>
     <plugins>
              <plugin>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <version>2.3.2</version>
                    <configuration>
                      <source>1.6</source>
                      <target>1.6</target>
                    </configuration>
              </plugin>

              <plugin>            
                  <groupId>org.apache.maven.plugins</groupId>
                  <artifactId>maven-war-plugin</artifactId>
                  <configuration>
                     <webXml>${project.basedir}/src/main/webapp/WEB-INF/web.xml</webXml>
                     <!-- ${project.basedir} -->
                    <!-- <failOnMissingWebXml>false</failOnMissingWebXml> -->     
                    <!-- <packagingExcludes>**/web.xml</packagingExcludes>    -->
                    <warName>BookApplication</warName>
                  </configuration>
                </plugin>

              <plugin>
                    <groupId>org.apache.tomcat.maven</groupId>
                    <artifactId>tomcat7-maven-plugin</artifactId>
                    <version>2.0</version>
                    <configuration>
                        <path>/bookApp</path>
                        <!-- <path>/</path> -->
                        <contextFile>${basedir}/src/test/resources/tomcat.xml</contextFile>
                    </configuration>

                    <dependencies>
                        <dependency>
                            <groupId>com.h2database</groupId>
                            <artifactId>h2</artifactId>
                            <version>1.3.168</version>
                        </dependency>
                    </dependencies>
            </plugin>

        </plugins>
  </build>
</project>

我是斯普林斯的新手,非常感谢任何帮助。 提前谢谢。

0 个答案:

没有答案