文章目录
spring 环境搭建
1. 导包
- spring4个核心包 + logging 日志包
- 在 WEN-INF\lib 文件夹下
2. 新建并配置配置文件(applicationContext.xml)
- 在 src 下新建 applicationContext.xml
- 文件名称和路径自定义.
- Spring 的容器是 ApplicationContext, applicationContext.xml 配置的信息最终存储到了 AppliationContext 容器中
- spring 配置文件是基于 schema
- schema 文件扩展名.xsd
- 把 schema 理解成 DTD 的升级版.
- 比 DTD 具备更好的扩展性.
- 每次引入一个 xsd 文件是一个 namespace(xmlns)
- 配置文件中只需要引入基本 schema
- 通过<bean /> 创建对象.
- 默认配置文件被加载时创建对象.
<!-- applicationContext.xml 最基本配置 -->
<!-- xmlns 还有aop, beans, tx, 等其他约束,用到的时候再导入即可
(参见:Spring Framework Reference Documentation)-->
<?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="唯一表示符" class="包名 + 类名"></bean>
</beans>
3. 编写测试方法
@Test
public void demo() {
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
类名 实例名 = context.getBean("唯一标识符id", 类名.class);
}
spring IOC_创建对象的三种方法
IOC介绍
- 中文名称:控制反转
- 英文名称:( Inversion of Control )
- IoC 是什么?
- IoC 完成的事情原先由程序员主动通过 new 实例化对象事情,转交给 Spring 负责
- 控制反转中控制指的是:控制类的对象.
- 控制反转中反转指的是转交给 Spring 负责.
- IoC 最大的作用: 解耦.
- 程序员不需要管理对象,解除了对象管理和程序员之间
的耦合.
- 程序员不需要管理对象,解除了对象管理和程序员之间
创建对象的三种方法
1. 通过构造方法创建
- 无参构造创建 : 默认情况.
- 有参构造创建 : 需要明确配置
- 需要在类中提供有参构造方法
- 在 applicationContext.xml 中设置调用哪个构造方法创建
对象 - 如果设定的条件匹配多个构造方法执行最后的构造方法
2. 实例工厂
- 工厂设计模式:帮助创建类对象,一个工厂可以生产多个对象.
- 实例工厂: 需要先创建工厂, 才能生产对象
- 实现步骤:
- 必须要有一个实例工厂
- 在 applicationContext.xml 中配置工厂对象和需要创建的
对象
3. 静态工厂
- 不需要创建工厂,快速创建对象.
- 实现步骤
- 编写一个静态工厂(在方法上添加 static)
- 在 applicationContext.xml 中配置工厂对象
代码演示
// 实例对象类 Person
package com.fu.pojo;
public class Person {
private int id;
private String name;
public Person() {
super();
System.out.println("执行了无参构造");
}
public Person(String name, int id) {
super();
this.id = id;
this.name = name;
System.out.println("执行了有参构造: name + id");
}
public Person(Integer id, String name) {
super();
this.id = id;
this.name = name;
System.out.println("执行了有参构造: Integerid + name");
}
public Person(int id, String name) {
super();
this.id = id;
this.name = name;
System.out.println("执行了有参构造: id + name");
}
// get/set toString 方法
}
// 工厂对象 PersonFactory
package com.fu.pojo;
public class PersonFactory {
/* 实例工厂 */
// public Person newInstance() {
// return new Person(123, "Jreey");
// }
/* 静态工厂 */
public static Person newInstance() {
return new Person(22, "Jim");
}
}
- 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">
<!-- 构造方法创建对象 -->
<!-- id: 唯一标识
class: 想要被创建的对象的类名(包名 + 类名)
-->
<bean id="person" class="com.fu.pojo.Person">
<!--
index : 构造方法里的参数下标,从0开始
name : 构造方法里的参数名
type : 参数的类型,严格区分基本数据类型和引用数据类型(例: int 和 Integer不一致)
以上3种属性,可同时存在,也可只写一种
value : 直接指定参数的值
ref : 指定参数的值的引用, 引用另一个bean(id)
以上2种属性,只可存在一种
根据配置的属性信息,如果有多个构造方法同时满足条件,
会按照书写顺序来调用构造方法,调用写在后边的构造方法
-->
<constructor-arg name="id" type="Integer" value="11"></constructor-arg>
<constructor-arg name="name" value="Tom"></constructor-arg>
</bean>
<!-- 构造方法创建对象 end -->
<!-- 实例工厂创建对象 -->
<bean id="personFactory" class="com.fu.pojo.PersonFactory"></bean>
<!--
factory-bean : 实例工厂的bean的id
factory-method : 创建对象的方法名
-->
<bean id="exampleFactory" factory-bean="personFactory" factory-method="newInstance"></bean>
<!-- 实例工厂创建对象 end -->
<!-- 测试静态工厂时,需要把实例工厂的配置,和PersonFactory的非静态newInstance方法 注释掉,反之亦然 -->
<!-- 静态工厂创建对象 -->
<bean id="staticFactory" class="com.fu.pojo.PersonFactory" factory-method="newInstance"></bean>
<!-- 静态工厂创建对象 end -->
</beans>
测试类
package com.fu.test;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.fu.pojo.Person;
import com.fu.pojo.PersonFactory;
/**
*
* @Description: 测试spring IOC,了解spring如何控制反转,创建对象
* @version V1.0
*/
public class TestIoc {
/**
* spring创建Bean对象的3种方式:
* 一、构造方法
*/
@Test
public void demo01() {
/*
* 只执行这一条代码,就可以看到控制台打印 :执行了无参构造
* 结论:
* 1. Spring 是在容器启动的时候创建对象的
* 2. 默认使用类的无参构造方法
*/
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
Person bean = context.getBean("person", Person.class);
// applicationContext.xml 文件里没有配置初始化值:constructor-arg
// Person [id=11, name=Tom]
System.out.println(bean);
}
/**
* spring创建Bean对象的3种方式:
* 二、实例工厂
*/
@Test
public void demo02() {
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
Person person = context.getBean("exampleFactory", Person.class);
// Person [id=123, name=Jreey]
System.out.println(person);
}
/**
* spring创建Bean对象的3种方式:
* 三、静态工厂
*/
@Test
public void demo03() {
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
Person person = context.getBean("staticFactory", Person.class);
System.out.println(person);
}
}