IOC具体操作,Bean管理(概念)

1.什么是Bean管理?=》》》》
(0)Bean管理指的是俩个操作
(1)Spring 创建对象
(2)Spring注入属性
image.png
2、Bean管理操作有俩种方式 基于XML和注解的Spring Bean管理
xml配置,注解,扫描?
Spring中Bean管理操作基于XML配置文件方法实现
(1)基于xml配置文件实现
1、基于xml方式创建对象
image.png
1:在spring配置文件中,使用bean标签,标签中添加对应属性,就可实现利用xml创建对象。
2:在bean标签中有很多属性,常用属性有
id属性,给需要创建的对象起一个唯一标识
class属性,需要创建对象类的全路径,包名(package)+类名(Class Name)(com.baidu.User)
*name属性,与id属性功能相同,区别在与name中可以加特殊符号(早期框架struts)
3:在创建对象时候,默认也是执行无参构造方法,完成创建对象。
image.png

基于xml方式注入属性

(1)DI:依赖注入,就是注入属性。DI为IOC中的一种具体实现,DI需要在创建对象的基础之上来进行DI属性注入。
依赖注入方法:set,有参,接口
image.png

1:set方法注入属性,先通过xml的bean标签创建对象,在通过xml中的property标签对该对象进行属性值注入;

image.png
image.png
image.png

2:DI注入第二中方式:有参构造

1.创建订单实体

  1. package com.junjay.spring5;
  2. public class Orders {
  3. // 订单名称
  4. private String oname;
  5. // 订单地址
  6. private String address;
  7. // 有参构造
  8. public Orders(String oname, String address) {
  9. super();
  10. this.oname = oname;
  11. this.address = address;
  12. }
  13. }

2,在spring文件中配置DI有参注入,

  1. <!-- 有参数构造注入属性 -->
  2. <bean id="orders" class="com.junjay.spring5.Orders">
  3. <constructor-arg name="oname" value="单号211"></constructor-arg>
  4. <constructor-arg name="address" value="洛阳"></constructor-arg>
  5. <!-- 对应有参注入中的第一个参数,即oname -->
  6. <!-- <constructor-arg index="0" value="可以通过有参构造的参数序号注入"></constructor-arg> -->
  7. </bean>

在订单实体中加入打印信息方法

  1. package com.junjay.spring5;
  2. public class Orders {
  3. // 订单名称
  4. private String oname;
  5. // 订单地址
  6. private String address;
  7. // 有参构造
  8. public Orders(String oname, String address) {
  9. super();
  10. this.oname = oname;
  11. this.address = address;
  12. }
  13. public void orderText() {
  14. System.err.println("oname="+oname);
  15. System.err.println("address="+address);
  16. }
  17. }

单元测试
image.png

拓展 P名称空间注入

使用p名称空间注入,可以简化基于xml配置的方式
1:通过修改xml配置文件中头部里的xml相关约束

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xmlns:aop="http://www.springframework.org/schema/aop"
  5. <!--区分命名空间,使用到xml中的约束 -->
  6. xmlns:p="http://www.springframework.org/schema/p"
  7. xmlns:tx="http://www.springframework.org/schema/tx"
  8. xmlns:context="http://www.springframework.org/schema/context"
  9. xsi:schemaLocation="
  10. http://www.springframework.org/schema/beans
  11. http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
  12. http://www.springframework.org/schema/aop
  13. http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
  14. http://www.springframework.org/schema/tx
  15. http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
  16. http://www.springframework.org/schema/context
  17. http://www.springframework.org/schema/context/spring-context-3.0.xsd">

2:通过xml 中p命名方式注入值

  1. <!-- set 注入属性 -->
  2. <bean id="book" class="com.junjay.spring5.Book" p:bname="三毛">
  3. </bean>

注入空值和特殊符号

1,字面量(设置对象默认值,固定值)

  1. // 订单名称
  2. private String oname="我是字面量设置的值";
  3. // 订单地址
  4. private String address;
  1. <bean id="book" class="com.junjay.spring5.Book" p:bname="三毛是字面量">
  2. </bean>

1:字面量设置null值:

  1. private String bname;
  2. private String zname;
  3. // set方法
  4. public void setBname(String bname) {
  5. this.bname = bname;
  6. }
  7. public void setZname(String zname) {
  8. this.zname = zname;
  9. }
  1. <bean id="book" class="com.junjay.spring5.Book">
  2. <!-- 使用property 完成属性注入 name:属性名称,value:注入属性的值 -->
  3. <property name="bname" value="小王子"></property>
  4. <!-- 设置zname空值 null -->
  5. <property name="zname">
  6. <null></null>
  7. </property>
  8. </bean>

image.png
2:属性值中包含特殊符号
image.png

方式1:通过&..转义符号实现<>作为name值

  1. <bean id="book" class="com.junjay.spring5.Book">
  2. <!-- 使用property 完成属性注入 name:属性名称,value:注入属性的值 -->
  3. <property name="bname" value="小王子"></property>
  4. <!-- 属性值中包含特殊符号 -->
  5. <property name="zname" value="&lt;左右尖括号被实别为标签符号&gt;">
  6. </property>
  7. </bean>

image.png

方式2:把特殊符号的内容写入到CDATA中,例如:

<![CDATA[<红色内容value>]]>

  1. <bean id="book" class="com.junjay.spring5.Book">
  2. <!-- 使用property 完成属性注入 name:属性名称,value:注入属性的值 -->
  3. <property name="bname" value="小王子"></property>
  4. <!-- 属性值中包含特殊符号 -->
  5. <property name="zname">
  6. <value><![CDATA[<被CDATA包裹的内容原样输出,不会被实别为标签>]]></value>
  7. </property>
  8. </bean>

image.png

注入属性——外部bean

1:创建俩个类service 类和 dao类
2:在service中调用dao方法

  1. public class UserService {
  2. // 原始方法
  3. public void add() {
  4. System.err.println("service -------- add ");
  5. // 创建UserDao 对象
  6. // 创建实现类UserDaoImpl以UserDao接受,这个过程就是多态
  7. // 接口 new 接口实现类
  8. UserDao userdao = new UserDaoImpl();
  9. userdao.add();
  10. }
  11. }
  1. /**
  2. * interface 接口关键字
  3. * dao 接口
  4. * @author My
  5. *
  6. */
  7. public interface UserDao {
  8. /**
  9. * 多外接口
  10. */
  11. void add();
  12. void update();
  13. }
  1. /**
  2. * implements 实现接口关键字
  3. * dao接口实现类
  4. * @author My
  5. *
  6. */
  7. public class UserDaoImpl implements UserDao{
  8. @Override
  9. public void add() {
  10. System.err.println("UserDaoImpl -------- add ");
  11. }
  12. @Override
  13. public void update() {
  14. System.err.println("UserDaoImpl -------- update ");
  15. }
  16. }

3:在spring配置文件中实现以上方法,首先通过配置文件创建俩个对象,然后在通过DI注入的方式把UserDao注入到UserServive中(注入方式可以为set注入,或有参注入)

  1. <!-- 1,service 和 dao 创建对象 -->
  2. <bean id="userService" class="com.junjay.spring5.service.UserService">
  3. <!-- set 方式注入userDao,
  4. name:为UserService的属性名称
  5. ref:为创建userDao对象的bean标签的id的值(外部bean注入)
  6. -->
  7. <property name="userDao" ref="userDaoImpl">
  8. </property>
  9. </bean>
  10. <!-- UserDao是被interface修饰的接口,接口无法实例化创建对象,所以需要创建接口实现类的对象 -->
  11. <bean id="userDaoImpl" class="com.junjay.spring5.dao.UserDaoImpl"></bean>

image.png
image.png

  1. 警告: Exception encountered during context initialization
  2. - cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException:
  3. Error creating bean with name 'userService' defined in class path resource [bean2.xml]:
  4. Error setting property values;
  5. nested exception is org.springframework.beans.NotWritablePropertyException:
  6. Invalid property 'userDao' of bean class [com.junjay.spring5.service.UserService]:
  7. Bean property 'userDao' is not writable or has an invalid setter method. Did you mean 'userdao'?

image.png

注入属性—内部bean和级联赋值

1:一对多关系 :部门与员工(一个部门有多个员工,一个员工属于一个部门)
2:在实体类之间表示一对多关系

  1. /**
  2. * @author 部门实体类
  3. *
  4. */
  5. public class Dept {
  6. private String dname;
  7. public void setDname(String dname) {
  8. this.dname = dname;
  9. }
  10. @Override
  11. public String toString() {
  12. return "Dept [dname=" + dname + "]";
  13. }
  14. }
  1. /**
  2. * @author 员工实体
  3. *
  4. */
  5. public class Emp {
  6. private String ename;
  7. private String gender;
  8. // 员工属于某一部门,使用对象表示
  9. private Dept dept;
  10. public void setEname(String ename) {
  11. this.ename = ename;
  12. }
  13. public void setGender(String gender) {
  14. this.gender = gender;
  15. }
  16. public void setDept(Dept dept) {
  17. this.dept = dept;
  18. }
  19. public void add() {
  20. System.out.println("ename=" + ename);
  21. System.out.println("gender=" + gender);
  22. System.out.println(dept.toString());
  23. }
  24. }

3:在spring配置文件中,配置相关实体内部bean

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xmlns:aop="http://www.springframework.org/schema/aop"
  5. xmlns:p="http://www.springframework.org/schema/p"
  6. xmlns:tx="http://www.springframework.org/schema/tx"
  7. xmlns:context="http://www.springframework.org/schema/context"
  8. xsi:schemaLocation="
  9. http://www.springframework.org/schema/beans
  10. http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
  11. http://www.springframework.org/schema/aop
  12. http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
  13. http://www.springframework.org/schema/tx
  14. http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
  15. http://www.springframework.org/schema/context
  16. http://www.springframework.org/schema/context/spring-context-3.0.xsd">
  17. <!-- 内部bean -->
  18. <!-- 1:创建员工实体对象 -->
  19. <bean id="emp" class="com.junjay.spring5.bean.Emp">
  20. <!-- 设置俩个普通属性 -->
  21. <property name="ename" value="张三"></property>
  22. <property name="gender" value="男"></property>
  23. <!-- 设置dept对象属性 -->
  24. <property name="dept">
  25. <!-- 嵌套dept对象并且给dept内的属性赋值 -->
  26. <!-- 这种内部嵌套的方式就是内部bean -->
  27. <bean id="dept" class="com.junjay.spring5.bean.Dept">
  28. <property name="dname" value="人事部"></property>
  29. </bean>
  30. </property>
  31. </bean>
  32. </beans>

测试
image.png

注入属性—级联赋值

方式1:引入外部对象,设置外部对象属性值

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xmlns:aop="http://www.springframework.org/schema/aop"
  5. xmlns:p="http://www.springframework.org/schema/p"
  6. xmlns:tx="http://www.springframework.org/schema/tx"
  7. xmlns:context="http://www.springframework.org/schema/context"
  8. xsi:schemaLocation="
  9. http://www.springframework.org/schema/beans
  10. http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
  11. http://www.springframework.org/schema/aop
  12. http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
  13. http://www.springframework.org/schema/tx
  14. http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
  15. http://www.springframework.org/schema/context
  16. http://www.springframework.org/schema/context/spring-context-3.0.xsd">
  17. <!-- 内部bean -->
  18. <!-- 1:创建员工实体对象 -->
  19. <bean id="emp" class="com.junjay.spring5.bean.Emp">
  20. <!-- 设置俩个普通属性 -->
  21. <property name="ename" value="张三"></property>
  22. <property name="gender" value="男"></property>
  23. <!-- 级联外部赋值,dept对象属性 -->
  24. <property name="dept" ref="dept">
  25. </property>
  26. </bean>
  27. <bean id="dept" class="com.junjay.spring5.bean.Dept">
  28. <property name="dname" value="财务部"></property>
  29. </bean>
  30. </beans>

image.png
方式2:级联内部赋值

  1. <!-- 内部bean -->
  2. <!-- 1:创建员工实体对象 -->
  3. <bean id="emp" class="com.junjay.spring5.bean.Emp">
  4. <!-- 设置俩个普通属性 -->
  5. <property name="ename" value="张三"></property>
  6. <property name="gender" value="男"></property>
  7. <!-- 级联内部赋值,dept对象属性 -->
  8. <property name="dept" ref="dept"></property>
  9. <!-- 如果emp实体类中没有dept的get方法则允许报错,因为没有get方法就获取不到dept.dname -->
  10. <property name="dept.dname" value="财务部"></property>
  11. </bean>

在emp对象中添加get方法以获取dept对象中的dname,供xml中的dept.dname进行值注入

  1. // Dept实体上面已经写过,在实体内添加get方法即可
  2. // 生成dept的get方法才能获取到spring配置文件中dept对象的dept.dname
  3. public Dept getDept() {
  4. return dept;
  5. }

xml注入集合属性

1:注入数组类型属性
1:创建包含个集合类型的对象实体并生成sett方法
数组,Set, 注入List集合属性3:注入Map集合属性

  1. package com.junjay.spring5.collectionbean;
  2. import java.sql.Array;
  3. import java.util.Arrays;
  4. import java.util.List;
  5. import java.util.Map;
  6. import java.util.Set;
  7. public class Stu {
  8. // 数组类型 属性
  9. private String[] sarr;
  10. // List集合类型属性
  11. private List slist;
  12. // Map集合类型属性
  13. private Map<String, String> smap;
  14. // Set集合属性
  15. private Set<String> set;
  16. public void setSarr(String[] sarr) {
  17. this.sarr = sarr;
  18. }
  19. public void setSlist(List slist) {
  20. this.slist = slist;
  21. }
  22. public void setSmap(Map<String, String> smap) {
  23. this.smap = smap;
  24. }
  25. public void setSet(Set<String> set) {
  26. this.set = set;
  27. }
  28. /**
  29. * 数组打印
  30. */
  31. public void getArr() {
  32. System.out.println(Arrays.toString(sarr));
  33. }
  34. public void getList() {
  35. System.out.println(slist);
  36. }
  37. public void getMap() {
  38. System.out.println(smap);
  39. }
  40. public void getSet() {
  41. System.out.println(set);
  42. }
  43. }

配置xml文件

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xmlns:aop="http://www.springframework.org/schema/aop"
  5. xmlns:p="http://www.springframework.org/schema/p"
  6. xmlns:tx="http://www.springframework.org/schema/tx"
  7. xmlns:context="http://www.springframework.org/schema/context"
  8. xsi:schemaLocation="
  9. http://www.springframework.org/schema/beans
  10. http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
  11. http://www.springframework.org/schema/aop
  12. http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
  13. http://www.springframework.org/schema/tx
  14. http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
  15. http://www.springframework.org/schema/context
  16. http://www.springframework.org/schema/context/spring-context-3.0.xsd">
  17. <!-- 集合类型属性注入 -->
  18. <bean id="stu" class="com.junjay.spring5.collectionbean.Stu">
  19. <!-- 数组类型属性注入 -->
  20. <property name="sarr">
  21. <!-- 数组类型可多内容,用array或list标签,注入多个值 -->
  22. <array>
  23. <value>数组1</value>
  24. <value>数组2</value>
  25. </array>
  26. </property>
  27. <!-- List类型属性注入 -->
  28. <property name="slist">
  29. <!-- List类型可多内容,用list标签,注入多个值 -->
  30. <list>
  31. <value>list1</value>
  32. <value>list2</value>
  33. </list>
  34. </property>
  35. <!-- Map类型属性注入 -->
  36. <property name="smap">
  37. <!-- Map类型可多内容,用entry标签,以key,value注入 -->
  38. <map>
  39. <entry key="Java" value="java"></entry>
  40. <entry key="Spring" value="spring"></entry>
  41. </map>
  42. </property>
  43. <!-- Set类型属性注入 -->
  44. <property name="set">
  45. <!-- Set类型可多内容,用entry标签,以key,value注入 -->
  46. <set>
  47. <value>set1</value>
  48. <value>set2</value>
  49. </set>
  50. </property>
  51. </bean>
  52. </beans>

测试
image.png
在集合里面设置对象类型值

  1. package com.junjay.spring5.collectionbean;
  2. // 集合中的对象
  3. public class Cos {
  4. private String cname;
  5. public void setCname(String cname) {
  6. this.cname = cname;
  7. }
  8. @Override
  9. public String toString() {
  10. return "Cos [cname=" + cname + "]";
  11. }
  12. }
  1. // 在上述Stu中加入集合对象属性 并生成set方法
  2. // 集合对象
  3. private List<Cos> cosList;
  4. public void setCosList(List<Cos> cosList) {
  5. this.cosList = cosList;
  6. }
  1. <!-- 集合类型属性注入 -->
  2. <bean id="stu" class="com.junjay.spring5.collectionbean.Stu">
  3. <!-- List集合类型,值为对象 类型属性注入 -->
  4. <property name="cosList">
  5. <list>
  6. <ref bean="cos1"></ref>
  7. <ref bean="cos2"></ref>
  8. </list>
  9. </property>
  10. </bean>
  11. <!-- 在上述xml配置文件中添加多对象值,注入stu 集合对象中 -->
  12. <!-- 创建多个cos对象通过ref注入到stu list对象中 -->
  13. <bean id="cos1" class="com.junjay.spring5.collectionbean.Cos">
  14. <property name="cname" value="cos1"></property>
  15. </bean>
  16. <bean id="cos2" class="com.junjay.spring5.collectionbean.Cos">
  17. <property name="cname" value="cos2"></property>
  18. </bean>

image.png

集合注入部分抽取提取出来
1:创建对象

  1. package com.junjay.spring5.collectionbean;
  2. import java.util.List;
  3. public class Boss {
  4. private List<String> blist;
  5. public void setBlist(List<String> blist) {
  6. this.blist = blist;
  7. }
  8. public void test() {
  9. System.out.println(blist);
  10. }
  11. }

在xml配置文件中添加命名空间util

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xmlns:aop="http://www.springframework.org/schema/aop"
  5. xmlns:p="http://www.springframework.org/schema/p"
  6. // 加命名空间
  7. xmlns:util="http://www.springframework.org/schema/util"
  8. xmlns:tx="http://www.springframework.org/schema/tx"
  9. xmlns:context="http://www.springframework.org/schema/context"
  10. xsi:schemaLocation="
  11. http://www.springframework.org/schema/beans
  12. http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
  13. http://www.springframework.org/schema/aop
  14. http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
  15. http://www.springframework.org/schema/tx
  16. http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
  17. http://www.springframework.org/schema/context
  18. http://www.springframework.org/schema/context/spring-context-3.0.xsd
  19. // 加util工具类
  20. http://www.springframework.org/schema/util
  21. http://www.springframework.org/schema/util/spring-util.xsd
  22. ">
  23. <!-- 1:提取list集合类型属性注入 -->
  24. <util:list id="blist">
  25. <!-- 如果list为对象则用ref -->
  26. <value>1</value>
  27. <value>2</value>
  28. <value>3</value>
  29. </util:list>
  30. <!-- 2:提取list集合类型属性注入使用 -->
  31. <bean id="boss" class="com.junjay.spring5.collectionbean.Boss">
  32. <!-- List类型属性注入 -->
  33. <property name="slist" ref="blist">
  34. </property>
  35. </bean>
  36. </beans>

测试
image.png

spring中有俩种bean

一种是普通的bean,我们自己创建的bean
一个是工厂bean(FactoryBean),spring内置bean

1:普通bean:在配置文件中定义的bean类型就是返回的类型

eg:class=”..User>” 则自能返回Userbean对象

2:工厂bean:在配置文件中定义的bean类型可以和返回类型不一样

1:创建类,让类做为工厂bean,实现FactoryBean接口

  1. package com.junjay.spring5.factorybean;
  2. import org.springframework.beans.factory.FactoryBean;
  3. /**
  4. * mybean 普通类先实现FactoryBean接口
  5. * 实现接口后需要重写接口内方法
  6. * @author My
  7. *
  8. */
  9. public class MyBean implements FactoryBean{
  10. /**
  11. * 设置返回类型
  12. */
  13. @Override
  14. public Cou getObject() throws Exception {
  15. Cou cou = new Cou();
  16. cou.setCname("222");
  17. return cou;
  18. }
  19. @Override
  20. public Class getObjectType() {
  21. return null;
  22. }
  23. }
  1. package com.junjay.spring5.factorybean;
  2. public class Cou {
  3. private String cname;
  4. public void setCname(String cname) {
  5. this.cname = cname;
  6. }
  7. @Override
  8. public String toString() {
  9. return "Cou [cname=" + cname + "]";
  10. }
  11. }

2:实现接口里的方法,在实现方法中定义bean类型

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xmlns:aop="http://www.springframework.org/schema/aop"
  5. xmlns:p="http://www.springframework.org/schema/p"
  6. xmlns:util="http://www.springframework.org/schema/util"
  7. xmlns:tx="http://www.springframework.org/schema/tx"
  8. xmlns:context="http://www.springframework.org/schema/context"
  9. xsi:schemaLocation="
  10. http://www.springframework.org/schema/beans
  11. http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
  12. http://www.springframework.org/schema/aop
  13. http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
  14. http://www.springframework.org/schema/tx
  15. http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
  16. http://www.springframework.org/schema/context
  17. http://www.springframework.org/schema/context/spring-context-3.0.xsd
  18. http://www.springframework.org/schema/util
  19. http://www.springframework.org/schema/util/spring-util.xsd
  20. ">
  21. <bean id="mybean" class="com.junjay.spring5.factorybean.MyBean">
  22. </bean>
  23. </beans>

@Test 单元测试
image.png

bean作用域

1:在spring里面,设置创建bean可以设置是单实例和多实例;

2:spring默认情况下,创建的是单实例对象

默认单例:@5579bb86为bean地址
image.png

3:如何设置单实例,多实例

(1):在spring配置文件中,bean标签内有属性(socpe),用于设置单例还是多例
(2):socpe属性值
2.1 singleton 默认值,单例

2.2 prototype 多例,每次获取重新实例化

2.3 request 每次请求重新实例化

2.4 session 每个会话对象内,对象是单例

2.5 application 在application对象内是单例

2.6 global session 是spring推出的对象,它依赖于spring-webmvc-portlet ,类似于session
配置文件中设置多实例,获取bean对象地址则不相同,也证明了返回对象不是一个对象
image.png
image.png
(3):singleton和prototype 区别
singleton,表示单实例,prototype ,表示多实例
设置scope值是singleton时,加载spring配置文件时候已经创建单实例对象(饿汉式)
image.png
设置prototype 值时,不是在加载spring文件时候创建对象,而是在调用getBean时候进行多实例对象创建(懒汉式)
image.png

Bean生命周期

1:生命周期:
(1):从对象的创建到对象销毁的过程
2: bean生命周期
(1):通过构造器创建bean实例(无参构造)
(2):为bean的属性设置值和对其他bean引用(调用set方法)
(3):调用bean的初始化方法(需要进行配置)
(4):bean实例创建成功得到bean对象
(5):当容器关闭的时候,调用bean的销毁的方法(需要进行配置销毁方法)
3:演示bean生命周期
【java】this()与super()使用详解
(1):创建对象

  1. package com.junjay.spring5.bean;
  2. public class Ord {
  3. private String oname;
  4. public void setOname(String oname) {
  5. this.oname = oname;
  6. System.out.println("(2):为bean的属性设置值和对其他bean引用(调用set方法)");
  7. }
  8. /**
  9. * 生命周期 (1):通过构造器创建bean实例(无参构造)
  10. */
  11. public Ord() {
  12. super();
  13. System.out.println("(1):通过构造器创建bean实例(无参构造)");
  14. }
  15. /**
  16. * 调用bean的初始化方法
  17. */
  18. public void init() {
  19. System.out.println("(3):调用bean的初始化方法(需要进行配置)");
  20. }
  21. /**
  22. * 对象销毁方法,需要在配置文件中配置,在配置文件中配置destroy-method,但不能使其销毁,
  23. * 需要手动让bean实例销毁
  24. */
  25. public void destroy() {
  26. System.out.println("(5):当容器关闭的时候,调用bean的销毁的方法(需要进行配置销毁方法)");
  27. }
  28. }

配置文件

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xmlns:aop="http://www.springframework.org/schema/aop"
  5. xmlns:p="http://www.springframework.org/schema/p"
  6. xmlns:util="http://www.springframework.org/schema/util"
  7. xmlns:tx="http://www.springframework.org/schema/tx"
  8. xmlns:context="http://www.springframework.org/schema/context"
  9. xsi:schemaLocation="
  10. http://www.springframework.org/schema/beans
  11. http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
  12. http://www.springframework.org/schema/aop
  13. http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
  14. http://www.springframework.org/schema/tx
  15. http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
  16. http://www.springframework.org/schema/context
  17. http://www.springframework.org/schema/context/spring-context-3.0.xsd
  18. http://www.springframework.org/schema/util
  19. http://www.springframework.org/schema/util/spring-util.xsd
  20. ">
  21. <!--1: 创建bean实例 -->
  22. <bean id="ord" class="com.junjay.spring5.bean.Ord"
  23. init-method="init" destroy-method="destroy"><!-- init-method调用初始化方法 -->
  24. <!-- 2:调用set方法 -->
  25. <property name="oname" value="阿斯蒂"></property>
  26. </bean>
  27. </beans>

@Test测试
image.png
使用ApplicationContext接受时,没有close方法,因为ApplicationContext没有close,我们需要用他的实现类,或子接口进行bean销毁。
image.png
4:bean后置处理器,bean生命周期变成7步
(1):通过构造器创建bean实例(无参构造)
(2):为bean的属性设置值和对其他bean引用(调用set方法)
(3):在bean初始化之前会调用的一个方法(把bean实例传递bean后置处理器的方法,即BeanPostProcessor接口中的postProcessBeforeInitialization 为初始化之前的方法)
(4):调用bean的初始化方法(需要进行配置)
(5):在bean初始化之后会调用的一个方法把bean实例传递bean后置处理器的方法,即BeanPostProcessor接口中的postProcessAfterInitialization为初始化之前的方法)
(6):bean实例创建成功得到bean对象
(7):当容器关闭的时候,调用bean的销毁的方法(需要进行配置销毁方法)
5:演示添加bean后置处理器之后效果
(1):创建类,实现BeanPostProcessor接口,传教后置处理器

  1. package com.junjay.spring5.bean;
  2. import org.springframework.beans.BeansException;
  3. import org.springframework.beans.factory.config.BeanPostProcessor;
  4. /**
  5. * 改类实现BeanPostProcessor接口
  6. * 只要实现改接口在创建所有对象时候都会调用
  7. *
  8. * @author My
  9. *
  10. */
  11. public class MyBeanPost implements BeanPostProcessor {
  12. /**
  13. * bean是容器调用构造器创建的实例 beanName是实例的id 在所有初始化方法之前调用
  14. */
  15. @Override
  16. public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
  17. System.out.println("(2.5)在bean初始化之前会调用的一个方法");
  18. return bean;
  19. }
  20. /**
  21. * bean是容器调用构造器创建的实例 beanName是实例的id 在所有初始化方法之后调用
  22. */
  23. @Override
  24. public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
  25. System.out.println("(3.5)在bean初始化之后会调用的一个方法");
  26. return bean;
  27. }
  28. }
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xmlns:aop="http://www.springframework.org/schema/aop"
  5. xmlns:p="http://www.springframework.org/schema/p"
  6. xmlns:util="http://www.springframework.org/schema/util"
  7. xmlns:tx="http://www.springframework.org/schema/tx"
  8. xmlns:context="http://www.springframework.org/schema/context"
  9. xsi:schemaLocation="
  10. http://www.springframework.org/schema/beans
  11. http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
  12. http://www.springframework.org/schema/aop
  13. http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
  14. http://www.springframework.org/schema/tx
  15. http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
  16. http://www.springframework.org/schema/context
  17. http://www.springframework.org/schema/context/spring-context-3.0.xsd
  18. http://www.springframework.org/schema/util
  19. http://www.springframework.org/schema/util/spring-util.xsd
  20. ">
  21. <!--1: 创建bean实例 -->
  22. <bean id="ord" class="com.junjay.spring5.bean.Ord"
  23. init-method="init" destroy-method="destroy"><!-- init-method调用初始化方法 -->
  24. <!-- 2:调用set方法 -->
  25. <property name="oname" value="阿斯蒂"></property>
  26. </bean>
  27. <!--配置后置处理器,配置后bean无需引用自动调用 -->
  28. <bean id="myBeanPost" class="com.junjay.spring5.bean.MyBeanPost"></bean>
  29. </beans>

BeanPostProcessor 接口
image.png
实现效果
image.png

XML自动装配

1:什么是自动装配
(1):根据指定装配规则(属性名称或属性类型),spring自动将匹配的属性值进行注入;
2:演示自动装配过程
创建类

  1. package com.junjay.spring5.autowire;
  2. public class Em {
  3. private Dep dep;
  4. public void setDep(Dep dep) {
  5. this.dep = dep;
  6. }
  7. @Override
  8. public String toString() {
  9. return "Em [dep=" + dep + "]";
  10. }
  11. public void test() {
  12. System.out.println(dep);
  13. }
  14. }
  1. package com.junjay.spring5.bean;
  2. /**
  3. * @author 员工实体
  4. *
  5. */
  6. public class Emp {
  7. private String ename;
  8. private String gender;
  9. // 员工属于某一部门,使用对象表示
  10. private Dept dept;
  11. public void setEname(String ename) {
  12. this.ename = ename;
  13. }
  14. public void setGender(String gender) {
  15. this.gender = gender;
  16. }
  17. public void setDept(Dept dept) {
  18. this.dept = dept;
  19. }
  20. // 生成dept的get方法才能获取到spring配置文件中dept对象的dept.dname
  21. public Dept getDept() {
  22. return dept;
  23. }
  24. public void add() {
  25. System.out.println("ename=" + ename);
  26. System.out.println("gender=" + gender);
  27. System.out.println(dept.toString());
  28. }
  29. }

XML配置文件配置自动装配:autowire标签

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xmlns:aop="http://www.springframework.org/schema/aop"
  5. xmlns:p="http://www.springframework.org/schema/p"
  6. xmlns:util="http://www.springframework.org/schema/util"
  7. xmlns:tx="http://www.springframework.org/schema/tx"
  8. xmlns:context="http://www.springframework.org/schema/context"
  9. xsi:schemaLocation="
  10. http://www.springframework.org/schema/beans
  11. http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
  12. http://www.springframework.org/schema/aop
  13. http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
  14. http://www.springframework.org/schema/tx
  15. http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
  16. http://www.springframework.org/schema/context
  17. http://www.springframework.org/schema/context/spring-context-3.0.xsd
  18. http://www.springframework.org/schema/util
  19. http://www.springframework.org/schema/util/spring-util.xsd
  20. ">
  21. <!-- 实现自动装配
  22. bean标签属性中autowire,配置自动装配
  23. autowire属性常用俩个值:
  24. byname根据属性名称注入; 注入值的bean的id值要和类属性名称一致
  25. bytype根据属性类型注入;
  26. -->
  27. <!-- 根据类型注入 <bean id="em" class="com.junjay.spring5.autowire.Em" autowire="byType"> -->
  28. <bean id="em" class="com.junjay.spring5.autowire.Em" autowire="byName">
  29. <!-- <property name="dep" ref="dep"></property> -->
  30. </bean>
  31. <bean id="dep" class="com.junjay.spring5.autowire.Dep"></bean>
  32. </beans>

junit测试
image.png

引入外部属性文件

1:直接配置数据库信息
(1):配置德鲁伊连接池
(2):引入德鲁伊连接池的jar
image.png
image.png

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xmlns:aop="http://www.springframework.org/schema/aop"
  5. xmlns:p="http://www.springframework.org/schema/p"
  6. xmlns:util="http://www.springframework.org/schema/util"
  7. xmlns:context="http://www.springframework.org/schema/context"
  8. xmlns:tx="http://www.springframework.org/schema/tx"
  9. xsi:schemaLocation="
  10. http://www.springframework.org/schema/beans
  11. http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
  12. http://www.springframework.org/schema/aop
  13. http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
  14. http://www.springframework.org/schema/tx
  15. http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
  16. http://www.springframework.org/schema/context
  17. http://www.springframework.org/schema/context/spring-context-3.0.xsd
  18. http://www.springframework.org/schema/util
  19. http://www.springframework.org/schema/util/spring-util.xsd
  20. ">
  21. <!-- 直接通过druidjar配置数据库链接 -->
  22. <bean id="dataSource"
  23. class="com.alibaba.druid.pool.DruidDataSource">
  24. <!-- 直接配置 *********************************************************** -->
  25. <!-- <property name="driverClassName" value="com.mysql.jdbc.Driver"></property> -->
  26. <!-- <property name="url" value="jdbc:mysql://localhost:3306/user"></property> -->
  27. <!-- <property name="username" value="root"></property> -->
  28. <!-- <property name="password" value="root"></property> -->
  29. <!-- 外部配置 ************************************************************** -->
  30. <!-- 获取properties文件中内容,根据key获取,使用spring表达式获取值,进行数据库链接配置 -->
  31. <!-- 驱动名称 com.mysql.jdbc.Driver -->
  32. <property name="driverClassName" value="${jdbc.driverClass}"></property>
  33. <!-- 地址 jdbc:mysql://localhost:3306/user -->
  34. <property name="url" value="${jdbc.url}"></property>
  35. <!-- 链接数据库用户名 -->
  36. <property name="username" value="${jdbc.username}"></property>
  37. <!-- 链接数据库用户密码 -->
  38. <property name="password" value="${jdbc.password}"></property>
  39. </bean>
  40. </beans>

2:引入外部属性文件配置数据库连接池
(1):创建外部属性文件,propreties格式文件,写入数据库链接信息

  1. prop.driverClass=com.mysql.jdbc.Driver
  2. prop.url=jdbc:mysql://localhost:3306/test_demo
  3. prop.username=root
  4. prop.password=root
  1. (2):xml配置文件中添加context命名空间,解析上下文
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xmlns:aop="http://www.springframework.org/schema/aop"
  5. xmlns:p="http://www.springframework.org/schema/p"
  6. xmlns:util="http://www.springframework.org/schema/util"
  7. xmlns:context="http://www.springframework.org/schema/context"
  8. xmlns:tx="http://www.springframework.org/schema/tx"
  9. xsi:schemaLocation="
  10. http://www.springframework.org/schema/beans
  11. http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
  12. http://www.springframework.org/schema/aop
  13. http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
  14. http://www.springframework.org/schema/tx
  15. http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
  16. http://www.springframework.org/schema/context
  17. http://www.springframework.org/schema/context/spring-context-3.0.xsd
  18. http://www.springframework.org/schema/util
  19. http://www.springframework.org/schema/util/spring-util.xsd
  20. ">
  21. <!-- 使用context标签,引入外部文件,外部配置 -->
  22. <context:property-placeholder
  23. location="classpath:jdbc.properties" />
  24. <bean id="dataSource"
  25. class="com.alibaba.druid.pool.DruidDataSource">
  26. <!-- 获取properties文件中内容,根据key获取,使用spring表达式"${}"获取值,进行数据库链接配置 -->
  27. <!-- 驱动名称 com.mysql.jdbc.Driver -->
  28. <property name="driverClassName" value="${prop.driverClass}"></property>
  29. <!-- 地址 jdbc:mysql://localhost:3306/user -->
  30. <property name="url" value="${prop.url}"></property>
  31. <!-- 链接数据库用户名 -->
  32. <property name="username" value="${prop.username}"></property>
  33. <!-- 链接数据库用户密码 -->
  34. <property name="password" value="${prop.password}"></property>
  35. </bean>
  36. </beans>

单元测试:
image.png github:https://github.com/My-Jun/spring5_demo1/tree/master