simpleThreadScope
每个线程都会创建一个单独的实例
<!--单线程中只有一个实例--> <bean id="simpleThreadScope" class="org.springframework.context.support.SimpleThreadScope"/> <bean class="org.springframework.beans.factory.config.CustomScopeConfigurer"> <property name="scopes"> <map> <entry key="simpleThreadScope" value-ref="simpleThreadScope"/> </map> </property> </bean> <bean class="com.demo.ioc.Bean" id="bean" scope="simpleThreadScope"/> package com.demo.ioc; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class BeanTest { @Test public void testBean(){ // 获取上下文 ApplicationContext context = new ClassPathXmlApplicationContext("spring.xml"); for (int i = 0; i < 10; i++) { Bean bean = context.getBean("bean", Bean.class); System.out.println(bean); } for (int i = 0; i < 10; i++) { new Thread(new Runnable() { @Override public void run() { Bean bean = context.getBean("bean", Bean.class); System.out.println(bean); } }).start(); } } }
Bean的懒加载
Spring容器会在创建容器时,提前初始化Singleton(单例)作用域的bean,
但是如果Bean被标注了lazy-init=“true”,
则该Bean只有在其在被需要的时候才会被初始化。
注意:只对Singleton(单例)作用域的bean有效,其他作用域如多例并不知道什么这个bean才会被需要实例化。
为某个Bean设定为懒加载
<bean class="com.demo.ioc.Bean" id="bean" lazy-init="true"/>
为所有Bean设定为懒加载
<beans default-lazy-init="true"> <bean class="com.demo.ioc.Bean" id="bean"/> </beans>
适用场景
如果某个Bean在程序整个运行周期都可能不会被使用,那么可考虑设定该Bean为懒加载。
优点:尽可能的节省了资源。
缺点:可能会导致某个操作响应时间增加。
Bean的初始化及销毁逻辑处理
1、如果需要在Bean实例化之后执行一些逻辑,有两种方法:
- 使用init-method属性
- 让Bean实现InitializingBean接口
2、如果需要在Bean销毁之前执行一些逻辑,有两种方法:
- 使用destory-method属性
- 让Bean实现DisposableBean接口
3、通过指定方法名实现
package com.demo.ioc; public class Bean { public void onInit(){ System.out.println("onInit"); } public void onDestroy(){ System.out.println("onDestroy"); } }
单个Bean初始化和销毁方法执行
<bean class="com.demo.ioc.Bean" id="bean" init-method="onInit" destroy-method="onDestroy"/> • 1
所有Bean初始化和销毁方法执行
<beans default-init-method="onInit" default-destroy-method="onDestroy" > <bean class="com.demo.ioc.Bean" id="bean" /> </beans> package com.demo.ioc; import org.junit.Test; import org.springframework.context.support.AbstractApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class BeanTest { @Test public void testBean(){ // 获取上下文 AbstractApplicationContext context = new ClassPathXmlApplicationContext("spring.xml"); // 关闭上下文 context.close(); } }
4、通过实现接口
package com.demo.ioc; import org.springframework.beans.factory.DisposableBean; import org.springframework.beans.factory.InitializingBean; public class Bean implements InitializingBean, DisposableBean { @Override public void destroy() throws Exception { System.out.println("destroy"); } @Override public void afterPropertiesSet() throws Exception { System.out.println("afterPropertiesSet"); } }
Bean属性继承
package com.demo.ioc; public class Parent { private String name; public String getName() { return name; } public void setName(String name) { this.name = name; } } package com.demo.ioc; public class Child1 extends Parent{ private String name1; public String getName1() { return name1; } public void setName1(String name1) { this.name1 = name1; } @Override public String toString() { return "Child1{" + "name='" + getName() + '\'' + "name1='" + name1 + '\'' + '}'; } } package com.demo.ioc; public class Child2 extends Parent{ private String name2; public String getName2() { return name2; } public void setName2(String name2) { this.name2 = name2; } @Override public String toString() { return "Child1{" + "name='" + getName() + '\'' + "name2='" + name2 + '\'' + '}'; } }
继承关系配置
<!-- 这个javaBean不需要实例化 --> <bean id="parent" class="com.demo.ioc.Parent" abstract="true"> <property name="name" value="name"/> </bean> <bean id="child1" class="com.demo.ioc.Child1" parent="parent"> <property name="name1" value="name1"/> </bean> <bean id="child2" class="com.demo.ioc.Child2" parent="parent"> <property name="name2" value="name1"/> </bean>
或者非继承关系,有相同的属性
<bean id="parent" abstract="true"> <property name="name" value="name"/> </bean> <bean id="child1" class="com.demo.ioc.Child1" parent="parent"> <property name="name1" value="name1"/> </bean> <bean id="child2" class="com.demo.ioc.Child2" parent="parent"> <property name="name2" value="name1"/> </bean>
SpringIoC注解的基本使用
pom.xml依赖
<dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>5.1.3.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>5.1.3.RELEASE</version> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.13</version> <scope>test</scope> </dependency>
1、使用xml文件
package com.demo.ioc; public class Bean{ }
创建spring.xml配置文件
<?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交由Spring创建并管理 --> <bean id="bean" class="com.demo.ioc.Bean"/> </beans>
获取Bean
package com.demo.ioc; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class BeanTest { @Test public void testBean() { // 获取上下文 ApplicationContext context = new ClassPathXmlApplicationContext("spring.xml"); // 获取Bean Bean bean = context.getBean("bean", Bean.class); System.out.println(bean); } }
2、使用annotation
package com.demo.ioc; public class Person { } package com.demo.ioc.human; import com.demo.ioc.Person; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; // 创建配置文件 @Configuration public class MyConfiguration { // 将Bean交由Spring创建并管理 // name参数可以省略,默认方法名就是bean的名称 @Bean(name = "person") public Person person(){ return new Person(); } }
获取Bean
package com.demo.ioc; import com.demo.ioc.human.MyConfiguration; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.annotation.AnnotationConfigApplicationContext; public class BeanTest { @Test public void testBean() { // 获取上下文 ApplicationContext context = new AnnotationConfigApplicationContext(MyConfiguration.class); // 获取Bean Person bean = context.getBean("person", Person.class); System.out.println(bean); } }