1. 导入Jar包
3.0.2是spring与市面上其他工具类的整合.
根据所需要实现的功能,导入相对应的Jar包.基础功能就导入beans,core,context,expression.
除此之外还需要导入日志包,否则运行会报错
2. 导入约束,书写配置文件.(Spring的配置文件的约束文件是Schema)
- 文件的位置:建议放到src下,配置文件的名字建议applicationContext.xml
- 导入约束:
- 选择约束文件的路径
- 在创建了xml文件的情况下,添加约束.
打开视图
- 引入约束
- 书写配置文件,将对象注册到配置文件中
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.springframework.org/schema/beans" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd "><bean name="User" class="com.itheima.pojo.User"></bean>
</beans>
复制代码
- 测试配置成功案例
public class Demo {@Testpublic void fun (){ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");User u = (User) ac.getBean("User");System.out.println(u);}
}
复制代码
配置详解
- Bean 元素:使用该元素描述需要被spring管理的对象
属性 | 详解 |
---|---|
class | 被管理对象的完整类名 |
name | 给被管理的对象起一个名字.获取对象的时候根据这个名称获取对象.(==可以重复,可以使用特殊字符,但不推荐==) |
id | 与name属性的作用一样.(==不能重复,不能使用特殊字符==) |
==结论:尽量使用name 属性==
- scope 属性:
值 | 详解 |
---|---|
singleton | 表示该对象为单例对象,在spring容器中只会存在一个实例 |
prototype | 表示该对象为多例对象.每次在spring容器中获取该对象时都会创建一个新的对象. |
request | 表示该对象的生命周期与request一样,一次请求结束后就会从spring容器中移除 |
session | 表示该对象的生命周期与session一样,会话结束后就会从spring容器中移除 |
注意:scope的默认值为singleton,在整合struts2时,action对象的scope的值就需要设置为prototype.因为action对象必须是多例的
- init&destory(初始化与销毁)
在容器启动的时候会执行对象的初始化方法,因为容器启动的时候会创建配置文件中的所有对象.当容器关闭的时候,会执行对象的销毁方法.
注意:ApplicationContext接口是没有关闭方法的.子接口ClassPathApplicationContext才有.
//实体类
public void init (){System.out.println("user对象被初始化了");}public void destory (){System.out.println("user对象被销毁了");}//配置文件中
<bean name="user" class="com.demo.pojo.User" init-method="init" destroy-method="destory"></bean>//执行
@Testpublic void fun2() {// 创建容器对象ClassPathXmlApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");// 从容器中获取User对象User u = (User) ac.getBean("user");System.out.println(u);ac.close();}
复制代码
- 模块化
随着项目对象会越来越多,因此配置需要分模块,一个配置文件中可以引入其他配置文件
<import resource=""/>
复制代码
Spring(创建对象的三种方式)
1. 空参构造方式
因为我们使用的接口是applicationContext,因此配置文件中的对象会在容器启动的时候全部创建.
@Testpublic void fun1 (){ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");}<bean name="user" class="com.itheima.pojo.User"></bean>
复制代码
2. 静态工厂
我们希望由我们开发人员自己来创建对象,然后把对象交给spring容器来管理.当我们需要使用的时候也是从容器中获取
public static User createUser (){System.out.println("User对象被创建了");return new User ();}<bean name="user2" class="com.itheima.test.UserFactory" factory-method="createUser"></bean>
复制代码
3. 实例工厂
由于方法不是静态,不能通过类名进行调用,因为我们必须创建对象才能进行调用方法,把所需要的对象创建出来.
@Test
public void fun1 (){
ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
User u = (User) ac.getBean("user3");}public User createUser2 (){System.out.println("User对象被动态创建了");return new User ();
}//配置文件
<bean name="userFactory" class="com.demo.test.UserFactory"></bean><bean name="user3" factory-method="createUser2"factory-bean="userFactory"></bean>
复制代码
Spring属性注入
- set方式注入
- 属性注入
<bean name="user" class="com.demo.pojo.User"><property name="name" value="tom"></property><property name="age" value="18"></property></bean>
复制代码
- 对象注入
<bean name="user" class="com.demo.pojo.User"><property name="name" value="tom"></property><property name="age" value="18"></property><property name="Car" ref="car"></property></bean><bean name="car" class="com.demo.pojo.Car"><property name="name" value="AE86"></property><property name="color" value="白色"></property></bean>复制代码
注意:值类型使用value属性,引用类型使用ref属性.
- 构造函数注入
constructor-arg元素
属性 | 详解 |
---|---|
name | 对象的属性名 |
value | 值 |
ref | 引用什么对象 |
index | 构造函数的参数列表的顺序 |
type | 构造函数的参数类型. |
<bean name="user" class="com.demo.pojo.User"><constructor-arg name="name" value="666" index="1" type="java.lang.Integer"></constructor-arg><constructor-arg name="car" ref="car" index="0"></constructor-arg></bean>//构造方法public User(String name, Car car) {this.name = name;this.car = car;}public User(Car car, Integer name) {this.name = name+"";this.car = car;}
复制代码
- p名称空间注入
前置工作:需要导入p命名空间
在根元素中添加以下的这段代码
xmlns:p="http://www.springframework.org/schema/p"
复制代码
其实就是set注入方法
<bean name="user2" class="com.demo.pojo.User" p:name="tom"p:age="18" p:car-ref="car"></bean>
复制代码
- spel 注入
<!-- spring expression language --><bean name="user3" class="com.demo.pojo.User"><property name="name" value="#{user.name}"></property><property name="age" value="#{user1.age}"></property><property name="car" ref="car"></property></bean>
复制代码
注意:引用类型不能使用spel,跟之前的引用类型用法一样
- 复杂类型注入
数组,集合,Map集合,Properties
注意:数据与集合当值只有一个的时候,值可以写在property元素中的value属性中
<property name="arr" value="tom"></property><property name="list" value="jerry"></property>
复制代码
<bean name="cb" class="com.demo.pojo.CollectionBean"><property name="arr"><array><value>tom</value><value>jerry</value><ref bean="car"/></array></property><property name="list"><list><value>#{user.name}</value><value>#{user.age}</value><ref bean="car"/></list></property><property name="map"><map><entry key="name" value="mama"></entry><entry key="uu" value-ref="user2"></entry><entry key-ref="user2" value-ref="user3"></entry></map></property><property name="prop"><props><prop key="name">tom</prop><prop key="age">18</prop></props></property></bean>
复制代码