【发布时间】:2018-01-08 20:34:04
【问题描述】:
为什么注入自动装配的依赖项失败。
严重:上下文初始化失败 org.springframework.beans.factory.BeanCreationException:错误 创建名为“homeController”的bean:注入自动装配 依赖失败;嵌套异常是 org.springframework.beans.factory.BeanCreationException:不能 自动装配字段:com.quickstart.com.springmvc.service.UserService com.quickstart.com.springmvc.controller.HomeController.userservice; 嵌套异常是 org.springframework.beans.factory.NoSuchBeanDefinitionException: 否 匹配类型的bean [com.quickstart.com.springmvc.service.UserService] 找到 依赖项:预计至少有 1 个符合自动装配条件的 bean 这种依赖的候选人。依赖注解: {@org.springframework.beans.factory.annotation.Autowired(required=true)} 在 org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:287) 在 org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1106)
MvcConfiguration.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/");
}
}
HomeController.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;
}
}
userServe.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;
}
}
web.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>
【问题讨论】:
-
请附上你的
UserService班级和问题 -
@sunkuet02 我马上更新
-
你必须启用注解配置这个链接会有帮助[stackoverflow.com/questions/15158046/…
-
我已经通过在
UserServiceImpl类中添加未声明的方法成功地运行了您给定的代码。 -
@sunkuet02 是的:我在服务中犯了非常错误的转储错误。因为公共类 UserServiceImpl 实现了 UserService。我没有用 userService 实现。我纠正了我的错误。并从心底里感谢你
标签: java spring spring-mvc autowired