Spitter不满意的依赖异常Spring MVC

问题描述 投票:0回答:5

我在Spring MVC中使用Book Spring in Action开始,我正在练习第5章,使用Spitter应用程序,但是我收到了以下错误:

org.springframework.beans.factory.UnsatisfiedDependencyException:在文件[C:\ xampp \ htdocs.metadata.plugins \ org.eclipse.wst.server.core \ tmp1 \ wtpwebapps \ spitter \ WEB中定义的名称为“spittleController”的bean创建时出错-INF \ classes \ com \ spitter \ web \ SpittleController.class]:通过构造函数参数表示的不满意的依赖关系,类型为[com.spitter.data.SpittleRepository]的索引0 ::没有类型为[com.spitter.data的限定bean]。 SpittleRepository]发现依赖:预期至少有1个bean可以作为此依赖项的autowire候选者。依赖注释:{};嵌套异常是org.springframework.beans.factory.NoSuchBeanDefinitionException:没有为依赖项找到类型为[com.spitter.data.SpittleRepository]的限定bean:期望至少有一个bean可以作为此依赖项的autowire候选者。依赖注释:{}

我在github得到了这个项目:https://github.com/kevingcfcb88/spitter.git

我已经做了我的研究,但似乎没有任何效果。

我正在使用STS和Maven,这是应用程序的结构:

这是我的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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>com.spitter.config</groupId>
<artifactId>spitter</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>

<name>spitter</name>
<url>http://maven.apache.org</url>

<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <springframework.version>4.1.5.RELEASE</springframework.version>
</properties>

<dependencies>
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>3.8.1</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-webmvc</artifactId>
        <version>${springframework.version}</version>
    </dependency>
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>javax.servlet-api</artifactId>
        <version>3.1.0</version>
    </dependency>
    <dependency>
        <groupId>javax.servlet.jsp</groupId>
        <artifactId>javax.servlet.jsp-api</artifactId>          
        <version>2.3.1</version>
    </dependency>
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>jstl</artifactId>
        <version>1.2</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/org.apache.commons/commons-lang3 -->
    <dependency>
        <groupId>org.apache.commons</groupId>
        <artifactId>commons-lang3</artifactId>
        <version>3.0</version>
    </dependency>

</dependencies>

<build>
    <pluginManagement>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.2</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-war-plugin</artifactId>
                <version>2.4</version>
                <configuration>
                    <warSourceDirectory>src/main/webapp</warSourceDirectory>
                    <warName>spitter</warName>
                    <failOnMissingWebXml>false</failOnMissingWebXml>
                </configuration>
            </plugin>
        </plugins>
    </pluginManagement>
    <finalName>spitter</finalName>
</build>

我的配置文件:

spit填入Web app initializer.Java

 package com.spitter.config;

import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;

public class SpittrWebAppInitializer extends AbstractAnnotationConfigDispatcherServletInitializer{

    @Override
    protected Class<?>[] getRootConfigClasses() {
        return new Class<?>[] { RootConfig.class };
    }

    @Override
    protected Class<?>[] getServletConfigClasses() {
        return new Class<?>[] { WebConfig.class };
    }

    @Override
    protected String[] getServletMappings() {
        return new String[] { "/" };
    }

}

Web config.Java

package com.spitter.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.ViewResolver;
import org.springframework.web.servlet.config.annotation.DefaultServletHandlerConfigurer;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import org.springframework.web.servlet.view.InternalResourceViewResolver;

@Configuration
@EnableWebMvc
@ComponentScan("com.spitter.web")
public class WebConfig extends WebMvcConfigurerAdapter {

    @Bean
    public ViewResolver viewResolver() {
        InternalResourceViewResolver resolver = new InternalResourceViewResolver();
        resolver.setPrefix("/WEB-INF/views/");
        resolver.setSuffix(".jsp");
        resolver.setExposeContextBeansAsAttributes(true);
        return resolver;
    }

    @Override
    public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
        configurer.enable();
    }

}

root config.Java

package com.spitter.config;

import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.ComponentScan.Filter;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.FilterType;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;

@Configuration
@ComponentScan(basePackages = { "com.spitter.data" }, excludeFilters = {@Filter(type = FilterType.ANNOTATION, value = EnableWebMvc.class) })
public class RootConfig {

}
java spring spring-mvc action
5个回答
0
投票

我知道这段代码。 2年前它曾经给我带来严重的头痛。错误在代码或书本身。

转到发布者网站并下载更新的源代码。 here

要么

https://www.manning.com/books/spring-in-action-fourth-edition


3
投票

正如@ mh-dev解​​释的那样,你需要实现SpittleRepository。尝试添加此类,看看您的代码是否可以运行:

public class SpittleRepositoryImpl implements SpittleRepository {

  List <Spittle> findSpittles(long max, int count) {
    System.out.println("I need a real implementation! " 
      + "I received max as " + max + " and count as " + count + ".");
  }

}

我建议重新阅读本书的相关部分,以确保你没有遗漏任何东西。


1
投票

你只需要在头文件中添加一个带有@Repository的SpittleRepository的存储库实现,因为即使你已经添加了实现类,spring IOC也不知道实际的依赖性,如果你把所有的java文件放在了本书中的例子应该有用在具有存储库实现的相同包层次结构中。

@Repository
public class SpittleRepositoryDAO implements SpittleRepository {

    public SpittleRepositoryDAO() {

    }

    @Override
    public List<Spittle> findSpittles(long max, int count) {
        List<Spittle> spittles = new ArrayList<Spittle>();
        for (int i = 0; i < count; i++) {
            spittles.add(new Spittle("Spittle " + i, new Date()));
        }
        return spittles;
    }
}

-1
投票

以下是存储库的实现:

package com.spitter.web;

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

import com.spitter.data.SpittleRepository;

@Controller
@RequestMapping("/spittles")
public class SpittleController {

    private SpittleRepository spittleRepository;

    @Autowired
    public SpittleController(SpittleRepository spittleRepository) {
        this.spittleRepository = spittleRepository;
    }

    @RequestMapping(method = RequestMethod.GET)
    public String spittles(Model model) {
        model.addAttribute("spittleList", spittleRepository.findSpittles(Long.MAX_VALUE, 20));
        return "spittles";
    }

}

-1
投票

是的,问题是你需要一个SpittleRepository的实现。因为你知道,因为你不能把接口作为bean。扫描组件至少需要一个实现,并且需要注释@Component。而已 ;)

© www.soinside.com 2019 - 2024. All rights reserved.