1.Spring创建对象的三种方式
(1)通过构造方法创建
1.1无参构造创建:默认情况.
1.2 有参构造创建:需要明确配置
1.2.1 需要在类中提供有参构造方法
1.2.2 在 applicationContext.xml 中设置调用哪个构造方法创建 对象
1.2.2.1 如果设定的条件匹配多个构造方法执行最后的构造方法
1.2.2.2 index : 参数的索引,从 0 开始
1.2.2.3 name: 参数名
1.2.2.4 type:类型(区分开关键字和封装类 int 和 Integer)
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="peo" class="pojo.People">
<constructor-arg index="0" name="id" type="int" value="123"></constructor-arg>
<constructor-arg index="1" name="name" type="java.lang.String" value="张三"></constructor-arg>
</bean>
</beans>
测试:
实体类:
public class People {
private int id;
private String name;
protected People() {
super();
}
protected People(int id, String name) {
super();
this.id = id;
this.name = name;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "People [id=" + id + ", name=" + name + "]";
}
}
测试类:
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import pojo.People;
public class PeopleTest {
public static void main(String[] args) {
ApplicationContext ac= new ClassPathXmlApplicationContext("applicationContext.xml");
People p=ac.getBean("peo",People.class);
System.out.println(p);
}
}
运行结果:
(2)通过实例工厂创建
2.1 工厂设计模式:帮助创建类对象.一个工厂可以生产多个对象.
2.2 实例工厂:需要先创建工厂,才能生产对象
2.3 实现步骤:
2.3.1 必须要有一个实例工厂(自己写工厂)
public class PeopleFactory {
public People newInstance(){
return new People(1,"王五");
}
}
2.3.2 在 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="peo" class="pojo.People">
<constructor-arg index="0" name="id" type="int" value="123"></constructor-arg>
<constructor-arg index="1" name="name" type="java.lang.String" value="张三"></constructor-arg>
</bean>
-->
<bean id="factory" class="pojo.PeopleFactory"></bean>
<bean id="peo" factory-bean="factory" factory-method="newInstance"></bean>
<!--
<bean id="peo" class="pojo.PeopleFactory" factory-method="newInstance"></bean>
-->
</beans>
测试:
实体类与测试类如上,在此省略
测试结果:
(3)通过静态工厂创建
3.1 不需要创建工厂,快速创建对象.
3.2 实现步骤
3.2.1 编写一个静态工厂(在方法上添加 static)
import pojo.People;
public class PeopleFactory {
public static People newInstance(){
return new People(12,"赵六");
}
}
3.2.2 在 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="peo" class="pojo.People">
<constructor-arg index="0" name="id" type="int" value="123"></constructor-arg>
<constructor-arg index="1" name="name" type="java.lang.String" value="张三"></constructor-arg>
</bean>
-->
<!--
<bean id="factory" class="pojo.PeopleFactory"></bean>
<bean id="peo" factory-bean="factory" factory-method="newInstance"></bean>
-->
<bean id="peo" class="pojo.PeopleFactory" factory-method="newInstance"></bean>
</beans>
测试:
实体类与测试类如上,在此省略
测试结果:
2.给bean的属性赋值(注入)
1.通过构造方法赋值(在介绍Spring创建对象的方式中已经有所体现,这里不再介绍)
2.设置注入(通过实体类的set方法)
2.1 如果要注入的属性是基本数据类型或则String类型
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">
<!-- 注入的属性为基本数据类型或者String -->
<bean id="per" class="pojo.Person">
<property name="id">
<value>3118</value>
</property>
<property name="name">
<value>张华</value>
</property>
<!--上述注入方式等同于下面这种-->
<!--
<property name="id" value="3118"></property>
<property name="name" value="张华"></property>
-->
</bean>
</beans>
实体类Person:
package pojo;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Set;
public class Person {
private int id;
private String name;
private List<?> list;
private Set<?> set;
private Map<?, ?>map;
private String[]arr;
private Student stu;
public Person(int id, String name, List<?> list, Set<?> set, Map<?, ?> map, String[] arr, Student stu) {
super();
this.id = id;
this.name = name;
this.list = list;
this.set = set;
this.map = map;
this.arr = arr;
this.stu = stu;
}
public Student getStu() {
return stu;
}
public void setStu(Student stu) {
this.stu = stu;
}
public Person() {
super();
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public List<?> getList() {
return list;
}
public void setList(List<?> list) {
this.list = list;
}
public Set<?> getSet() {
return set;
}
public void setSet(Set<?> set) {
this.set = set;
}
public Map<?, ?> getMap() {
return map;
}
public void setMap(Map<?, ?> map) {
this.map = map;
}
public String[] getArr() {
return arr;
}
public void setArr(String[] arr) {
this.arr = arr;
}
@Override
public String toString() {
return "Person [id=" + id + ", name=" + name + ", list=" + list + ", set=" + set + ", map=" + map + ", arr="
+ Arrays.toString(arr) + ", stu=" + stu + "]";
}
}
实体类Student:
package pojo;
public class Student {
private String school;
private int age;
public Student() {
super();
}
public Student(String school, int age) {
super();
this.school = school;
this.age = age;
}
public String getSchool() {
return school;
}
public void setSchool(String school) {
this.school = school;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "Student [school=" + school + ", age=" + age + "]";
}
}
测试类:
public class PeopleTest {
public static void main(String[] args) {
ApplicationContext ac= new ClassPathXmlApplicationContext("applicationContext.xml");
People p=ac.getBean("per",Person.class);
System.out.println(p);
}
}
测试结果:Person [id=3118, name=张华, list=null, set=null, map=null, arr=null, stu=null]
2.2 如果属性是 集合Set <?>
<property name="set">
<set>
<value>1</value>
<value>郑州</value>
</set>
</property>
2.3如果属性为List<?>
<property name="list">
<list>
<value>2</value>
<value>焦作</value>
</list>
</property>
2.4如果属性为数组
<property name="arr">
<array>
<value>经济城市</value>
<value>文明城市</value>
</array>
</property>
2.5如果属性为map集合
<property name="map">
<map>
<entry key="第一名" value="熊大"></entry>
<entry key="第二名" value="熊二"></entry>
</map>
</property>
2.6如果属性为properties(外部配置文件)
<property name="demo">
<props>
<prop key="driverClass">com.mysql.cj.jdbc.Driver</prop>
<prop key="userName">root</prop>
</props>
</property>
2.7如果属性为另外一个bean对象
<bean>
<property name="stu" ref="student"></property>
</bean>
<bean id="student" class="pojo.Student">
<property name="school" value="河南理工大学"></property>
<property name="age" value="110"></property>
</bean>
综合上述:
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">
<!-- 注入的属性为基本数据类型或者String -->
<bean id="per" class="pojo.Person">
<property name="id">
<value>3118</value>
</property>
<property name="name">
<value>张华</value>
</property>
<!-- 上面的方式等同于下面的这种
<property name="id" value="3118"></property>
<property name="name" value="张华"></property>
-->
<property name="set">
<set>
<value>1</value>
<value>郑州</value>
</set>
</property>
<property name="list">
<list>
<value>2</value>
<value>焦作</value>
</list>
</property>
<property name="arr">
<array>
<value>经济城市</value>
<value>文明城市</value>
</array>
</property>
<property name="map">
<map>
<entry key="第一名" value="熊大"></entry>
<entry key="第二名" value="熊二"></entry>
</map>
</property>
<!--
<property name="demo">
<props>
<prop key="driverClass">com.mysql.cj.jdbc.Driver</prop>
<prop key="userName">root</prop>
</props>
</property>
-->
<property name="stu" ref="student"></property>
</bean>
<bean id="student" class="pojo.Student">
<property name="school" value="河南理工大学"></property>
<property name="age" value="110"></property>
</bean>
</beans>
实体类,测试类与前面相同
测试结果如下:
Person [id=3118, name=张华, list=[2, 焦作], set=[1, 郑州], map={第一名=熊大, 第二名=熊二}, arr=[经济城市, 文明城市], stu=Student [school=河南理工大学, age=110]]
3 DI
1. DI:中文名称:依赖注入
2. 英文名称((Dependency Injection)
3. DI 是什么?
3.1 DI 和 IoC 是一样的
3.2 当一个类(A)中需要依赖另一个类()对象时,把 B 赋值给 A 的过 程就叫做依赖注入.
4. 代码体现:
<bean>
<property name="stu" ref="student"></property>
</bean>
<bean id="student" class="pojo.Student">
<property name="school" value="河南理工大学"></property>
<property name="age" value="110"></property>
</bean>