2011-10-19 151 views
0

我一直在服務層得到一個NoSuchBeanDefinitionException異常,我不知道爲什麼,任何幫助將不勝感激。這是Spitter應用程序的行動書中的春天。我錯過了什麼。我想用maven而不是gradle自己構建它。spitter服務異常

例外:

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> 
+0

您是否也可以發佈'Service.xml'? – tolitius

回答

0

看來你可能在不同的包SpitterService。嘗試通過任一指定要到底是哪一個控制器:

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

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

謝謝你,@Resource已經工作過。 – john

0

我覺得你還沒有編寫其他必要的類和接口,像Spitter,唾沫等服務請先創建這些類別