什么是Bean管理
(1)Spring创建对象
(2)Spring注入属性
Bean管理操作的两种方式
(1)基于XML配置文件方式实现
(2)基于注解方式实现
Bean管理(基于XML)
① 基于XML方式创建对象
(1)在Spring配置文件中,使用bean
标签,标签里边添加对应属性,就可以实现对象创建。
<!--配置User对象创建-->
<bean id="userDao" class="com.wxm.UserDao"></bean>
(2)bean
标签有很多属性,常用属性如下:
id
属性:唯一标识class
属性:类全路径(包类路径)
(3)创建对象时,默认执行无参的构造方法完成对象的创建
② 基于XML方式注入属性
1)方式一(set方式注入):
- 创建类,定义属性和对应的set方法
public class Book {
// 创建属性
private String bookName;
private String bookAuthor;
// 创建属性对应的方法
public void setBookName(String bookName) {
this.bookName = bookName;
}
public void setBookAuthor(String bookAuthor) {
this.bookAuthor = bookAuthor;
}
public void testDemo(){
System.out.println(bookName + "::" + bookAuthor);
}
}
- 在Spring配置文件中配置对象创建,配置属性注入
<bean id="book" class="com.wxm.Book">
<!-- 使用property完成属性注入
name:类里面属性名称
value:向属性注入的值
-->
<property name="bookName" value="易筋经"></property>
<property name="bookAuthor" value="达摩老祖"></property>
</bean>
- 测试
@Test
public void testBook(){
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("bean1.xml");
Book book = context.getBean("book", Book.class);
System.out.println(book);
book.testDemo();
}
2)方式二(有参数构造器方式注入):
- 创建类,定义属性,创建属性对应的有参数构造器
public class Order {
// 属性
private String orderName;
private String address;
// 有参数构造器
public Order(String orderName, String address){
this.orderName = orderName;
this.address = address;
}
public void testDemo(){
System.out.println(orderName + "::" + address);
}
}
- 在Spring配置文件中进行配置
<!-- 有参数构造注入属性 -->
<bean id="order" class="com.wxm.Order">
<constructor-arg name="orderName" value="abc"></constructor-arg>
<constructor-arg name="address" value="China"></constructor-arg>
</bean>
- 测试
@Test
public void testOrder(){
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("bean1.xml");
Order order = context.getBean("order", Order.class);
System.out.println(order);
order.testDemo();
}
3)方式三(p
名称空间注入):
- 在配置文件中添加
p
名称空间
xmlns:p="http://www.springframework.org/schema/p"
- 在
bean
标签里操作进行属性注入
<bean id="book" class="com.wxm.Book" p:bookName="红楼梦" p:bookAuthor="曹雪芹">
</bean>
Bean管理(XML注入其它类型的属性)
① 字面量
(1)null
值
<!-- null值 -->
<property name="address">
<null/>
</property>
(2)属性值包含其它符号
- 把<>转义
- 把带符号的内容写进
CDATA
<!-- 属性值包含特殊符号 -->
<property name="address">
<value><![CDATA[<<中国>>]]></value>
</property>
② 注入属性——外部bean
(1)创建两个类service类和dao类
public class UserService {
private UserDao userDao;
public void setUserDao(UserDao userDao) {
this.userDao = userDao;
}
public void add() {
System.out.println("add......");
userDao.update();
}
}
public interface UserDao {
public void update();
}
public class UserDaoImpl implements UserDao{
@Override
public void update() {
System.out.println("update......");
}
}
(2)在service内调用dao的方法
public class UserService {
// 创建UserDao类型属性
private UserDao userDao;
// 生成set方法
public void setUserDao(UserDao userDao) {
this.userDao = userDao;
}
public void add() {
System.out.println("add......");
userDao.update();
}
}
(3)在Spring配置文件中进行配置
<!--service和dao对象创建-->
<bean id="userService" class="com.service.UserService">
<!--注入UserDao对象
name属性:类里面属性名称
ref属性:创建UserDao对象bean标签id值
-->
<property name="userDao" ref="userDao"></property>
</bean>
<bean id="userDao" class="com.dao.UserDaoImpl"></bean>
(4)测试
@Test
public void test(){
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("bean2.xml");
UserService userService = context.getBean("userService", UserService.class);
System.out.println(userService);
userService.add();
}
③ 注入属性——内部bean
(1)一对多关系:部门和员工,一个部门有多个员工,一个员工属于一个部门
(2)在实体类之间表示一对多关系,使用对象类型的属性表示员工所属部门
// 部门类
public class Department {
private String departmentName;
public void setDepartmentName(String departmentName) {
this.departmentName = departmentName;
}
}
// 员工类
public class Employee {
private String name;
private String gender;
// 使用对象属性表示员工所属部门
private Department department;
public void setName(String name) {
this.name = name;
}
public void setGender(String gender) {
this.gender = gender;
}
}
(3)在Spring配置文件中进行配置
<!--内部bean-->
<bean id="employee" class="com.wxm.Employee">
<property name="name" value="Lucy"></property>
<property name="gender" value="女"></property>
<!--设置对象类型属性-->
<property name="department">
<bean id="department" class="com.wxm.Department">
<property name="departmentName" value="财务部"></property>
</bean>
</property>
</bean>
(4)测试
@Test
public void test(){
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("bean3.xml");
Employee employee = context.getBean("employee", Employee.class);
System.out.println(employee);
employee.add();
}
④ 注入属性——级联赋值
(1)第一种写法
<!--级联赋值-->
<bean id="employee" class="com.wxm.Employee">
<property name="name" value="Lucy"></property>
<property name="gender" value="女"></property>
<!--设置对象类型属性-->
<property name="department" ref="department"></property>
</bean>
<bean id="department" class="com.wxm.Department">
<property name="departmentName" value="人事部"></property>
</bean>
(2)第二种写法
- 在Employee类中添加对象属性的get方法
public Department getDepartment() {
return department;
}
- 配置文件
<!--级联赋值-->
<bean id="employee" class="com.wxm.Employee">
<property name="name" value="Lucy"></property>
<property name="gender" value="女"></property>
<!--设置对象类型属性-->
<property name="department" ref="department"></property>
<property name="department.departmentName" value="技术部"></property>
</bean>
<bean id="department" class="com.wxm.Department">
</bean>
Bean管理(XML注入集合属性)
- 注入数组类型属性
- 注入List集合类型属性
- 注入Map集合类型属性
(1)创建类,定义数组、List、Map、Set类型属性,并生成set方法
public class Student {
// 1. 数组类型的属性
private String[] courses;
// 2. List集合类型属性
private List<String> list;
// 3. Map集合类型属性
private Map<String, String> map;
// 4. Set集合类型属性
private Set<String> set;
public void setCourses(String[] courses) {
this.courses = courses;
}
public void setList(List<String> list) {
this.list = list;
}
public void setMap(Map<String, String> map) {
this.map = map;
}
public void setSet(Set<String> set) {
this.set = set;
}
public void test(){
System.out.println("数组:" + Arrays.toString(courses));
System.out.println("List集合:" + list);
System.out.println("Map集合:" + map);
System.out.println("Set集合:" + set);
}
}
(2)在Spring配置文件进行配置
<bean id="student" class="com.wxm.collectiontype.Student">
<!--数组类型注入-->
<property name="courses">
<array>
<value>Java</value>
<value>数据库</value>
</array>
</property>
<!--List类型注入-->
<property name="list">
<list>
<value>张三</value>
<value>李四</value>
</list>
</property>
<!--Map类型注入-->
<property name="map">
<map>
<entry key="JAVA" value="java"></entry>
<entry key="PYTHON" value="python"></entry>
</map>
</property>
<!--Set类型注入-->
<property name="set">
<set>
<value>数学</value>
<value>语文</value>
</set>
</property>
</bean>
(3)测试
public class TestSpring5 {
@Test
public void testCollection() {
ApplicationContext context = new ClassPathXmlApplicationContext("bean1.xml");
Student student = context.getBean("student", Student.class);
student.test();
}
}
(4)在集合里设置对象类型值
<!--创建多个Course对象-->
<bean id="course1" class="com.wxm.collectiontype.Course">
<property name="courseName" value="Spring5框架课程"></property>
</bean>
<bean id="course2" class="com.wxm.collectiontype.Course">
<property name="courseName" value="MyBatis框架课程"></property>
</bean>
<!--注入List集合类型,值是对象-->
<property name="courseList">
<list>
<ref bean="course1"></ref>
<ref bean="course2"></ref>
</list>
</property>
(5)把集合注入部分提取出来
- 在Spring配置文件中引入名称空间
util
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd">
</beans>
- 使用
util
标签完成List集合注入提取
<!--提取List集合类型属性注入-->
<util:list id="bookList">
<value>易筋经</value>
<value>九阴真经</value>
<value>葵花宝典</value>
</util:list>
<!--提取List集合类型属性注入使用-->
<bean id="book" class="com.wxm.collectiontype.Book">
<property name="list" ref="bookList"></property>
</bean>
FactoryBean
Spring有两种类型bean,一种普通bean,另外一种工厂bean(FactoryBean)。
1、普通bean:在配置文件中定义的bean类型就是返回类型。
2、工厂bean:在配置文件中定义的bean类型可以和返回类型不一样。
- 第一步:创建类,让这个类作为工厂bean,实现接口FactoryBean。
- 第二步:实现接口里面的方法,在实现的方法中定义返回的bean类型。
public class MyBean implements FactoryBean<Course> {
// 定义返回类型
@Override
public Course getObject() throws Exception {
Course course = new Course();
course.setCourseName("数据结构");
return course;
}
@Override
public Class<?> getObjectType() {
return null;
}
@Override
public boolean isSingleton() {
return false;
}
}
<bean id="myBean" class="com.wxm.FactoryBean.MyBean"></bean>
@Test
public void testCollection3(){
ApplicationContext context = new ClassPathXmlApplicationContext("bean3.xml");
Course course = context.getBean("myBean", Course.class);
System.out.println(course);
}
bean作用域
(1)在Spring里面,设置创建bean实例是单实例或者多实例
(2)在Spring里面,默认情况下,bean是单实例对象
(3)设置单实例或者多实例
- 在Spring配置文件
bean
标签里面有属性scope
用于设置单实例或者多实例。 scope
属性值
① 默认值,singleton
表示单实例对象
②prototype
表示多实例对象
<bean id="book" class="com.wxm.collectiontype.Book" scope="prototype"></bean>
③ singleton
和prototype
区别
1)singleton
单实例,prototype
多实例
2)设置scope
值是singleton
时,加载Spring配置文件时就会创建单实例对象;设置scope
值是prototype
时,不是在加载Spring配置文件时创建对象,而是在调用getBean
方法时创建多实例对象。
bean生命周期
1、生命周期:从对象创建到对象销毁的过程
2、bean生命周期
(1)通过构造器创建bean实例(无参数构造)
(2)为bean的属性设置值和对其他bean的引用(调用set方法)
(3)调用bean的初始化方法(需要进行配置)
(4)使用bean(获取到对象)
(5)当容器关闭的时候,调用bean的销毁方法(需要进行配置销毁的方法)
3、演示bean生命周期
public class Orders {
// 无参数构造
public Orders() {
System.out.println("第一步,执行我参数构造创建bean实例");
}
private String OrderName;
public void setOrderName(String orderName) {
this.OrderName = orderName;
System.out.println("第二步,调用set方法设置属性值");
}
// 创建执行的初始化方法
public void initMethod() {
System.out.println("第三步,执行初始化的方法");
}
// 创建执行的销毁方法
public void destroyMethod() {
System.out.println("第五步,执行销毁的方法");
}
}
<bean id="orders" class="com.wxm.bean.Orders" init-method="initMethod" destroy-method="destroyMethod">
<property name="orderName" value="手机"></property>
</bean>
@Test
public void testBean(){
ApplicationContext context = new ClassPathXmlApplicationContext("bean4.xml");
Orders orders = context.getBean("orders", Orders.class);
System.out.println("第四步,获取创建的bean实例对象");
// 手动让bean实例销毁
((ClassPathXmlApplicationContext) context).close();
}
4、bean的后置处理器,bean的生命周期有七步
(1)通过构造器创建bean实例(无参数构造)
(2)为bean的属性设置值和对其他bean的引用(调用set方法)
(3)把bean实例传递给bean后置处理器的方法postProcessBeforeInitialization
(4)调用bean的初始化方法(需要进行配置)
(5)把bean实例传递给bean后置处理器的方法postProcessAfterInitialization
(6)使用bean(获取到对象)
(7)当容器关闭的时候,调用bean的销毁方法(需要进行配置销毁的方法)
5、演示添加后置处理器效果
创建类,实现接口BeanPostProcessor,创建后置处理器
public class MyBeanPost implements BeanPostProcessor {
@Override
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
System.out.println("在初始化之前执行的方法");
return bean;
}
@Override
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
System.out.println("在初始化之后执行的方法");
return bean;
}
}
<!--配置后置处理器-->
<bean id="myBeanPost" class="com.wxm.bean.MyBeanPost"></bean>
IOC操作Bean管理(XML自动装配)
1、什么是自动装配
根据指定的装配规则(属性名称或者属性类型),Spring自动将匹配的属性值进行注入。
2、演示自动装配过程
通过bean
标签属性autowire
,配置自动装配,autowire
有两个常用值:
byName
:根据属性名称注入,注入值bean
的id
值和类属性名称一样byType
:根据属性类型注入
(1)根据属性名称自动注入
<!--实现自动装配-->
<bean id="employee" class="com.wxm.autowire.Employee" autowire="byName">
<!-- <property name="department" ref="department"></property>-->
</bean>
<bean id="department" class="com.wxm.autowire.Department"></bean>
(2)根据属性类型自动注入
<!--实现自动装配-->
<bean id="employee" class="com.wxm.autowire.Employee" autowire="byType">
<!-- <property name="department" ref="department"></property>-->
</bean>
<bean id="department" class="com.wxm.autowire.Department"></bean>
IOC操作Bean管理(外部属性文件)
1、直接配置数据库信息
(1)配置德鲁伊连接池
(2)引入德鲁伊连接池依赖jar包
<!--直接配置连接池-->
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
<property name="url" value="jdbc:mysql://localhost:3306/userDb"></property>
<property name="username" value="root"></property>
<property name="password" value="root"></property>
</bean>
2、引入外部属性文件配置数据库连接池
(1)创建外部属性文件,properties格式文件,写数据库信息
(2)把外部properties属性文件引入到Spring配置文件中
- 引入
context
名称空间
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
- 在Spring配置文件中使用标签引入外部属性文件
<!--引入外部属性文件-->
<context:property-placeholder location="classpath:jdbc.properties"/>
<!--配置连接池-->
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
<property name="driverClassName" value="${prop.driverClass}"></property>
<property name="url" value="${prop.url}"></property>
<property name="username" value="${prop.userName}"></property>
<property name="password" value="${prop.password}"></property>
</bean>