本章前提:classpath所指向的路径。
1.src路径下的文件编译后会放到WEB-INF/classes路径下。即默认的classpath。
2.单元测试:在启动或者运行的选项里指定classpath路径。
3.maven:resources目录就是默认的classpath。
一、Bean的配置项
——Id 唯一标识
——Class 具体实例化的类,必须的
——Scope 作用域
——Constructor arguments 构造器
——Properties 属性
——Autowiring mode 自动装配模式
——lazy-initialization mode 懒加载模式
——Initialization/destruction method 初始化/销毁方法
二、Bean的作用域
1.singleton:单例,指一个Bean容器中只存在一份。IOC容器默认方式
public class BeanScope {
public void say() {
System.out.println("BeanScope say : " + this.hashCode());
}
}
<?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
http://www.springframework.org/schema/beans/spring-beans.xsd" >
<bean id="beanScope" class="com.csdn.bean.BeanScope" scope="singleton"></bean>
</beans>
@RunWith(BlockJUnit4ClassRunner.class)
public class TestBeanScope extends UnitTestBase {
public TestBeanScope() {
super("classpath*:spring-beanscope.xml");
}
@Test
public void testSay() {
BeanScope beanScope = super.getBean("beanScope");
beanScope.say();
BeanScope beanScope2 = super.getBean("beanScope");
beanScope2.say();
}
@Test
public void testSay2() {
BeanScope beanScope = super.getBean("beanScope");
beanScope.say();
}
}
2.prototype:每次请求(每次使用)创建新的实例,destroy方式不生效。
3.request:每次http请求创建一个实例且仅在当前request内有效。
4.session:同上,每次http请求创建,当前session内有效。
5.global session:基于portlet的web容器中有效(portlet定义了global session),如果是在web中,同session。
三、Bean的生命周期
——定义
——初始化
——实现org.springframework.beans.factory.InitializationBean接口,覆盖afterPropertiesSet方法
——配置init-method
——使用
——销毁
——实现org.springframework.beans.factory.DisposableBean接口,覆盖destroy方法
——配置destroy-method
全局配置初始化、销毁
四、Aware接口
1.Spring中提供了一些以Aware结尾的接口,实现了Aware接口的bean在被初始化之后,可以获取相应资源。
2.通过Aware接口,可以对Spring相应资源进行操作(慎重)
3.为对Spring进行简单的扩展提供了方便的入口
public class CsdnApplicationContext implements ApplicationContextAware {
@Override
public void setApplicationContext(ApplicationContext applicationContext)
throws BeansException {
System.out.println("CsdnApplicationContext : " + applicationContext.getBean("csdnApplicationContext").hashCode());
}
}
public class CsdnBeanName implements BeanNameAware, ApplicationContextAware {
private String beanName;
@Override
public void setBeanName(String name) {
this.beanName = name;
System.out.println("CsdnBeanName : " + name);
}
@Override
public void setApplicationContext(ApplicationContext applicationContext)
throws BeansException {
System.out.println("setApplicationContext : " + applicationContext.getBean(this.beanName).hashCode());
}
}
<?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
http://www.springframework.org/schema/beans/spring-beans.xsd" >
<!-- <bean id="csdnApplicationContext" class="com.csdn.aware.CsdnApplicationContext" ></bean> -->
<bean id="csdnBeanName" class="com.csdn.aware.CsdnBeanName" ></bean>
</beans>
@RunWith(BlockJUnit4ClassRunner.class)
public class TestAware extends UnitTestBase {
public TestAware() {
super("classpath:spring-aware.xml");
}
// @Test
// public void testCsdnApplicationContext() {
// System.out.println("testCsdnApplicationContext : " + super.getBean("csdnApplicationContext").hashCode());
// }
@Test
public void textCsdnBeanName() {
System.out.println("textCsdnBeanName : " + super.getBean("csdnBeanName").hashCode());
}
}
五、Bean的自动装配
1.No:不做任何操作。
2.byname:根据属性名自动装配,此选项将检查容器并根据名字查找与属性完全一致的bean,并将其与属性自动装配。
3.byType:如果容器中存在一个与指定属性类型相同的bean,那么将与该属性自动装配;如果存在多个该类型bean,那么抛出异常,并指出不能使用byType方式进行自动装配;如果没有找到相匹配的bean,则什么事都不发生。
4.Constructor:与byType方式类似,不同之处在于它应用于构造器参数。如果容器中没有找到与构造器参数类型一致的bean,那么抛出异常。
六、Resources&ResourceLoader
1.针对于资源文件的统一接口
2.Resoureces
——UrlResource:URL对应的资源,根据一个URL地址即可构建。
——ClassPathResource:获取哦类路径下的资源文件。
——FileSystemResource:获取文件系统里面的资源。
——ServletContextResource:ServletContext封装的资源,用于访问ServletContext环境下的资源。
——InputStreamResource:针对输入流封装的资源。
——ByteArrayResource:针对于字节数组封装的资源。
3.ResourceLoader
public class CsdnResource implements ApplicationContextAware {
private ApplicationContext applicationContext;
@Override
public void setApplicationContext(ApplicationContext applicationContext)
throws BeansException {
this.applicationContext = applicationContext;
}
public void resource() throws IOException {
Resource resource = applicationContext.getResource("config.txt");//依赖于applicationContext的位置
//Resource resource = applicationContext.getResource("classpath:config.txt");
//Resource resource = applicationContext.getResource("file:D:\\study\\config.txt");
//Resource resource = applicationContext.getResource("url:https://docs.spring.io/spring/docs/5.0.3.RELEASE/spring-framework-reference/");
System.out.println(resource.getFilename());
System.out.println(resource.contentLength());
}
}
<?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
http://www.springframework.org/schema/beans/spring-beans.xsd" >
<bean id="csdnResource" class="com.csdn.resource.CsdnResource" ></bean>
</beans>
@RunWith(BlockJUnit4ClassRunner.class)
public class TestResource extends UnitTestBase {
public TestResource() {
super("classpath:spring-resource.xml");
}
@Test
public void testResource() {
CsdnResource resource = super.getBean("csdnResource");
try {
resource.resource();
} catch (IOException e) {
e.printStackTrace();
}
}
}