目录
11、IOC操作Bean管理(基于注解方式:简化xml配置)
1、IOC容器概念
1)控制反转,把对象创建和对象之间的调用过程,交给Spring进行管理。
2)使用IOC目的:为了耦合度降低
2、IOC底层原理
1)XML解析、工厂设计模式、反射
2)IOC过程
第一步:xml配置文件,配置创建的对象
<bean id="dao" class="com.zsh.UserDao"></bean>
第二步:有service类和dao类,创建工厂类
class UserFactory{
public static UserDao getDao(){
//1.xml解析
String classValue = class属性值;
//2.通过反射创建对象
Class clazz = Class.forName(classValue);
return (UserDao)clazz.getDeclaredConstructor().newInstance();
}
}
3、IOC(接口)
1)IOC思想基于IOC容器完成,IOC容器底层就是对象工厂
2)Spring提供IOC容器实现方式:(两个接口)
a.BeanFactory:IOC容器基本实现,是Spring内部的使用接口,不提供开发人员进行使用。
** 加载配置获取文件时候不会创建对象,在获取对象(使用)才去创建对象。
b.ApplocationContext:BeanFactory接口的子接口,提供更多更强大的功能,一般由开发人员进行使用。
** 加载配置文件时候就会把在配置文件中的对象进行创建。
3)ApplicationContext接口有实现类
4、IOC操作Bean管理(概念)
1)什么是Bean管理? Bean管理有两方面
a. Spring创建对象
b. Spring注入属性
2)Bean管理操作有两种方式
a. 基于xml配置文件方式实现
b. 基于注解方式实现
5、IOC操作Bean管理(基于xml方式)
1. 基于xml创建对象
<!-- 配置User对象 -->
<bean id="user" class="com.spring.User"></bean>
1)在spring配置文件中,使用bean标签,标签里面添加对应属性,就可以实现对象创建
2)在bean标签有很多属性,介绍常用的属性
* id属性:唯一标识
* class属性:类全路径(包类路径)
3)创建对象时候,默认也是执行无参数构造方法完成对象创建。
2. 基于xml方式注入属性
1)DI:依赖注入,就是注入属性
a. 第一种注入方式:set注入方式
1. 创建类,定义属性和对应的set方法
2. 在spring配置文件中配置对象创建,配置属性注入。
<bean id="book" class="zsh.spring.Book">
<!--2.set方法注入属性-->
<!--
name:类里面属性名称
value:向属性注入的值
-->
<property name="bname" value="天龙八部"></property>
<property name="bauthor" value="金庸"></property>
</bean>
b. 第二种注入方式:有参数构造方式
1.创建类,定义属性,创建属性对应有参构造方法。
2. 在spring配置文件中配置。
<!--3.有参构造注入属性-->
<bean id="orders" class="zsh.spring.Orders">
<constructor-arg name="oname" value="abc"></constructor-arg>
<constructor-arg name="address" value="China"></constructor-arg>
</bean>
c. p名称空间注入(了解)
1)使用p名称空间注入,可以简化基于xml配置方式。
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
2)进行属性注入,在bean标签里面进行操作。
<bean id="book" class="zsh.spring.Book" p:bname="天龙八部" p:bauthor="金庸"</bean>
2)xml注入其他类型属性
a.字面量
1. null值
<property name="address">
<null></null>
</property>
2.属性值包含特殊符号
<!--
属性值包含特殊符号
1.把《》进行转义
2.把带特殊符号内容写到CDATA中
-->
<property name="bname">
<value><![CDATA[《天龙八部》]]></value>
</property>
3) 注入属性-外部bean
a. 创建两个类service类和dao类
b. 在service调用到里面的方法
public interface UserDao {
public abstract void update();
}
public class UserDaoImpl implements UserDao {
@Override
public void update() {
System.out.println("dao update....");
}
}
public class UserService {
private UserDao userDao;
public void setUserDao(UserDao userDao){
this.userDao = userDao;
}
public void add(){
System.out.println("service add .....");
userDao.update();
}
}
c. 在xml中配置
<!-- 1 service和dao对象创建 -->
<bean id="userService" class="zsh.service.UserService">
<!--
name属性:类里面的属性名称
ref属性:创建userDaoImpl对象bean标签
-->
<property name="userDao" ref="userDaoImpl"></property>
</bean>
<bean id="userDaoImpl" class="zsh.dao.Impl.UserDaoImpl"></bean>
4)注入属性-内部bean(很多人喜欢用外部bean,结构明显)
a. 一对多关系:部门和员工(一个部门多个员工,一个员工属于某一个部门 )
b. 在实体类之间表示一对多关系,员工表示所属部门,使用对象类型属性进行表示
//部门类
public class Dept {
private String dname;
public void setDname(String dname) {
this.dname = dname;
}
}
//员工类
public class Emp {
private String ename;
private String gender;
//员工属于某一个部门
private Dept dept;
public void setDept(Dept dept) {
this.dept = dept;
}
public void setEname(String ename) {
this.ename = ename;
}
public void setGender(String gender) {
this.gender = gender;
}
}
c. 在spring配置中进行配置。
<!-- 内部bean -->
<bean id="emp" class="zsh.bean.Emp">
<property name="ename" value="lucky"></property>
<property name="gender" value="女"></property>
<property name="dept">
<bean id="dept" class="zsh.bean.Dept">
<property name="dname" value="安保部"></property>
</bean>
</property>
</bean>
5)注入属性-级联赋值
方式一:同外部bean注入
方式二:xml配置如下(需要在emp表给dept设置getter)
<!-- 级联赋值 -->
<bean id="emp" class="zsh.bean.Emp">
<property name="ename" value="lucky"></property>
<property name="gender" value="女"></property>
<property name="dept" ref="dept"></property>
<property name="dept.dname" value="技术部"></property>
</bean>
<bean id="dept" class="zsh.bean.Dept">
<property name="dname" value="安保部"></property>
</bean>
6)xml注入集合属性(数组、List、Map、Set)
a. 创建类、定义数组、List、Map、Set类型属性,生成对应set方法。
public class Stu {
private String[] courses;
private List<String> list;
private Map<String,String> maps;
private Set<String> sets;
public void setCourses(String[] courses){
this.courses = courses;
}
public void setList(List<String> list){
this.list = list;
}
public void setMaps(Map<String,String> maps){
this.maps = maps;
}
public void setSets(Set<String> sets){
this.sets = sets;
}
}
b. 在Spring配置文件进行配置。
<!-- 集合类型属性注入 -->
<bean id="stu" class="zsh.shuzu.Stu">
<!-- 数组类型属性注入 -->
<property name="courses">
<array><value>java</value></array>
</property>
<!-- list类型属性注入,值是对象类型 -->
<property name="list">
<list>
<ref bean="course1"></ref>
<ref bean="course2"></ref>
</list>
</property>
<!-- Map类型属性注入 -->
<property name="maps">
<map><entry key="JAVA" value="java"></entry></map>
</property>
<!-- Set类型属性注入 -->
<property name="sets">
<set><value>MySql</value></set>
</property>
</bean>
<!--创建多个Course对象-->
<bean id="course1" class="zsh.shuzu.Course">
<property name="cname" value="Spring5框架"></property>
</bean>
<bean id="course2" class="zsh.shuzu.Course">
<property name="cname" value="Mybatis框架"></property>
</bean>
d. 把集合注入部分提取出来。
在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"
xmlns:p="http://www.springframework.org/schema/p"
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">
使用标签完成list集合注入提取
<util:list id="bookList">
<ref bean="course1"></ref>
</util:list>
<!-- list类型属性注入,值是对象 -->
<property name="courseList" ref="bookList"></property>
注意:面试中会问DI 和 IOC区别?
DI 是 IOC中一种具体实现,就表示依赖注入(注入属性),注入属性需要在创建对象的基础之上。
6、IOC操作Bean管理(FactoryBean)
Spring有两种类型bean,一种普通bean,另一种工厂bean(FactoryBean)
1)普通bean:在配置文件中定义bean类型就是返回类型(上面的配置bean就是普通bean)
2)工厂bean:在配置文件定义bean类型可以和返回值类型不一样
第一步:创建类,让这个类作为工厂bean,实现接口FactoryBean
第二步:实现接口里面的方法,在实现的方法中定义返回的bean类型
public class MyBean implements FactoryBean<Course> {
//定义返回bean
@Override
public Course getObject() throws Exception {
Course course = new Course();
course.setCname("abc");
return course;
}
@Override
public Class<?> getObjectType() {
return null;
}
}
7、IOC操作Bean管理(bean作用域)
1)在Spring里面,设置创建bean实例是单实例还是多实例?
** 默认为单实例对象
2)如何设置单实例还是多实例?
在spring配置文件bean标签里面有属性(scope)用于设置
scope属性值:
* 默认值sigleton,表示单实例对象 ;
* prototype,表示多实例对象。
注意: sigleton和prototype区别?
第一:sigleton单实例,prototype多实例
第二:设置scope是sigleton时候,加载spring配置文件时候就会创建单实例对象。
第三:设置scope是prototype时候,不是在加载spring配置文件时候创建对象,在调用getBean方法时候创建多实例对象。
8、IOCBean管理(bean生命周期)
1)生命周期:从对象创建到对象销毁的过程
2)bean生命周期五步(配置后置处理器七步)
a. 通过构造器创建bean实例(无参数构造)
b. 为bean的属性设置值和对其它bean引用(调用set方法)
c. 调用bean的初始化的方法(需要进行配置初始化的方法)
d. bean可以使用了(对象获取到了)
e. 当容器关闭时候,调用bean的销毁的方法(需要进行配置销毁的方法)
<bean id="xxx" class="xxx" init-method="initMethod" destroy-method="destroyMethod">
3)bean的后置处理器,bean生命周期有七步(创建类实现BeanPostProcessor并在xml配置成bean)
c. 把bean实例传递bean后置处理器的方法(调用类的before)
d. 调用bean的初始化的方法(需要进行配置初始化的方法)
e. 把bean实例传递bean后置处理器的方法(调用类的after)
9、IOC操作Bean管理(xml自动装配)
1)什么是自动装配?
根据指定装配规则(属性名称或者属性类型),Spring自动将匹配的属性值进行注入。
2)spring文件中配置(Emp中有Dept属性)
<!--实现自动装配
autowire属性常用两个值:
byName根据属性名称注入 注入bean的id值和类的属性名称一样
byType根据属性类型注入
-->
<bean id="emp" class="autowire.Emp" autowire="byName"></bean>
<bean id="dept" class="zsh.autowire.Dept"></bean>
10、IOC操作Bean管理(外部属性文件)
1)直接配置数据库信息(不展示了)
2) 引入外部属性文件配置数据库连接池(德鲁伊连接池)
<!-- 引入外部属性文件 -->
<context:property-placeholder location="classpath:jdbc.properties"/>
<!-- 配置连接池 -->
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
<property name="driverClassName" value="${jdbc.driverClassName}"></property>
<property name="url" value="${jdbc.url}"></property>
<property name="username" value="${jdbc.username}"></property>
<property name="password" value="${jdbc.password}"></property>
</bean>
11、IOC操作Bean管理(基于注解方式:简化xml配置)
1)Spring针对Bean管理中创建对象提供注解
@Component(value="")
@Service(value="")
@Controller(value="")
@Repository(value="userDaoImp")
* 上面四个注解功能是一样的,都可以用来创建bean实例
2)基于注解方式实现对象创建
第一步:引入aop依赖jar包
第二步:开启组件扫描
<!--1 如果扫描多个包,多个包使用逗号隔开 ;2.扫描包上层目录-->
<context:component-scan base-package=""/>
第三步:创建类,在类上面添加创建对象注解
注意:开启组件扫描细节配置
<!--示例一
use-default-filters="false 表示现在不使用默认filter,自己配置filter
context:include-filter 表示扫描包下Controller注解
-->
<context:component-scan base-package="com" use-default-filters="false">
<context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
<!-- 设置不去扫描哪些内容 -->
<context:component-scan base-package="com">
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
12、基于注解方式实现属性注入
1)Spring提供的注解
@AutoWired:根据属性类型进行自动装配
@Qualifier(value="userDaoImp"):根据属性名称进行自动装配(和@Autowired一起使用)
@Reaource(name="userDaoImp"):可以根据类型注入,也可以根据名称注入
注意:此注解的包是javax中的,不是spring中的
@Value:注入普通类型属性
13、完全注解开发
1)创建配置类,替代xml配置文件
@Configuration //配置类,替代xml配置文件
@ComponentSean(basePackage={}) //包扫描
public class SpringConfig{
}
2)编写测试类
ApplicationContext context = new AnnotationConfigApplicationContext(SpringConfig.class);