目录
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>spring02</groupId>
<artifactId>spring02</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<dependencies>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.1.9.RELEASE</version>
</dependency>
</dependencies>
</project>
将上次的耦合和解耦的笔记放进去
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd">
<!--把对象的创建交给 spring 来管理-->
<bean id="accountService" class="com.service.impl.AccountServiceImpl"/>
<bean id="accountDao" class="com.dao.impl.AccountDaoImpl"/>
</beans>
public class Client {
/**
* 获取 spring 的 ioc 核心容器,并根据 id 获取对象
* @param args
*/
public static void main(String[] args) {
ApplicationContext ac=new ClassPathXmlApplicationContext("bean.xml");
IAccountService accountService=(IAccountService)ac.getBean("accountService");
IAccountDao accountDao=ac.getBean("accountDao",IAccountDao.class);
System.out.println(accountService);
System.out.println(accountDao);
}
}
ApplicationContext的三个常用实现类:
ClassPathXmlApplicationContext:他可以加载类路径下的配置文件,要求配置文件必须在类路径下,不在的话,加载不了。
AnnotationConfigApplicationContext:它是用于读取注解创建容器的。
获取配置文件对应实现类有两种方法:
public static void main(String[] args) {
ApplicationContext ac=new ClassPathXmlApplicationContext("bean.xml");
IAccountService accountService=(IAccountService)ac.getBean("accountService");
IAccountDao accountDao=ac.getBean("accountDao",IAccountDao.class);
System.out.println(accountService);
System.out.println(accountDao);
/*-------BeanFactory-------------*/
Resource resource=new ClassPathResource("bean.xml");
BeanFactory beanFactory=new XmlBeanFactory(resource);
IAccountService as=(IAccountService)beanFactory.getBean("accountService");
System.out.println(as);
}
}
核心容器接口两个引发出的问题:
ApplicationContext: (单例对象适用)
他在创建核心容器时,创建对象采取的策略是采用立即加载的方式。也就是说,只要一读完配置文件马上就创建配置文件的对象。
BeanFactory: (多例对象适用)
他在创建核心容器时,创建对象采取的策略是采用延迟加载的方式。也就是说,什么时候根据 id 获取对象了,什么时候才真正的创建对象。
<bean id="accountService" class="com.service.impl.AccountServiceImpl"/>
<bean id="accountDao" class="com.dao.impl.AccountDaoImpl"/>
package com.factory;
import com.service.IAccountService;
import com.service.impl.AccountServiceImpl;
/*
模拟一个工厂类(该类可能存在于jar包中,我们无法通过修改源码的方式来提供默认构造函数)
*/
public class InstanceFactory {
public IAccountService getAccountService(){
return new AccountServiceImpl();
}
}
<bean id="instanceFactory" class="com.factory.InstanceFactory"/>
<bean id="accountService2" factory-bean="instanceFactory" factory-method="getAccountService"/>
package com.factory;
import com.service.IAccountService;
import com.service.impl.AccountServiceImpl;
/*
模拟一个工厂类(该类可能存在于jar包中,我们无法通过修改源码的方式来提供默认构造函数)
*/
public class StaticFactory {
public static IAccountService getAccountService(){
return new AccountServiceImpl();
}
}
<bean id="accountService3" class="com.factory.StaticFactory" factory-method="getAccountService"/>
bean 的作用范围调整
作用:用于指定bean作用范围 取值:常用的就是单例的于多例的
singleton:单例的(默认值)
prototype:多例的
request:作用与web应用的请求范围
session:作用于web应用的会话范围
global-session:作用于集群环境的会话范围(全局会话范围),当不是集群环境时,它就是session
对于 global-session 的理解
global session 可以理解为全局 session
出生:当容器创建时对象出生
活着:只要容器还在,对象一直活着
死亡:单例对象的生命周期和容器相同多例对象
public class AccountServiceImpl implements IAccountService {
private IAccountDao accountDao=new AccountDaoImpl();
public void saveAccount() {
accountDao.saveAccount();
}
public void init(){
System.out.println("对象初始化");
}
public void destroy(){
System.out.println("对象销毁");
}
}
<bean id="accountService" class="com.service.impl.AccountServiceImpl" scope="singleton"
init-method="init" destroy-method="destroy"/>
<bean id="accountDao" class="com.dao.impl.AccountDaoImpl"/>
public class Client {
public static void main(String[] args) {
ClassPathXmlApplicationContext ac=new ClassPathXmlApplicationContext("bean.xml");
IAccountService as=ac.getBean("accountService",IAccountService.class);
as.saveAccount();
ac.close();
}
}
对象初始化
保存成功
对象销毁
出生:当我们使用时 spring 框架为我们创建
活着:对象只要是在使用过程中就一直活着
死亡:当对象长时间不用,且没有别的对象引用时,由Java的垃圾回收器回收
原文:https://www.cnblogs.com/zuiren/p/11415413.html