spitter服务异常

时间:2011-10-19 14:16:43

标签: java spring spring-mvc maven annotations

我在服务层中不断收到NoSuchBeanDefinitionException,我不明白为什么,任何帮助都会受到赞赏。这是春天的动作书,spitter应用程序。我错过了什么。我正在尝试使用maven而不是gradle来构建它。

例外:

org.springframework.beans.factory.BeanCreationException: Error creating bean witlerMapping#0': Initialization of bean failed; 
nested exception is org.springfram name 'homeController' defined in file [/data/TOMCAT-6.0/apache-tomcat-6.0.33/wess]: Unsatisfied dependency expressed through constructor argument with index 0  of type [com.john.springinaction.service.SpitterService] found for dependency: ndency. Dependency annotations: {}; 
nested exception is org.springframework.beanspringinaction.service.SpitterService] found for dependency: expected at least 1annotations: {}

    at org.springframework.beans.factory.support.AbstractAutowireCapableBean
    at org.springframework.beans.factory.support.AbstractAutowireCapableBean
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getOb
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistr
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBe
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.
    at org.springframework.context.support.AbstractApplicationContext.finish
    at org.springframework.context.support.AbstractApplicationContext.refres
    at org.springframework.web.servlet.FrameworkServlet.createWebApplication
    at org.springframework.web.servlet.FrameworkServlet.createWebApplication
    at org.springframework.web.servlet.FrameworkServlet.initWebApplicationCo
    at org.springframework.web.servlet.FrameworkServlet.initServletBean(Fram
    at org.springframework.web.servlet.HttpServletBean.init(HttpServletBean.
    at javax.servlet.GenericServlet.init(GenericServlet.java:212)
    at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.
    at org.apache.catalina.core.StandardWrapper.allocate(StandardWrapper.jav
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperV
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextV
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.j
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.j
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineVal
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.jav
    at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java
    at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.proce
    at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:48
    at java.lang.Thread.run(Thread.java:619)

控制器:

package com.john.springinaction.mvc;

import java.util.Map;

import javax.inject.Inject;

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

import com.john.springinaction.service.SpitterService;

@Controller
public class HomeController {

    public static final int DEFAULT_SPITTLES_PER_PAGE = 25;

    @Autowired
    private SpitterService spitterService;


    @RequestMapping({"/","/home"})
    public String showHomePage(Map<String, Object> model) {
        model.put("spittles", 
            spitterService.getRecentSpittles(DEFAULT_SPITTLES_PER_PAGE));
        return "home";
    }
}

服务:

package com.john.springinaction.service;

import static java.lang.Math.min;

import java.util.Date;
import java.util.List;

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

import com.john.springinaction.domain.Spitter;
import com.john.springinaction.domain.Spittle;
import com.john.springinaction.persistence.SpitterDAO;

@Service("spitterService")
@Transactional(propagation = Propagation.REQUIRED)
public class SpitterServiceImpl implements SpitterService {

    @Autowired
    private SpitterDAO spitterDAO;

    @Transactional(propagation=Propagation.SUPPORTS, readOnly=true)
    public List<Spittle> getRecentSpittles(int count) {
        List<Spittle> recentSpittles =
            spitterDAO.getRecentSpittle();
        return recentSpittles.subList(0, min(49, recentSpittles.size()));
    }

    public void saveSpittle(Spittle spittle) {
        spittle.setWhen(new Date());
        spitterDAO.saveSpittle(spittle);

    }

    @Transactional(propagation=Propagation.SUPPORTS, readOnly=false)
    public void saveSpitter(Spitter spitter) {
        if (spitter.getId() == null) {
            spitterDAO.addSpitter(spitter);
        }else {         
            spitterDAO.saveSpitter(spitter);
        }
    }

    @Transactional(propagation=Propagation.SUPPORTS, readOnly=false)
    public void deleteSpitter(Spitter spitter) {
        spitterDAO.deleteSpitter(spitter);

    }

    @Transactional(propagation = Propagation.SUPPORTS, readOnly = true)
    public Spitter getSpitter(long id) {
        return spitterDAO.getSpitterById(id);
    }

    public void startFollowing(Spitter follower, Spitter followee) {

    }

    public List<Spittle> getSpittlesForSpitter(Spitter spitter) {
         return spitterDAO.getSpittlesForSpitter(spitter);
    }

    public List<Spittle> getSpittlesForSpitter(String username) {
        return spitterDAO.getSpittlesForSpitter(username);
    }

    public Spitter getSpitter(String username) {
        return spitterDAO.getSpitterByUsername(username);
    }

    public Spittle getSpittleById(long id) {
        return spitterDAO.getSpittleById(id);
    }

    public void deleteSpittle(long id) {
        spitterDAO.deleteSpittle(id);

    }

    public List<Spitter> getAllSpitters() {
        return null;
    }


}

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

    <!-- Database Configuration -->
    <import resource="DataSource.xml"/>
    <import resource="Hibernate.xml"/>
    <import resource="Service.xml"/>

    <!-- Auto scan the components -->
    <context:component-scan base-package="com.john.springinaction" />
    <context:annotation-config/>
</beans>

hibernate.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:tx="http://www.springframework.org/schema/tx"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
    http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/tx
    http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-3.0.xsd">

    <context:component-scan base-package="com.john.springinaction.service" />

    <tx:annotation-driven/>
</beans>

的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" 
         version="2.4" 
         xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee 
                   http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">

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

    <!-- <listener>
        <listener-class>org.apache.tiles.web.startup.TilesListener</listener-class>
    </listener> -->

    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>
            classpath:*BeanLocations.xml
        </param-value>
    </context-param>
    <servlet>
        <servlet-name>spitter</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>

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

</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.john.springinaction</groupId>
  <artifactId>Spitter</artifactId>
  <packaging>war</packaging>
  <version>1.0-SNAPSHOT</version>
  <name>Spitter</name>
  <url>http://maven.apache.org</url>

  <repositories>
    <repository>
      <id>JBoss repository</id>
      <url>http://repository.jboss.com/maven2/</url>
    </repository>
  </repositories>

  <properties>
    <spring.version>3.0.5.RELEASE</spring.version>
  </properties>
  <dependencies>

  <!-- Test Dependencies -->
    <dependency>  
        <groupId>junit</groupId>  
        <artifactId>junit</artifactId>  
        <version>4.7</version>  
        <scope>test</scope>  
    </dependency>  

    <!-- Spring framework -->
    <!-- <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring</artifactId>
        <version>2.5.6</version>
    </dependency> -->

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

    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
        <version>3.0.6.RELEASE</version>
    </dependency>

    <!-- Spring AOP Dependency -->
    <dependency>
        <groupId>cglib</groupId>
        <artifactId>cglib</artifactId>
        <version>2.2</version>
    </dependency>

    <!-- Spring MVC framework -->

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

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


    <!-- MySQL database driver -->
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>5.1.9</version>
    </dependency>

    <!-- Hibernate framework -->
     <dependency>
       <groupId>org.hibernate</groupId>
       <artifactId>hibernate-core</artifactId>
       <version>3.3.2.GA</version>
    </dependency>

    <!-- Hibernate annotation -->
    <dependency>
       <groupId>org.hibernate</groupId>
       <artifactId>hibernate-annotations</artifactId>
       <version>3.4.0.GA</version>
    </dependency>
    <dependency>
       <groupId>org.hibernate</groupId>
       <artifactId>hibernate-commons-annotations</artifactId>
       <version>3.3.0.ga</version>
    </dependency>

    <!-- Hibernate library dependecy start -->
    <dependency>
        <groupId>dom4j</groupId>
        <artifactId>dom4j</artifactId>
        <version>1.6.1</version>
    </dependency>

    <dependency>
        <groupId>commons-logging</groupId>
        <artifactId>commons-logging</artifactId>
        <version>1.1.1</version>
    </dependency>

    <dependency>
        <groupId>commons-collections</groupId>
        <artifactId>commons-collections</artifactId>
        <version>3.2.1</version>
    </dependency>

    <dependency>
        <groupId>antlr</groupId>
        <artifactId>antlr</artifactId>
        <version>2.7.7</version>
    </dependency>

    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-log4j12</artifactId>
        <version>1.5.6</version>
    </dependency>

    <dependency>
        <groupId>javassist</groupId>
        <artifactId>javassist</artifactId>
        <version>3.12.1.GA</version>
    </dependency>
    <!-- Hibernate library dependecy end -->

    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-jdbc</artifactId>
        <version>3.0.6.RELEASE</version>
    </dependency>

    <dependency>
        <groupId>commons-lang</groupId>
        <artifactId>commons-lang</artifactId>
        <version>2.3</version>
    </dependency>

    <dependency>
        <groupId>javax.validation</groupId>
        <artifactId>validation-api</artifactId>
        <version>1.0.0.GA</version>
    </dependency>

    <dependency>
        <groupId>com.sun.jersey.glassfish.v3.osgi</groupId>
        <artifactId>jackson-core-asl</artifactId>
        <version>1.1.5</version>
    </dependency>

    <dependency>  
        <groupId>javax.inject</groupId>  
        <artifactId>javax.inject</artifactId>  
        <version>1</version>  
    </dependency>

  </dependencies>

  <build>
    <plugins>
        <plugin>
            <artifactId>maven-compiler-plugin</artifactId>
            <configuration>
                <source>1.5</source>
                <target>1.5</target>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-war-plugin</artifactId>
            <version>2.1.1</version>
            <configuration>
                <outputDirectory>target/artefacts/wars</outputDirectory>
                <warName>spitter</warName>
            </configuration>
        </plugin>
    </plugins>
  </build>
</project>

2 个答案:

答案 0 :(得分:0)

您似乎可能在不同的包中有SpitterService。尝试通过以下任一方式在控制器中指定您想要的那个:

@Autowired
@Qualifier( "spitterService" )
private SpitterService spitterService;

@Resource( name="spitterService" )
private SpitterService spitterService;

答案 1 :(得分:0)

我认为你还没有写过其他必要的类和接口,比如Spitter,Spittle和其他服务类请先创建这些类