Java Spring自动布线服务不支持';我的控制器不能工作
在控制器中自动连接服务时出现问题。我发现了这个错误:Java Spring自动布线服务不支持';我的控制器不能工作,java,spring,spring-mvc,Java,Spring,Spring Mvc,在控制器中自动连接服务时出现问题。我发现了这个错误: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'myController': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException:
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'myController': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private es.unican.meteo.service.UserService es.unican.meteo.controller.MyController.userService; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No matching bean of type [es.unican.meteo.service.UserService] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
似乎userService没有注册,因此控制器无法获取bean。我认为我的配置还可以,因为它可以与测试一起工作。在测试中,我有以下几点:
ClassPathXmlApplicationContext("/WEB-INF/app-config.xml");
我可以从ApplicationContext.xml获取bean ok
我的包结构如下所示:
E.unican.meteo.controller
|----MyController.java
E.unican.meteo.service
|----UserService.java
e.unican.meteo.service.impl
|----UserServiceImpl.java
WebContent/WEB-INF
|----MyDispatcherServlet-servlet.xml
|----app-config.xml
|----web.xml
类别:
==UserServiceImpl.java==
@Service
public class UserServiceImpl implements UserService{
@Autowired
private UserMapper userMapper;
public void setUserMapper(UserMapper userMapper) {
this.userMapper = userMapper;
}
==MyController.java==
@Controller
public class MyController {
@Autowired
private UserService userService;
@RequestMapping(method=RequestMethod.GET, value="/home")
public String handleRequest(){
return "welcome";
}
@RequestMapping(method=RequestMethod.GET, value="/getUsers")
public @ResponseBody List<User> getUsersInJSON(){
return userService.getUsers();
}
}
我已经复习了一些关于这个话题的问题,但是我没有找到解决问题的方法。也许我跳过了什么,但我不确定。我试图更改组件扫描,但没有结果
当我尝试访问/SPRING-MVC/getUsers.go时,会出现这些错误
我不知道bean是否必须放在app config(applicationContext)或servlet.xml中,因为这有点让人困惑
谢谢您需要更改在控制器中自动连接服务的方式 更改以下代码
@Autowired
private UserService userService;
有以下几点
@Resource(name="userService")
private UserService userService;
因为在UserServiceImpl中,您定义了别名为“userService”的@Service注释
我希望这能解决您的问题。:) 您可以使用
@限定符注释,如下所示:
@Autowired
@Qualifier("userService")
private UserService userService;
确保您的userserviceinpl
与context:component scan
中定义的包在同一个包中。如果不是,spring将无法检测到它。另外,尝试从userserviceinpl
定义中删除value属性,因为只有一个该类型的bean。Spring将能够按类型自动连接。乍一看,配置似乎正常,但可能有一些较小的tripwires可能不太明显
a) 实现了UserService
接口,是否与控制器需求相同?这是个愚蠢的问题,我知道,但请注意安全
b) bean名称:尝试从@Service
注释中删除值(ba da tush),不管怎样,它都是超级值。或者在@限定符的帮助下更加具体
c) 包扫描:仔细检查您实施的服务是否真的在es.unican.meteo
内。有时是小事情。您的配置非常奇怪
首先,排除显而易见的因素
我在web.xml
中没有看到根web应用程序上下文配置。可能是您忘记添加这段代码了吗
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
WEB-INF/app-config.xml
</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
上下文配置位置
WEB-INF/app-config.xml
org.springframework.web.context.ContextLoaderListener
现在有一点理论
一点Spring理论-Spring使用web应用程序的应用程序上下文层次结构:
- 顶级web应用程序上下文由
ContextLoaderListener加载
- 然后,每个
DispatcherServlet
实例都有单独的上下文
当一个新bean被实例化时,它可以从定义它的上下文或父上下文中获取依赖项。这使得可以在根上下文(服务、DAO等)中定义公共bean,并在servlet应用程序上下文中拥有请求处理bean,因为每个servlet都可以拥有自己的一组控制器、视图处理程序等
最后,但并非最不重要——你的错误
您正在根上下文中配置MVC。这是错误的。删除当您遇到此类问题时,请检查context:component scan basepackage的路径
它应该是根名称,就像我将com.mike作为包名一样&它的结构中包含bean、controller、dao、service文件夹,在这种情况下,您必须遵循这样的内容----context:component scan basepackage=“com.mike.*”
其中*表示将扫描所有文件夹(bean、服务、dao、控制器和它们对应的类)。在服务上添加@Component注释。除非只有一个这种类型的豆子,我相信它不会引起任何问题。谢谢你的回答天蝎座!a) UserServiceImpl实现UserService,这是需要MyController.java(UserService)b)的服务。您想说不包括@service(value=“UserService”),只包括@service吗?c) 我的打包扫描是这样的:UserService和userserviceinpl位于es.unican.meteo中。es.unican.meteo.service中的第一个和es.unican.meteo.service.implTo b中的第二个:是的,@service
应该足够了,或者如果autowire by name失败,您可以添加@Autowired
限定符(“userService”)
来进一步指定bean。c:听起来不错。首先,谢谢你的回复。关于“根web应用程序上下文配置”,我正在eclipse中开发该项目,以便您可以将contextroot配置为项目的属性。这是你想说的吗?您的意思是我必须将app-config.xml中的“”替换为“”,因为我正在根目录(app-config)和servlet中注册控制器?非常感谢。我不明白您在Eclipse中指定contextroot是什么意思。Contextroot(即servlet应用程序的基本URI)与根web应用程序上下文(Spring容器)不同。您的web.xml
(或使用WebappInitializer
)中需要有ContextLoaderListener
。现在,我明白了。我将侦听器包含在web.xml中,并删除了applicationContext中对mvc的所有引用。现在它起作用了。谢谢你,如果答案有帮助,你能接受吗
@Autowired
private UserService userService;
@Resource(name="userService")
private UserService userService;
@Autowired
@Qualifier("userService")
private UserService userService;
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
WEB-INF/app-config.xml
</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>