当前位置: 首页 > 知识库问答 >
问题:

上下文初始化失败。springframework。豆。工厂BeanCreationException:创建名为“homeController”的bean时出错

赫连靖琪
2023-03-14

为什么自动加载依赖项的注入失败。

严重:上下文初始化失败。springframework。豆。工厂BeanCreationException:创建名为“homeController”的bean时出错:自动连线依赖项的注入失败;嵌套的异常是org。springframework。豆。工厂BeanCreationException:无法自动关联字段:com。快速启动。通用域名格式。springmvc。服务用户服务公司。快速启动。通用域名格式。springmvc。控制器。家庭控制器。用户服务;嵌套的异常是org。springframework。豆。工厂NoSuchBeanDefinitionException:没有为依赖项找到类型为[com.quickstart.com.springmvc.service.UserService]的匹配bean:至少需要1个符合此依赖项autowire候选项条件的bean。依赖项注释:{@org.springframework.beans.factory.annotation.Autowired(required=true)}。springframework。豆。工厂注释。AutoWiredNotationBeanPostProcessor。org上的PostProcessPropertyValue(AutowiredNotationBeanpostProcessor.java:287)。springframework。豆。工厂支持AbstractAutoWireAbleBeanFactory。populateBean(AbstractAutowireCapableBeanFactory.java:1106)

MVC配置。JAVA

package com.quickstart.com.springmvc.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.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import org.springframework.web.servlet.view.InternalResourceViewResolver;

@Configuration
@ComponentScan(basePackages="com.quickstart.com.springmvc")
@EnableWebMvc
public class MvcConfiguration extends WebMvcConfigurerAdapter{

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

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/resources/**").addResourceLocations("/resources/");
    }
}

家庭控制器。JAVA

package com.quickstart.com.springmvc.controller;

import java.io.IOException;
import java.util.List;

import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import com.quickstart.com.springmvc.model.User;
import com.quickstart.com.springmvc.service.UserService;

@Controller

public class HomeController {

    @Autowired
    UserService userservice;

    @RequestMapping(value="/ved")
    public ModelAndView test(HttpServletResponse response) throws IOException{
        System.out.println("home");
        return new ModelAndView("home");
    }

    @RequestMapping(value="/myfun")

    public ModelAndView fun(HttpServletResponse response) throws IOException {
        System.out.println("myfun");
        return new ModelAndView();
    }

    @RequestMapping(value="/user/" , method=RequestMethod.GET)
    public ResponseEntity<List<User>> listAllUser() {

        List<User> users = userservice.findAllUsers();
        System.out.println("ammm");
        if(users.isEmpty()){
            return new ResponseEntity<List<User>>(HttpStatus.NO_CONTENT);
        }

        else{
            return new ResponseEntity<List<User>>(users,HttpStatus.OK);
        }
    }
}

User.java

package com.quickstart.com.springmvc.model;

public class User {

    private int id;

    private String name;

    private String email;

    private int age;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }
}

用户服务。JAVA

package com.quickstart.com.springmvc.service;

import java.util.List;

import org.springframework.context.annotation.Bean;

import com.quickstart.com.springmvc.model.User;


public interface UserService {

    User findById(long id);

    User findByName(String name);

    void saveUser(User user);

    void updateUser(User user);

    void deleteUserById(long id);

    List<User> findAllUsers(); 

    void deleteAllUsers();

    public boolean isUserExist(User user);

}

UserServiceImpl。JAVA

package com.quickstart.com.springmvc.service;

import java.util.List;

import org.springframework.stereotype.Service;

import com.quickstart.com.springmvc.model.User;

@Service("userService")
public class UserServiceImpl implements UserService {

    private List<User> user;

    /*User findById(long id){

        return User;
    }*/

    List<User> findAllUsers(){
        System.out.println("my implemetion");

        return user;
    }

}

网状物xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
    <display-name>springmvc</display-name>
    <context-param>
        <param-name>contextClass</param-name>
        <param-value>
            org.springframework.web.context.support.AnnotationConfigWebApplicationContext
        </param-value>
    </context-param>
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

    <servlet>
        <servlet-name>SpringDispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextClass</param-name>
            <param-value>
                org.springframework.web.context.support.AnnotationConfigWebApplicationContext
            </param-value>
        </init-param>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>com.quickstart.com.springmvc</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>SpringDispatcher</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

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

共有1个答案

杨雪松
2023-03-14

我在我的机器上运行你的代码。一切都很好。请检查下面的行,了解更多关于@Service@Autowire的信息

在用户服务上方添加@Service注释,并正确实施。

 类似资料: