@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;
 }

}


CV战士
7 声望1 粉丝