• IOC具体操作(Bean管理操作)——基于xml方式


    1. IOC操作Bean管理(基于xml方式)

    1.1 基于xml方式创建对象

    (1)在Spring配置文件中使用bean标签,标签里面添加对应的属性,就可以实现对象创建

    1. <beans xmlns="http://www.springframework.org/schema/beans"
    2. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    3. xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
    4. <bean id="user" class="com.godairo.spring5.User">bean>
    5. beans>

    (2)在bean标签有很多属性,介绍常用的属性

    id属性:唯一标识
    class属性:创建对象的那个类的全路径——包类路径(com.godairo.spring5.User)


    name属性:做个了解就行,用的不多,作用是和id一样的,id的位置也可以不用id,用的是name,而它们区别就是,id里面不能加特殊符号,name可以加特殊符号,这是早期的属性,基本现在不用name,都用id

    (3)创建对象时候,默认也是执行无参构造方法。

    1.2 基于xml方式注入属性

    DI:依赖注入,就是注入属性

    这两个经常在面试中问到,问IOC和DI有什么区别?

    DI是IOC中一种具体实现,它表示注入属性,它需要在创建对象的基础之上进行完成。

    也就是说DI不能单独存在,DI需要在IOC的基础上来完成

    这样做得好处:做到了单一职责,并且提高了复用性,解耦了之后,任你如何实现,使用接口的引用调用的方法,永远不需要改变


    我们来看一下我们最原始的操作——使用set方法进行注入

    1. //第一种注入方式:使用set方法进行注入,这是最原始的操作。
    2. public class Book {
    3. private String bname;
    4. public void setBname(String bname) {
    5. this.bname = bname;
    6. }
    7. public static void main(String[] args) {
    8. Book book = new Book();
    9. book.setBname("abc");
    10. }
    11. }

    使用有参构造进行注入

    1. //第二种注入方式:通过有参构造注入
    2. public class Book {
    3. private String bname;
    4. public Book(String bname) {
    5. this.bname = bname;
    6. }
    7. public static void main(String[] args) {
    8. Book book = new Book("abc");
    9. }
    10. }

    演示一下使用set方法进行注入属性

    首先我们创建类

    1. //(1)创建类,定义属性和对应的set方法
    2. public class Book {
    3. private String bname;
    4. private String bauthor;
    5. public void setBname(String bname) {
    6. this.bname = bname;
    7. }
    8. public void setBauthor(String bauthor) {
    9. this.bauthor = bauthor;
    10. }
    11. public void testDemo(){
    12. System.out.println(bname+"::"+bauthor);
    13. }
    14. }
    1. <beans xmlns="http://www.springframework.org/schema/beans"
    2. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    3. xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
    4. <bean id="book" class="com.godairo.spring5.Book">
    5. <property name="bname" value="如何成为小可爱">property>
    6. <property name="bauthor" value="GodAiro">property>
    7. bean>
    8. beans>

    编写测试类 

    1. @Test
    2. public void testBook1(){
    3. //1. 加载Spring配置文件
    4. ApplicationContext context = new ClassPathXmlApplicationContext("bean1.xml");
    5. Book book = context.getBean("book", Book.class);
    6. book.testDemo();
    7. }

                                        

    注入属性要在创建对象基础之上完成,先创建对象再注入属性。


    接下来演示通过有参构造来注入属性 

    (1)创建类,定义属性,创建属性对应有参构造方法

    1. public class Orders {
    2. private String oname;
    3. private String address;
    4. public Orders(String oname, String address) {
    5. this.oname = oname;
    6. this.address = address;
    7. }
    8. public void print(){
    9. System.out.println(oname+"::"+address);
    10. }
    11. }

    (2)在Spring配置文件中进行配置

    1. "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. xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
    5. <bean id="orders" class="com.godairo.spring5.Orders">
    6. <constructor-arg name="oname" value="电脑">constructor-arg>
    7. <constructor-arg name="address" value="China">constructor-arg>
    8. bean>
    9. beans>

    这里要注意的是我们用有参构造进行注入的时候,

    ,在这一步写完会爆红,因为我们创建对象的时候已经不是用无参构造去new对象了,是有参构造进行取创建对象,创建的过程中还进行赋值,所以我们还要在里面加个标签,里面的name属性对应类里面的属性,value则是我们要进行赋的值。  


    1.3 xml 注入其他类型属性

    用xml注入其他类型属性,比如说可以注入有控制情况,有对象形式,集合形式。


    1、字面量

    字面量:比如说在Book类中要给属性赋初始值,我们可以在类中属性就直接赋值,或者在xml中bean标签下的property可以赋值

     在自变量中,设置值的时候,空值和特殊符号该怎么做?

    (1)null值,在property中写name属性,在这个property标签里面写个null标签就代表空值

    1. <bean id="book" class="com.godairo.spring5.Book">
    2. <property name="bname" value="如何称为小可爱">property>
    3. <property name="bauthor" value="GodAiro">property>
    4. <property name="address">
    5. <null />
    6. property>
    7. bean>

    测试一下,address的值是null

     (2)属性值包含特殊符号,我们在property标签里给value属性赋值的时候,如果包含特殊符号就会报错

    解决有2种办法
    1.对尖括号进行转义:<>
    2.用xml中的CDATE结构
      >]]> 

    在IDEA中的快捷键可以直接按CD

    执行结果

      


    2、注入属性-外部 bean

    我们在操作中有3层,外部层调用Service层,Service层调用Dao层。那么演示就写2层,一个Service层,一个Dao层,通过Service调用Dao就叫引入外部bean

    (1)创建两个类,service类和dao类

    UserDao.java

    1. public interface UserDao {
    2. public void update();
    3. }

    UserDaoImpl.java

    1. public class UserDaoImpl implements UserDao{
    2. @Override
    3. public void update() {
    4. System.out.println("dao update..............");
    5. }
    6. }

    UserService.java

    1. public class UserService {
    2. public void add(){
    3. System.out.println("service add.................");
    4. }
    5. }

    (2)在service调用dao里面的方法

        在原始的方式中,是在Service里面去new UserDaoImpl这个实现类

        private UserDao userDao = new UserDaoImpl();

    现在我们通过Spring文件进行配置

    (3)在spring配置文件中进行配置,现在我们在UserService里面生成UserDao属性,并且生成set方法,然后通过set方法可以完成注入。

    UserService.java

    1. public class UserService {
    2. private UserDao userDao;
    3. public void setUserDao(UserDao userDao) {
    4. this.userDao = userDao;
    5. }
    6. public void add(){
    7. System.out.println("service add.................");
    8. userDao.update();
    9. }
    10. }

    接下来就是在配置文件中进行配置

    bean2.xml

    1. <beans xmlns="http://www.springframework.org/schema/beans"
    2. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    3. xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
    4. <bean id="userService" class="com.godairo.service.UserService">
    5. <property name="userDao" ref="userDaoImpl">property>
    6. bean>
    7. <bean id="userDaoImpl" class="com.godairo.dao.UserDaoImpl">bean>
    8. beans>

    这里Dao对象的class我们需要填写的是实现类UserDaoImpl的类全路径。

    然后在Service中也是通过bean标签去创建对象,而注入userDao对象则是属性注入,因为我们在Service创建了Dao属性

    而property里的name就是我们的属性名,ref是创建userDao对象bean标签里的id值。  

    3、注入属性——内部bean

    (1)一对多关系:部门和员工
    一个部门可以有多个员工,一个员工属于一个部门
    部门是一,员工是多
    (2)在实体类之间表示一对多关系,员工表示所属部门,使用对象类型进行表示。

    创建一个部门类

    Dept.java

    1. public class Dept {
    2. private String dname;
    3. public void setDname(String dname) {
    4. this.dname = dname;
    5. }
    6. @Override
    7. public String toString() {
    8. return "Dept{" +
    9. "dname='" + dname + '\'' +
    10. '}';
    11. }
    12. }

    Emp.java

    1. public class Emp {
    2. private String ename;
    3. private String gender;
    4. public void setEname(String ename) {
    5. this.ename = ename;
    6. }
    7. public void setGender(String gender) {
    8. this.gender = gender;
    9. }
    10. }

    目前两个类是没有关系的,那么我想表示这个关系,怎么表示呢?

    一个部门可以有多个员工,怎么表示多个,那么就可以用集合表示,一个员工属于某一个部门,所以在员工里表示某一个部门可以用一个对象表示

    1. public class Emp {
    2. private String ename;
    3. private String gender;
    4. //员工属于某一个部门,使用对象形式表示
    5. private Dept dept;
    6. public void setDept(Dept dept) {
    7. this.dept = dept;
    8. }
    9. public void setEname(String ename) {
    10. this.ename = ename;
    11. }
    12. public void setGender(String gender) {
    13. this.gender = gender;
    14. }
    15. public void add(){
    16. System.out.println(ename+"::"+gender+"::"+dept);
    17. }
    18. }

    并且把对应的set方法也创建出来。

    (3)在Spring配置文件中进行配置

    bean3.xml

    1. <beans xmlns="http://www.springframework.org/schema/beans"
    2. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    3. xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
    4. <bean id="emp" class="com.godairo.bean.Emp">
    5. <property name="ename" value="lucy">property>
    6. <property name="gender" value="女">property>
    7. <property name="dept">
    8. <bean id="dept" class="com.godairo.bean.Dept">
    9. <property name="dname" value="安保部">property>
    10. bean>
    11. property>
    12. bean>
    13. beans>

    其实就是在设置dept这个属性值的时候没有value了,而是在property里面嵌套定义一个bean,在里面把Dept这个类给定义好

    编写测试,进行运行

    1. @Test
    2. public void testBean2(){
    3. //1. 加载Spring配置文件
    4. ApplicationContext context = new ClassPathXmlApplicationContext("bean3.xml");
    5. //2. 获取配置创建的对象
    6. Emp emp = context.getBean("emp", Emp.class);
    7. emp.add();
    8. }


    4、注入属性—————级联赋值

    (1)第一种写法

    1. <bean id="emp" class="com.godairo.bean.Emp">
    2. <property name="ename" value="lucy">property>
    3. <property name="gender" value="女">property>
    4. <property name="dept" ref="dept" >property>
    5. bean>
    6. <bean id="dept" class="com.godairo.bean.Dept">
    7. <property name="dname" value="安保部">property>
    8. bean>

    其实就是外部bean的方式

    (2)第二种写法  

    1. <bean id="emp" class="com.godairo.bean.Emp">
    2. <property name="ename" value="lucy">property>
    3. <property name="gender" value="女">property>
    4. <property name="dept" ref="dept" >property>
    5. <property name="dept.dname" value="技术部">property>
    6. bean>
    7. <bean id="dept" class="com.godairo.bean.Dept">bean>

     dept.dname中的dept是我们在Emp的这个类声明的Dept对象名

     

    dept.dname中的dname就是Dept中的dname属性值,我们要去拿这个值就是要这样写:dept.dname。

    那么我们写这一步的时候:

    ,会进行编译报错

    原因是我们声明了这个对象,但是我们需要得到它,那么就需要在Emp这个类中去建一个get这个对象的方法,否则会进行报错。  


    1.4 xml注入集合属性

    1、注入数组类型属性

    2、注入List集合类型属性

    3、注入Map集合类型属性

    (1)创建类,定义数组、list、map、set类型属性,生成对应的set方法

    1. public class Student {
    2. //1. 数组类型属性
    3. private String[] courses;
    4. //2. List集合类型属性
    5. private List list;
    6. //3. Map集合类型属性
    7. private Map maps;
    8. //4. Set集合类型属性
    9. private Set sets;
    10. public void setCourses(String[] courses) {
    11. this.courses = courses;
    12. }
    13. public void setList(List list) {
    14. this.list = list;
    15. }
    16. public void setMaps(Map maps) {
    17. this.maps = maps;
    18. }
    19. public void setSets(Set sets) {
    20. this.sets = sets;
    21. }
    22. public void test(){
    23. System.out.println(Arrays.toString(courses));
    24. System.out.println(list);
    25. System.out.println(maps);
    26. System.out.println(sets);
    27. }
    28. }

    (2)在Spring配置文件中进行配置

    1. <beans xmlns="http://www.springframework.org/schema/beans"
    2. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    3. xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
    4. <bean id="student" class="com.godairo.spring5.connectiontype.Student">
    5. <property name="courses">
    6. <list>
    7. <value>Java课程value>
    8. <value>MySQL课程value>
    9. <value>Spring5value>
    10. list>
    11. property>
    12. <property name="list">
    13. <list>
    14. <value>小红value>
    15. <value>张三value>
    16. <value>李四value>
    17. list>
    18. property>
    19. <property name="maps">
    20. <map>
    21. <entry key="JAVA" value="java">entry>
    22. <entry key="PHP" value="php">entry>
    23. map>
    24. property>
    25. <property name="sets">
    26. <set>
    27. <value>MySQLvalue>
    28. <value>Redisvalue>
    29. set>
    30. property>
    31. bean>
    32. beans>

    数组用array和list都可以去赋值,这里我就用list。

    然后编写测试类

    1. public class TestSpring5 {
    2. @Test
    3. public void testCollection(){
    4. ApplicationContext context = new ClassPathXmlApplicationContext("bean1.xml");
    5. Student student = context.getBean("student", Student.class);
    6. student.test();
    7. }
    8. }


    4、在集合里面设置对象类型的值

    先创建一个课程类

    1. public class Course {
    2. private String cname;
    3. public void setCname(String cname) {
    4. this.cname = cname;
    5. }
    6. }
    1. public class Student {
    2. //1. 数组类型属性
    3. private String[] courses;
    4. //2. List集合类型属性
    5. private List list;
    6. //3. Map集合类型属性
    7. private Map maps;
    8. //4. Set集合类型属性
    9. private Set sets;
    10. //5. 学生所学的多门课程
    11. private List courseList;
    12. public void setCourseList(List courseList) {
    13. this.courseList = courseList;
    14. }
    15. public void setCourses(String[] courses) {
    16. this.courses = courses;
    17. }
    18. public void setList(List list) {
    19. this.list = list;
    20. }
    21. public void setMaps(Map maps) {
    22. this.maps = maps;
    23. }
    24. public void setSets(Set sets) {
    25. this.sets = sets;
    26. }
    27. public void test(){
    28. System.out.println(Arrays.toString(courses));
    29. System.out.println(list);
    30. System.out.println(maps);
    31. System.out.println(sets);
    32. System.out.println(courseList);
    33. }
    34. }

    然后创建配置文件

    1. <beans xmlns="http://www.springframework.org/schema/beans"
    2. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    3. xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
    4. <bean id="student" class="com.godairo.spring5.connectiontype.Student">
    5. <property name="courseList">
    6. <list>
    7. <ref bean="course">ref>
    8. <ref bean="course2">ref>
    9. list>
    10. property>
    11. bean>
    12. <bean id="course" class="com.godairo.spring5.connectiontype.Course">
    13. <property name="cname" value="Spring5框架">property>
    14. bean>
    15. <bean id="course2" class="com.godairo.spring5.connectiontype.Course">
    16. <property name="cname" value="MyBatis框架">property>
    17. bean>
    18. beans>

    这里进行注入list集合类型,值是对象,里面不再是进行赋值,里面有个标签,有个bean属性,用来引入外部bean,我们可以在外面创建多个course对象,id值不同,这样就完成对象值的注入。

    然后在Course里面生成toString方法,在Student类的test方法中再打印出courseList

     5、把集合注入部分提取出来,作为公共部分。

    (1)在Spring配置文件中引入名称空间 util

    (2)使用util标签完成list集合注入提取

    1. public class Book {
    2. private List list;
    3. public void setList(List list) {
    4. this.list = list;
    5. }
    6. }
    1. <beans xmlns="http://www.springframework.org/schema/beans"
    2. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    3. xmlns:util="http://www.springframework.org/schema/util"
    4. xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
    5. http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd">
    6. <util:list id="bookList">
    7. <value>如何变成小可爱?value>
    8. <value>如何称为富婆?value>
    9. <value>活着value>
    10. util:list>
    11. <bean id="book" class="com.godairo.spring5.connectiontype.Book">
    12. <property name="list" ref="bookList">property>
    13. bean>
    14. beans>

    这个标签进行抽取,里面还有都有,写完之后给取个名字,也就是取个id。

    就好比我们提取个公共类,里面有公共方法,给这个方法起名字,写完之后在里面的写法就跟之前一样,写上标签可以写多个,但是如果说引入的是对象,那么就是写ref就是加个bean属性,这下就提取完了。

    然后就是开始用了,我们把Book的对象进行创建,然后在里面的在里面加个ref属性,写上bookList,这里的值对应的就是里的id值。

    编写测试类

    1. @Test
    2. public void testCollection2(){
    3. ApplicationContext context = new ClassPathXmlApplicationContext("bean2.xml");
    4. Book book = context.getBean("book", Book.class);
    5. book.test();
    6. }

    1.5 FactoryBean

    1、在Spring里面有两种类型Bean,一种是普通类型的Bean,普通Bean就是我们自己创建的          Bean,另外一种叫工厂Bean,也可以叫FactoryBean

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

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


    普通Bean,我们随便取之前写过的配置文件中找个例子

    1. <bean id="book" class="com.godairo.spring5.connectiontype.Book">
    2. <property name="list" ref="bookList">property>
    3. bean>

    在这里面,id是我们给bean取个名字,通过它可以得到bean实例,class里面是bean类的包类路径,我们最后写的是Book

    那么在获取配置文件后,去getBean返回的Bean实例就是Book

    这就叫普通bean  


    还有一种是工厂Bean,比如说我们现在定义的是Book,那返回的类型可以不是Book,可以是Dept,或者其他类型,这就是工厂bean

    那它们的区别是什么?

    普通bean我们可以看到,定义什么类型,就返回什么类型。而工厂bean的定义类型和返回类型可以是不一样的,这就是它们的不同特点

    演示工厂bea

    第一步,创建一个类,让这个类作为工厂bean,实现接口 FactoryBean

    第二步,实现接口里面的方法,在实现的方法中定义返回的bean类型

    1. public class MyBean implements FactoryBean {
    2. //定义返回bean
    3. @Override
    4. public Course getObject() throws Exception {
    5. Course course = new Course();
    6. course.setCname("abc");
    7. return course;
    8. }
    9. @Override
    10. public Class getObjectType() {
    11. return null;
    12. }
    13. @Override
    14. public boolean isSingleton() {
    15. return false;
    16. }
    17. }

    在getObject()中默认的返回类型是Object,我们的做法是加一个泛型类的写法,这里为了方便就直接把对象new一下,并且赋值abc然后把course返回,在这里就表示我们设置返回的Course对象,而不是MyBean

    bean3.xml

    1. <beans xmlns="http://www.springframework.org/schema/beans"
    2. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    3. xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
    4. <bean id="mybean" class="com.godairo.spring5.factorybean.MyBean">
    5. bean>
    6. beans>

    此时去执行测试类

    1. @Test
    2. public void test3(){
    3. ApplicationContext context = new ClassPathXmlApplicationContext("bean3.xml");
    4. MyBean mybean = context.getBean("mybean", MyBean.class);
    5. System.out.println(mybean);
    6. }

    会进行报错,类型已经不匹配了

    这时候把getBean改成Course就可以了。

    1. @Test
    2. public void test3(){
    3. ApplicationContext context = new ClassPathXmlApplicationContext("bean3.xml");
    4. Course course = context.getBean("mybean", Course.class);
    5. System.out.println(course);
    6. }

     

    这个过程中,特点就是,在配置文件中定义的返回类型跟得到的类型可以不一样,而这个类型在FactoryBean这个接口中的getBoject()中做定义,这就是定义类型和返回类型可以不一样

    1.6 bean作用域

    1、在Spring里面,可以设置创建bean实例是单实例还是多实例。
    2、在Spring里面,默认情况下,bean是单实例对象。

    之前在配置文件中一直在写这个 

    而这个配置之后通过测试类可以得到创建的对象实例,但这里强调,在Spring里,创建bean实例,bean实例可以是单例对象,也可以是多例对象

    在原有的基础上,我们进行测试一下Spring里默认是不是单实例对象

    1. <beans xmlns="http://www.springframework.org/schema/beans"
    2. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    3. xmlns:util="http://www.springframework.org/schema/util"
    4. xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
    5. http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd">
    6. <util:list id="bookList">
    7. <value>如何变成小可爱?value>
    8. <value>如何称为富婆?value>
    9. <value>活着value>
    10. util:list>
    11. <bean id="book" class="com.godairo.spring5.connectiontype.Book">
    12. <property name="list" ref="bookList">property>
    13. bean>
    14. beans>
    1. public void testCollection2(){
    2. ApplicationContext context = new ClassPathXmlApplicationContext("bean2.xml");
    3. Book book1 = context.getBean("book", Book.class);
    4. Book book2= context.getBean("book", Book.class);
    5. //book.test();
    6. System.out.println(book1);
    7. System.out.println(book2);
    8. }

    这里我们验证得到,两个地址是一样的,默认是单实例对象

    3、如何设置单实例还是多实例
    (1)在Spring配置文件bean标签里面有属性用于设置单实例还是多实例
    (2)scope属性,里面有多个,但是最常用的就2个
       第一个值:singleton,表示单实例对象,不写的话默认就是它
       第二个值:prototype,表示多实例对象 

    此时我们把这个scope属性里填prototype

    然后我们再进行测试

    可以看到两个对象的地址不一样了

    singleton和prototype区别

    (1)singleton是单实例,prototype是多实例

    (2)设置scope值是singleton时候,加载spring配置文件时候就会创建单实例对象

    设置scope的值是prototype的时候,并不是在加载Spring配置文件的时候创建对象,是在调用getBean方法的时候创建多实例对象。

    1.7 bean生命周期

    生命周期:从对象创建到对象销毁的过程

    bean的生命周期
    (1)通过构造器创建bean实例,无参构造


    (2)为bean里面的属性设置值和对其他bean的引用(调用set方法),其他bean的应用就是之前在Spring配置文件的外部bean的调用


    (3)调用bean的初始化方法(需要进行配置)


    (4)bean可以使用了(对象获取到了)


    (5)当容器在关闭时候,调用bean的销毁方法(需要自己进行配置销毁方法)

    演示bean生命周期

    首先创建一个Orders类

    1. public class Orders {
    2. private String oname;
    3. public void setOname(String oname) {
    4. this.oname = oname;
    5. System.out.println("第二步 调用set方法设置属性值");
    6. }
    7. //无参数构造
    8. public Orders() {
    9. System.out.println("第一步 执行无参数构造创建bean实例");
    10. }
    11. //创建执行的初始化方法
    12. public void initMethod(){
    13. System.out.println("第三步 执行初始化方法");
    14. }
    15. //创建执行的销毁方法
    16. public void destroyMethod(){
    17. System.out.println("第五步 执行销毁的方法");
    18. }
    19. }

    按照之前叙述的顺序:

    bean声明周期第一步,通过构造器创建bean实例,无参构造

    第二步:为bean里面的属性设置值和对其他bean的引用(调用set方法)

    第三步:调用bean的初始化方法(需要进行配置),这里我们自己创建一个方法,叫initMethod(),然后还得在Spring配置文件中这么写: init-method="initMethod"

    1. <bean id="orders" class="com.godairo.spring5.bean.Orders" init-method="initMethod">
    2. <property name="oname" value="手机">property>
    3. bean>

    第四步:对象已经获取到了

    1. ApplicationContext context = new ClassPathXmlApplicationContext("bean4.xml");
    2. Orders orders = context.getBean("orders", Orders.class);
    3. System.out.println("第四步 获取到bean实例对象");
    4. System.out.println(orders);

    第五步:调用销毁方法,这里我们还是自己创建一个方法

    1. public void destroyMethod(){
    2. System.out.println("第五步 执行销毁的方法");
    3. }

    然后在xml中这么配置:destroy-method="destroyMethod"

    1. <bean id="orders" class="com.godairo.spring5.bean.Orders" init-method="initMethod" destroy-method="destroyMethod">
    2. <property name="oname" value="手机">property>
    3. bean>

    最后我们需要手动让bean实例销毁

    1. public void testBean3(){
    2. ApplicationContext context = new ClassPathXmlApplicationContext("bean4.xml");
    3. Orders orders = context.getBean("orders", Orders.class);
    4. System.out.println("第四步 获取到bean实例对象");
    5. System.out.println(orders);
    6. //手动让bean实例销毁
    7. ((ClassPathXmlApplicationContext) context).close();
    8. }

    context.close的时候会进行强转,我们去看一下结构,在ApplicationContext这个接口中,没有close方法,我们需要找它的子接口,也就是ConfigurableApplicationContext,我们需要用它的子接口或者说是实现类,这里会进行强转

    为了更明确的看清,我们可以这么写,就不需要强转了

    1. @Test
    2. public void testBean3(){
    3. ClassPathXmlApplicationContext context= new ClassPathXmlApplicationContext("bean4.xml");
    4. Orders orders = context.getBean("orders", Orders.class);
    5. System.out.println("第四步 获取到bean实例对象");
    6. System.out.println(orders);
    7. //手动让bean实例销毁
    8. context.close();
    9. }

    1.8 Spring xml中外部属性文件

    之前做法,我们创建一个bean对象,然后在property进行属性赋值,但是如果说一个类中属性很多,那就要写很多property,如果一个类中属性很多,那么就要写很多property,这么写也很不方便,而我们需要变化的时候还需要改xml配置文件,而xml中还有其他配置,改起来也不是很方便,所以我们一般可以把一些固定的值,相关的值,放到其他文件中,然后把那个文件引入到xml中,去读取其他文件的内容。在我们之前数据库就有这种操作,一些固定值,数据库驱动,地址,用户名密码,把这些值放到properties文件中,然后在xml文件去读取内容。


    1、直接配置数据库信息

    (1)配置德鲁伊连接池,这里就直接写maven依赖

    1. <dependency>
    2. <groupId>com.alibabagroupId>
    3. <artifactId>druidartifactId>
    4. <version>1.1.9version>
    5. dependency>

    这是直接配置连接池

    1. <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
    2. <property name="driverClassName" value="com.mysql.jdbc.Driver">property>
    3. <property name="url" value="jdbc:mysql//localhost:3306/book">property>
    4. <property name="username" value="root">property>
    5. <property name="password" value="root">property>
    6. bean>

    2、通过引入外部属性文件配置数据库连接池

    (1)创建外部属性文件,properties格式文件,写上数据库信息等

    1. prop.driverClass=com.mysql.jdbc.Driver
    2. prop.url=jdbc:mysql//localhost:3306/book
    3. prop.username=root
    4. prop.password=root

    (2)把外部properties属性文件引入到spring配置文件中

    在Spring配置里先引入context名称空间

    1. <beans xmlns="http://www.springframework.org/schema/beans"
    2. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    3. xmlns:context="http://www.springframework.org/schema/context"
    4. xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
    5. http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">

    然后在spring配置文件中使用标签引入外部属性文件

    1. <context:property-placeholder location="classpath:jdbc.properties"/>

    然后开始配置连接池

    1. <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
    2. <property name="driverClassName" value="${prop.driverClass}">property>
    3. <property name="url" value="${prop.url}">property>
    4. <property name="username" value="${prop.username}">property>
    5. <property name="password" value="${prop.password}">property>
    6. bean>

    这里的value值就是properties文件里的左边的值

  • 相关阅读:
    【云原生】项目上云最佳实践
    利用Gitlab进行代码的协作开发
    自定义实现乘风破浪的小船
    JNI的简单使用(Eclipse)
    C语言 指针——函数指针
    预训练语言模型复现CPT-1&Restructure_pretrain
    cpp中this和*this区别
    软信天成:干货分享,如何实施云迁移策略!
    Mysql高级篇学习总结9:创建索引、删除索引、降序索引、隐藏索引
    【立创机械狗从0到成品PCB画图总结】
  • 原文地址:https://blog.csdn.net/qq_44706176/article/details/126108827