开发者社区 问答 正文

spring 注解开发与配置文件混用问题

spring 注解开发与配置文件混用问题

展开
收起
游客pklijor6gytpx 2019-12-04 13:59:56 913 分享 版权
1 条回答
写回答
取消 提交回答
  • @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;
        }
    
    
    2019-12-04 14:00:26
    赞同 展开评论