Spring的IOC入门-环境搭建
(1)创建Project maven
(2)创建模块module maven
(3)配置依赖
<!--spring依赖 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.2.9.RELEASE</version>
</dependency>
SpringIOC容器
SpringIOC容器,是spring核心内容。
作用: 创建对象 & 处理对象的依赖关系
IOC容器创建对象:
创建对象, 有几种方式:
调用无参数构造器
带参数构造器
工厂创建对象(静态工厂方式与非静态工厂方式创建对象)
配置对象的基本结构
<?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
https://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="..." class="...">
<!-- collaborators and configuration for this bean go here -->
</bean>
<bean id="..." class="...">
<!-- collaborators and configuration for this bean go here -->
</bean>
<!-- more bean definitions go here -->
</beans>
怎么取出在IoC容器中的对象
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
Xxx bean = (Xxx)context.getBean("bean的id");/方式一
Xxx bean = context.getBean("bean的id",Xxx.class对象)/方式二
示例使用:
public class Person {
private String name;
private int age;
private String sex;
public Person() {
}
public Person(String name,int age,String sex){
this.name=name;
this.age=age;
this.sex=sex;
}
get set省略
调用无参数构造器
<bean class="com.glc.bean.Person" id="person" />
带参数构造器
如果想要以其他参数个数创建对象,必须存在响应的构造方法,要不然报错
<bean class="com.glc.bean.Person2" id="person">
<constructor-arg name="name" value="zhangsan"/>
<constructor-arg name="age" value="13"/>
<constructor-arg name="sex" value="男"/>
</bean>
静态工厂方式
public class PersonFactory {
public static Person getBean() {
return new Person();//静态方法返回创建的对象
}
}
非静态工厂方式
public class PersonFactory {
public Person getBean() {
return new Person();//静态方法返回创建的对象
}
}
单例和多例
单例是什么?
内存中只有一个对象,每次获取到该对象的地址值一样.
多实例是什么?
内存中的每个对象都是一个新的对象,他们的地址值都不同.
(1)问题: 每次获取对象的时候,spring是新创建一个对象还是始终给我们返回同一个对象.
(2)答案: spring默认的情况下创建的对象都是单例的. (每次返回的对象都是同一个)
scope="singleton" 单例(默认值)
scope="prototype" 多例
scope="request" 创建的对象放到request域中
scope="session" 创建对象放到session对象
多实例
<bean id="person" class="com.wzx.domain.Person" scope="prototype"/>
单实例
<bean id="person" class="com.wzx.domain.Person" scope="singleton"/>
Spring生命周期
(1)生命周期
- 创建方法init
- 销毁方法destory
- 普通方法service
(2)属性
- init-method 当该对象初始化的时候该方法会自动执行
- destroy-method 当该对象即将销毁的时候会自动调用该方法
public class Person{
public void init(){
System.out.println("init...");
}
public void eat(){
System.out.println("eat...");
}
public void destory(){
System.out.println("destory...");
}
}
<bean id="person6" class="com.wzx.domain.Person"
init-method="init"
destroy-method="destory"
/>
Spring依赖注入DI-set方法
依赖注入DI (dependency injection)就是给对象的属性设置值
set方法给对象设置值
(基本类型赋值)
property标签
让spring调set方法,前提条件类中必须有set方法name : 代表的是set方法去掉set,首字母小写setName Name name value:
value 可以设置基本类型或字符串类型的值,
ref (引用) : 引用对象的id,作为一个对象类型注入
(复杂类型赋值)
//数组类型
<bean class="com.glc.bean.Person" id="person5">
<property name="strings">
<array>
<value>一</value>
<value>二</value>
<value>三</value>
</array>
</property>
</bean>
//list类型
<bean class="com.glc.bean.Person" id="person6">
<property name="order">
<list>
<value>一</value>
<value>二</value>
<value>三</value>
</list>
</property>
</bean>
//set类型
<bean class="com.glc.bean.Person" id="person4">
<property name="set">
<set>
<value>一</value>
<value>二</value>
<value>三</value>
</set>
</property>
</bean>
//map类型
<bean class="com.glc.bean.Person" id="person4">
<property name="map">
<map>
<entry key="1" value="一"/>
<entry key="2" value="二"/>
<entry key="3" value="三"/>
</map>
</property>
</bean>
//properties类型
<bean class="com.glc.bean.Person" id="person4">
<property name="properties">
<props>
<prop key="1">一</prop>
<prop key="2">二</prop>
<prop key="3">三</prop>
</props>
</property>
</bean>
//值设置为空
<bean class="com.glc.bean.Person" id="person4">
<property name="Null">
<null></null>
</property>
</bean>