Spring boot@Autowired到底该怎么使用那set和构造方法
文章目录
@Component
//@Component("searchService")
@Scope("singleton")
public class SearchService {
1.@Autowired 描述属性
@Autowired
private Cache cache;
public SearchService() {
System.out.println("SearchService()");
}
//2.@Autowired 描述set方法(通常配合无参数构造函数使用)
@Autowired
public void setCache( @Qualifier("softCache")Cache cache) {
System.out.println("setCache()");
this.cache = cache;
}
3.@Autowired 描述构造方法
@Autowired //描述构造方法时这个@Autowired注解可以省略
public SearchService(@Qualifier("softCache") Cache cache) {
System.out.println("SearchService( Cache cache) ");
this.cache=cache;
}
/**外界通过此方法获取由spring容器为SearchService注入的cache对象*/
public Cache getCache() {
return cache;
}}

评论
登录后参与技术讨论,分享你的经验与见解。
登录 / 注册暂无评论