@Scope:用于指定作用范围
作用:用于指定bean的作用范围
位置:放在实体类上
value:指定范围的取值
常用取值:singleton(单例)和 prototype(多例)
@Scope(“prototype”)
@Component(value="accountService")
@Scope("prototype")
public class AccountServiceImpl implements AccountService{
@Resource(name = "accountDao2")
private AccountDao accountDao;
public void saveAccount() {
accountDao.saveAccount();
}
}
public class Client {
public static void main(String[] args) {
ApplicationContext context =
new ClassPathXmlApplicationContext("bean.xml");
AccountService service =
context.getBean("accountService", AccountService.class);
AccountService service1 =
context.getBean("accountService", AccountService.class);
System.out.println(service==service1);//false
}
}