Spring 框架bug总结
spring 注解开发与配置文件混用问题
@Component 与 @Autowired等一般要一起用,如果用了@Component又要去配置文件中进行此类的set或者构造注入是行不通的的,因为@Component已经是代表创建此类对象,配置文件中的又会是另一个对象。
@RequestMapping("/role")
@Controller
public class RoleController {
@Autowired //*****
private RoleService roleService;
@RequestMapping("/list")
public ModelAndView list() {
System.out.println("reach ---> list");
ModelAndView modelAndView = new ModelAndView();
List<Role> roleList = roleService.list();
//设置模型
modelAndView.addObject("roleList", roleList);
//设置视图
modelAndView.setViewName("role-list");
return modelAndView;
}
spring 注解有时候@Autowired注入下方对象报红的问题
- 看看自己的配置中注解的扫描范围是否对了
- 自动写入的时候把接口写成实现类了
@Autowired
private BrandServiceImpl brandServiceImpl;
应该是
@Autowired
private BrandService brandService ; - 在BrandDao 类上加上@Repository注解