标签:cte prot tco 对象类型 单例 对象销毁 lease title inter
明确:写在前面
学习基于注解的 IOC 配置,大家脑海里首先得有一个认知,即注解配置和 xml 配置要实现的功能都是一样的,都是要降低程序间的耦合。只是配置的形式不一样。
关于实际的开发中到底是用 xml 还是注解,每家公司有着不同的使用习惯,所以这两者配置方式我们都需要掌握。
在讲解注解配置时,采用上一章案例,吧 spring 的 xml 配置内容改为使用注解逐步实现。
spring-core路劲 按住 ctrl + f 搜索 xmlns:context
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
https://www.springframework.org/schema/context/spring-context.xsd">
<!--告知 spring 在创建容器时要扫描的包,配置所需要的标签不是在 beans 的约束中,而是一个名称为 context 名称空间和约束中-->
<context:component-scan base-package="com"/>
</beans>
public interface IAccountService {
void saveAccount();
}
//@Component(value = "accountServiceImpl") 指定 id
@Component
public class AccountServiceImpl implements IAccountService {
private IAccountDao accountDao;
public AccountServiceImpl(){
System.out.println("AccountServiceImpl 对象创建了");
}
public void saveAccount() {
accountDao.saveAccount();
}
}
public class Client {
public static void main(String[] args) {
//1.获取核心容器对象
ApplicationContext ac=new ClassPathXmlApplicationContext("bean.xml");
//2.根据 id 获取 bean 对象
IAccountService as=ac.getBean("accountServiceImpl",IAccountService.class);
System.out.println(as);
}
}
接下来我们调用方法
public class Client {
public static void main(String[] args) {
//1.获取核心容器对象
ApplicationContext ac=new ClassPathXmlApplicationContext("bean.xml");
//2.根据 id 获取 bean 对象
IAccountService as=ac.getBean("accountServiceImpl",IAccountService.class);
System.out.println(as);
as.saveAccount();
}
}
结果为空指针异常
AccountServiceImpl 对象创建了
com.service.Impl.AccountServiceImpl@1f010bf0
Exception in thread "main" java.lang.NullPointerException
at com.service.Impl.AccountServiceImpl.saveAccount(AccountServiceImpl.java:26)
at com.ui.Client.main(Client.java:24)
@Component(value = "accountServiceImpl")
public class AccountServiceImpl implements IAccountService {
@Autowired
private IAccountDao accountDao;
public AccountServiceImpl(){
System.out.println("AccountServiceImpl 对象创建了");
}
public void saveAccount() {
accountDao.saveAccount();
}
}
结果可以正常输出
如果不止一个 IAccountDao 的实现类,而是有多个怎么办?
@Repository("accountDao1")
public class AccountDaoImpl implements IAccountDao {
public void saveAccount() {
System.out.println("保存了");
}
}
@Repository("accountDao2")
public class AccountDaoImpl2 implements IAccountDao {
public void saveAccount() {
System.out.println("保存了");
}
}
运行结果出错
Caused by: org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type 'com.dao.IAccountDao' available: expected single matching bean but found 2: accountDao1,accountDao2
解决:
修改 AccountServiceImpl 中 IAccountDao 数据类型的变量名 为 accountDao1,运行结果成功
@Service(value = "accountServiceImpl")
public class AccountServiceImpl implements IAccountService {
@Autowired
private IAccountDao accountDao1;
public AccountServiceImpl(){
System.out.println("AccountServiceImpl 对象创建了");
}
public void saveAccount() {
accountDao1.saveAccount();
}
}
@Qualifier:
@Service(value = "accountServiceImpl")
public class AccountServiceImpl implements IAccountService {
@Autowired
@Qualifier("accountDao1")
private IAccountDao accountDao;
public AccountServiceImpl(){
System.out.println("AccountServiceImpl 对象创建了");
}
public void saveAccount() {
accountDao.saveAccount();
}
}
@Resource:
@Service(value = "accountServiceImpl")
public class AccountServiceImpl implements IAccountService {
@Resource(name = "accountDao2")
private IAccountDao accountDao;
public AccountServiceImpl(){
System.out.println("AccountServiceImpl 对象创建了");
}
public void saveAccount() {
accountDao.saveAccount();
}
}
@Service(value = "accountServiceImpl")
@Scope(value = "prototype")
public class AccountServiceImpl implements IAccountService {
/* @Autowired
@Qualifier("accountDao1")*/
@Resource(name = "accountDao2")
private IAccountDao accountDao;
public AccountServiceImpl(){
System.out.println("AccountServiceImpl 对象创建了");
}
public void saveAccount() {
accountDao.saveAccount();
}
}
public class Client {
public static void main(String[] args) {
//1.获取核心容器对象
ApplicationContext ac=new ClassPathXmlApplicationContext("bean.xml");
//2.根据 id 获取 bean 对象
IAccountService as1=ac.getBean("accountServiceImpl",IAccountService.class);
IAccountService as2=ac.getBean("accountServiceImpl",IAccountService.class);
System.out.println(as1==as2);
}
}
@Service(value = "accountServiceImpl")
@Scope(value = "prototype")
public class AccountServiceImpl implements IAccountService {
/* @Autowired
@Qualifier("accountDao1")*/
@Resource(name = "accountDao2")
private IAccountDao accountDao;
@PostConstruct
public void init(){
System.out.println("初始化方法执行了");
}
@PreDestroy
public void destroy(){
System.out.println("销毁方法执行");
}
public void saveAccount() {
accountDao.saveAccount();
}
}
public class Client {
public static void main(String[] args) {
//1.获取核心容器对象
ClassPathXmlApplicationContext ac=new ClassPathXmlApplicationContext("bean.xml");
//2.根据 id 获取 bean 对象
IAccountService as1=ac.getBean("accountServiceImpl",IAccountService.class);
ac.close();
}
}
为什么这么写?
ClassPathXmlApplicationContext ac=new ClassPathXmlApplicationContext("bean.xml");
如果你把一个子类看作父类,那么就只能调用父类的方法,这个时候就不能调用 close() 方法
注意:
然而结果并没有调用销毁方法,这是因为这是一个多例对象,看实现类上面 @Scope(value = "prototype") ,多例对象销毁 spring 是不负责的,所以应该改为单例对象。
标签:cte prot tco 对象类型 单例 对象销毁 lease title inter
原文地址:https://www.cnblogs.com/zuiren/p/11429714.html