首页 > @Autowired写在成员变量上和set方法上的区别?

@Autowired写在成员变量上和set方法上的区别?

今天项目上遇到一个问题,service是普通的类,非接口实现类,如下

@Service
@Transactional
@OALogger
public class ArchiveSortManager {
..........
}

这样居然会报错

private ArchiveSortManager sortManager;

@Autowired
public void setSortManager(ArchiveSortManager sortManager) {
this.sortManager = sortManager;
}

而如下这样注入却正常

@Autowired
private ArchiveSortManager sortManager;

求解


效果是一样的

Marks a constructor, field, setter method or config method as to be autowired by Spring's dependency injection facilities.

PS: 使用@Autowired的属性无需生成setter方法,推荐直接在属性上进行@Autowired

【热门文章】
【热门文章】