当前位置: 移动技术网 > IT编程>开发语言>Java > @Autowired自动注入失败

@Autowired自动注入失败

2019年06月22日  | 移动技术网IT编程  | 我要评论
新手注意的问题

package cn.ryq.web.controller;

import cn.ryq.domain.company.company;
import cn.ryq.service.company.companyservice;
import org.springframework.beans.factory.annotation.autowired;
import org.springframework.stereotype.controller;
import org.springframework.web.bind.annotation.requestmapping;
import org.springframework.web.servlet.modelandview;

import java.util.date;
import java.util.list;

@controller
@requestmapping("/system/company")
public class companycontroller {
@autowired
public companyservice companyservice;

@requestmapping("/list.do")
public modelandview list(){
list<company> list = companyservice.findall();
modelandview mv= new modelandview();
mv.addobject("list",list );
mv.setviewname("company/company-list");
return mv;
}
}
在确保applicationcontext-service.xml,有扫包(可以通过右边的小叶子图标来查看是否有扫到包)companycontroller控制器中有@controller,再检查web.xml的配置,
一般情况下我们都是用多个模块去完成一个项目,那么web.xml的context-param就如下配置,
<context-param>
<param-name>contextconfiglocation</param-name>
<param-value>classpath*:spring/applicationcontext-*.xml</param-value>
</context-param>
还有注意监听器的配置,很多新手对这个配置不熟练,会忘记配置~
<listener>
<listener-class>org.springframework.web.context.contextloaderlistener</listener-class>
</listener>

加载配置文件的路径加*,让web可以扫到整个项目的配置文件!

补充点:发现有些同学的pojo类中没有get和set方法,这会导致数据库无法获取到数据,更加无法自动注入了
如果还有更多问题欢迎来访,相互学习成长,谢谢!

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网