1. Spring框架的核心
Bean对象创建的细节:
对象创建的个数?对象创建的时机?
1. 对象创建:单例和多例(bean中添加 scope属性)
scope=“singleton” 或是默认状态下(即不设定 scope的值),是单例(new出来都是同一个对象);
scope=“prototype” ,是多例(new出来是多个对象);
2. 什么时候创建
①scope=“prototype” 时,先生成 IOC容器,在使用时再创建对象;
②scope=“singleton” 时,先生成单例对象,再启动容器;
③想在启动时不创建对象,在使用时才创建,用单例模式实现(多例无法实现),使用懒加载(懒加载:bean中添加 lazy-init属性);
(多例时,先生成IOC容器,在使用到对象时再创建;单例时,在启动(容器初始化之前)时,创建 bean对象,且整个应用中只有一个对象;)
3. 是否使用延迟(懒)加载(只能是单例模式,多例模式本来就是后生成对象无需延迟加载):
lazy-init ="default"默认为 false时,不延迟加载,依然是在容器生成之前创建单例对象;
lazy-init ="true"时,延迟加载,启动容器时不创建对象,使用时再创建对象;
4. 对象创建之后的初始化和和销毁:
bean中添加 init-method、destory-method 属性
servlet 中(init、service、destory)
一、
1. User.java
public class User{
private int id;
private String name;
set、get();
}
2. applicationContext.xml
(bean 中添加 scope=“singleton” 或者 默认情况 即不写,表示生成单例;)
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">
<bean id="user" class="com.mdd.User" scope="singleton"></bean>
</beans>
3. Test.java
public class Test{
public static void main(String[] args) {
ApplicationContext applicationContext=new ClassPathXmlApplicationContext("applicationContext.xml");
User user=(User)applicationContext.getBean("user");
User user2=(User)applicationContext.getBean("user");
System.out.println(user);
System.out.println(user2);
}
}
运行结果:
(单例类生成的对象只有一个)
二、
1. User.java
2. applicationContext.xml
(bean 中添加 scope=“prototype” 时,表示多例模式;)
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">
<bean id="user" class="com.mdd.User" scope="prototype"></bean>
</beans>
3. Test.java
public class Test{
public static void main(String[] args) {
ApplicationContext applicationContext=new ClassPathXmlApplicationContext("applicationContext.xml");
User user=(User)applicationContext.getBean("user");
User user2=(User)applicationContext.getBean("user");
System.out.println(user);
System.out.println(user2);
}
}
运行结果:
(多例模式生成的对象是多个,不是同一个)
三、
1. bean 中添加 scope=“prototype” 即多例模式时,先生成IOC容器,在使用时再创建对象;
1.1 User.java
(User 类中一个无参构造方法,里面输出内容作为标记; bean中 scope=“prototype”即多例模式时; 运行方法中没有使用到user对象时,运行结果显示只创建 IOC容器,不创建 user对象;)
public class User{
private int id;
private String name;
set、get();
public User(){
System.out.println("------对象被创建------");
}
}
1.2. applicationContext.xml
(bean 中添加 scope=“prototype” 时,表示多例模式;)
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">
<bean id="user" class="com.mdd.User" scope="prototype"></bean>
</beans>
1.3. Test.java
(没使用 user对象时,只生成 IOC容器,不生成 user对象;)
public class Test{
public static void main(String[] args) {
ApplicationContext applicationContext=new ClassPathXmlApplicationContext("applicationContext.xml");
System.out.println("------得到IOC容器------");
}
}
运行结果:
1.3. Test.java
(当使用 user对象时再生成对象:先生成 IOC容器,再生成 user对象;)
public class Test{
public static void main(String[] args) {
ApplicationContext applicationContext=new ClassPathXmlApplicationContext("applicationContext.xml");
System.out.println("------得到IOC容器------");
User user=(User)applicationContext.getBean("user");
User user2=(User)applicationContext.getBean("user");
}
}
运行结果:
2. 单例时:bean 中添加 scope=“singleton” 即单例模式时,先生成单例对象,再启动容器;
2.1 User.java
(User 类中一个无参构造方法,里面输出内容作为标记; bean中 scope=“singleton”即单例模式时; )
public class User{
private int id;
private String name;
set、get();
public User(){
System.out.println("------对象被创建------");
}
}
2.2. applicationContext.xml
(bean 中添加 scope=“singleton” 时,表示单例模式;单例时:先生成对象,再创建 IOC容器;)
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">
<bean id="user" class="com.mdd.User" scope="singleton"></bean>
</beans>
2.3. Test.java
(单例时:先生成对象,再创建 IOC容器;)
public class Test{
public static void main(String[] args) {
ApplicationContext applicationContext=new ClassPathXmlApplicationContext("applicationContext.xml");
System.out.println("------得到IOC容器------");
User user=(User)applicationContext.getBean("user");
User user2=(User)applicationContext.getBean("user");
}
}
运行结果:
(单例时:先生成对象,再创建 IOC容器;)
3. 单例时:bean 中添加 lazy-init=“default” ;
3.2. applicationContext.xml
(bean 中添加 lazy-init=“default” 时,;)
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">
<bean id="user" class="com.mdd.User" scope="singleton" lazy-init="default"/"true"></bean>
</beans>
3.3. Test.java
public class Test{
public static void main(String[] args) {
ApplicationContext applicationContext=new ClassPathXmlApplicationContext("applicationContext.xml");
System.out.println("------得到IOC容器------");
User user=(User)applicationContext.getBean("user");
User user2=(User)applicationContext.getBean("user");
}
}
运行结果:
(单例时使用懒加载为默认 lazy-init=" default " 即 false 时是不延迟加载:依然是先生成对象,再创建 IOC容器;)
运行结果:
(单例时使用懒加载为 true 时即使用延迟加载:则先创建 IOC容器,在生成对象;)
<bean id="user" class="com.mdd.User" scope="singleton" lazy-init="true"></bean>
4. 初始化和销毁时:bean 中添加 init-method、destory-method????? 属性;
4.1. User.java?????
public class User{
private int id;
private String name;
set、get();
public User(){
System.out.println("------对象被创建------");
}
public void init_user(){
System.out.println("------对象初始化-----")
}
public void destory_user(){
System.out.println("------对象销毁-----")
}
}
4.2. applicationContext.xml
(bean 中添加 init-method=“init_user” destory-method=“destory_user”;)
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">
<bean id="user" class="com.mdd.User" scope="singleton" lazy-init="default"/"true" init-method="init_user" destory-method="destory_user"</bean>
</beans>
4.3. Test.java
public class Test{
public static void main(String[] args) {
ApplicationContext applicationContext=new ClassPathXmlApplicationContext("applicationContext.xml");
System.out.println("------得到IOC容器------");
User user=(User)applicationContext.getBean("user");
User user2=(User)applicationContext.getBean("user");
applicationContext.destory();//销毁
}
}
运行结果:
(对象初始化;当在测试方法中调用applicationContext的destory(方法时会被销毁;)