目录
(1)在com.cqgcxy.entity包下创建Phone接口。
(2)在com.cqgcxy.entity包下创建HuaWeiPhone实现类。
4.创建Spring核心配置文件:在resources文件夹下创建spring的核心配置文件applicationContext.xml
5.在配置文件中配置实现类:在配置文件applicationContext.xml的根标签中添加以下配置:
(1)选中HuaWeiPhone类,按ctrl+shift+T为该类创建对应的JUnit4的测试类和测试方法。
7.配置scope属性,修改对象的作用范围为singleton。【可以打断点观察对象创建时机】
(1)在HuaWeiPhone类中添加无参构造方法,代码如下:
(2)在applicationContext.xml的bean标签中添加scope属性,并设置属性值为singleton。
编辑(3)在测试类HuaWeiPhoneTest编写测试方法scopeTest,代码如下:
8.配置init-method属性,指定类中的初始化方法名称。配置destroy-method属性,指定类中销毁方法名称。
(1)在HuaWeiPhone类中添加初始化方法init,代码如下:
(2)在HuaWeiPhone类中添加销毁方法destory,代码如下:
(3)在applicationContext.xml的bean标签中添加init-method属性,并设置属性值为init,添加destroy-method属性,并设置属性值为destory。
(4)在测试类HuaWeiPhoneTest编写测试方法lifeTest,代码如下:
9.配置factory-method属性,通过工厂静态方法实例化对象。
(1)在com.cqgcxy.entity包下创建手机工厂类PhoneFactory,创建静态方法获取手机对象getInstance,代码如下:
(2)修改applicationContext.xml的bean标签中class属性为手机工厂类,添加factory-method属性,并设置属性值为手机工厂类中的静态方法getInstance。
(3)调用测试类HuaWeiPhoneTest的测试方法call。
10.分别配置factory-bean属性和factory-method属性,通过工厂动态方法实例化对象。
(1)手机工厂类PhoneFactory,创建通过动态方法获取手机对象getPhone,代码如下:
(2)修改applicationContext.xml中配置PhoneFactory的bean标签,再创建Phone的bean标签,指定工厂方法实例和动态工厂方法。
一.Spring的概念
Spring是分层的Java SE/EE应用轻量级开源框架。以IOC ( Inverse Of Control :反转控制)和AOP( Aspect Oriented Programming :面向切面编程)为内核。
1.IOC控制反转
所谓的“控制反转”就是对组件对象控制权的转移,从程序代码本身转移到了外部容器,由容器来创建对象并管理对象之间的依赖关系。
2.bean标签基本配置
默认情况下它调用的是类中的无参构造函数,如果没有无参构造函数则不能创建成功。
- 基本属性:
- id:Bean实例在Spring容器中的唯一标识
- class:Bean实例的全限定名称
<bean id="" class=""></bean>
bean作用范围配置scope:
singleton:单例的,默认值
- 反射默认找的是无参构造创建对象
- Bean的实例化时机:当Spring核心文件被加载时,实例化配置的Bean实例
- 对象创建:当应用加载,创建容器时,对象就被创建了
- 对象运行:只要容器在,对象一直活着
- 对象销毁:当应用卸载,销毁容器时,对象就被销毁了
<bean id="" class="" scope="singleton"></bean>
prototype:多例的
- Bean的实例化时机:当调用getBean()方法时实例化Bean
- 对象创建:当使用对象时,创建新的对象实例
- 对象运行:只要对象在使用中,就一直活着
- 对象销毁:当对象长时间不用时,被Java的垃圾回收器回收了
<bean id="" class="" scope="prototype"></bean>
request:
WEB项目中,Spring创建一个Bean的对象,将对象存入request域中(了解)
<bean id="" class="" scope="request" ></bean>
session:
WEB项目中,Spring创建一个Bean的对象,将对象存入session域中(了解)
<bean id="" class="" scope="session" ></bean>
global session:
WEB项目中,应用在Portlet环境,如果没有Portlet环境那么就相当于session(了解)
<bean id="" class="" scope="global-session" ></bean>
bean生命周期配置:
init-method 初始化方法:
指定类中的初始化方法名称
destroy-method 销毁方法:
指定类中销毁方法名称
<bean id="" class="" init-method="" destroy-method=""/>
bean实例化方式:
无参构造方法实例化(默认):
1.创建maven项目
2.导入Spring开发基本包坐标:
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.3.8</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
</dependencies>
3.创建接口和实现类:
(1)在com.cqgcxy.entity包下创建Phone接口。
package com.cqgcxy.entity;
public interface Phone {
void call();
}
(2)在com.cqgcxy.entity包下创建HuaWeiPhone实现类。
package com.cqgcxy.entity;
public class HuaWeiPhone implements Phone{
public void call() {
System.out.println("华为手机正在拨打卫星电话···");
}
}
4.创建Spring核心配置文件:在resources文件夹下创建spring的核心配置文件applicationContext.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">
</beans>
5.在配置文件中配置实现类:在配置文件applicationContext.xml的根标签中添加以下配置:
<bean id="huaWeiPhone" class="com.cqgcxy.entity.HuaWeiPhone"></bean>
6.使用Spring API获取实例:
(1)选中HuaWeiPhone类,按ctrl+shift+T为该类创建对应的JUnit4的测试类和测试方法。
(2)测试类中代码如下:
package com.cqgcxy.entity;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class HuaWeiPhoneTest {
@Test
public void call() {
ApplicationContext cxt=new ClassPathXmlApplicationContext("applicationContext.xml");
HuaWeiPhone huaWeiPhone = (HuaWeiPhone)cxt.getBean("huaWeiPhone");
huaWeiPhone.call();
}
}
7.配置scope属性,修改对象的作用范围为singleton。【可以打断点观察对象创建时机】
(1)在HuaWeiPhone类中添加无参构造方法,代码如下:
public HuaWeiPhone(){
System.out.println("麒麟9000s装配成功,华为mate60创建成功···");
}
(2)在applicationContext.xml的bean标签中添加scope属性,并设置属性值为singleton。
(3)在测试类HuaWeiPhoneTest编写测试方法scopeTest,代码如下:
@Test
public void scopeTest() {
ApplicationContext cxt=new ClassPathXmlApplicationContext("applicationContext.xml");
HuaWeiPhone huaWeiPhone1 = (HuaWeiPhone)cxt.getBean("huaWeiPhone");
System.out.println(huaWeiPhone1);
HuaWeiPhone huaWeiPhone2 = (HuaWeiPhone)cxt.getBean("huaWeiPhone");
System.out.println(huaWeiPhone2);
System.out.println(huaWeiPhone1==huaWeiPhone2);
}
8.配置init-method属性,指定类中的初始化方法名称。配置destroy-method属性,指定类中销毁方法名称。
(1)在HuaWeiPhone类中添加初始化方法init,代码如下:
public void init(){
System.out.println("正在加载鸿蒙系统···");
}
(2)在HuaWeiPhone类中添加销毁方法destory,代码如下:
public void destory(){
System.out.println("备份重要信息中···");
}
(3)在applicationContext.xml的bean标签中添加init-method属性,并设置属性值为init,添加destroy-method属性,并设置属性值为destory。
(4)在测试类HuaWeiPhoneTest编写测试方法lifeTest,代码如下:
@Test
public void lifeTest() {
ApplicationContext cxt=new ClassPathXmlApplicationContext("applicationContext.xml");
HuaWeiPhone huaWeiPhone = (HuaWeiPhone)cxt.getBean("huaWeiPhone");
huaWeiPhone.call();
//注意:ClassPathXmlApplicationContext中才能close
((ClassPathXmlApplicationContext)cxt).close();
}
工厂静态方法实例化:
9.配置factory-method属性,通过工厂静态方法实例化对象。
(1)在com.cqgcxy.entity包下创建手机工厂类PhoneFactory,创建静态方法获取手机对象getInstance,代码如下:
package com.cqgcxy.entity;
public class PhoneFactory {
//静态方法创建手机
public static Phone getInstance(){
System.out.println("手机工厂调用静态方法直接创建···");
return new HuaWeiPhone();
}
}
(2)修改applicationContext.xml的bean标签中class属性为手机工厂类,添加factory-method属性,并设置属性值为手机工厂类中的静态方法getInstance。
(3)调用测试类HuaWeiPhoneTest的测试方法call。
工厂动态方法实例化
10.分别配置factory-bean属性和factory-method属性,通过工厂动态方法实例化对象。
(1)手机工厂类PhoneFactory,创建通过动态方法获取手机对象getPhone,代码如下:
//动态方法创建手机
public Phone getPhone(){
System.out.println("手机工厂对象调用动态方法创建···");
return new HuaWeiPhone();
}
(2)修改applicationContext.xml中配置PhoneFactory的bean标签,再创建Phone的bean标签,指定工厂方法实例和动态工厂方法。
(3)调用测试类HuaWeiPhoneTest的测试方法call。
学习心得:
单例singleton :被创建时就已经存在当应用加载,创建容器时,对象就被创建了 (使用或不使用都已经创建)
多例prototype :当使用对象时,创建新的对象实例(使用时才会创建 未使用时为空)