xml文件对bean的管理操作,FactoryBean,Bean的生命周期,xml 自动装配,外部文件的引入,spring API(二)

IOC 操作 Bean 管理(xml 注入集合属性)

1、注入数组类型属性
2、注入 List 集合类型属性
3、注入 Map 集合类型属性
(1)创建类,定义数组、list、map、set 类型属性,生成对应 set 方法

package collection;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Set;

public class Stu {
    public void setCourses(String[] courses) {
        this.courses = courses;
    }

    private String[] courses;

    private List<String> list;

    public void setList(List<String> list) {
        this.list = list;
    }

    public void setMaps(Map<String, String> maps) {
        this.maps = maps;
    }

    private Map<String,String> maps;

    public void setSets(Set<String> sets) {
        this.sets = sets;
    }

    private Set<String> sets;

    public void test(){
        System.out.println(Arrays.toString(courses));
        System.out.println(list);
        System.out.println(maps);
        System.out.println(sets);
    }

}

配置类的编写

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
      <bean id="stu" class="collection.Stu">
<!--          数据类型的注入-->
          <property name="courses">
              <array>
                  <value>java课程</value>
                  <value>数据结构</value>

              </array>
          </property>

          <property name="list">
              <list>
                  <value>张三</value>
                  <value>小三</value>
              </list>
          </property>

          <property name="maps">
              <map>
                  <entry key="Java" value="java"></entry>
                  <entry key="PHP" value="php"></entry>
              </map>
          </property>

          <property name="sets">
              <set>
                  <value>MySQL</value>
                  <value>Redis</value>
              </set>
          </property>
      </bean>
</beans>

测试类的编写,执行

 @Test
    public void test6(){
        ApplicationContext context=new ClassPathXmlApplicationContext("bean4.xml");
        Stu stu=context.getBean("stu",Stu.class);
        stu.test();
    }

在这里插入图片描述
接下来对xml对集合的细化
创建一个实体类:

package collection;

public class Course {
    public void setCname(String cname) {
        this.cname = cname;
    }

    @Override
    public String toString() {
        return "Course{" +
                "cname='" + cname + '\'' +
                '}';
    }

    private String cname;

}

在Stu类添加,因为List集合里面是一个对象,并不是字符串,所以对于xml配置文件要使用ref

    public void setCourseList(List<Course> courseList) {
        this.courseList = courseList;
    }

    private List<Course> courseList;

<!--          注入list集合类型,值是对象-->
          <property name="courseList">
              <list>
<!--                  对象使用ref-->
                  <ref bean="course1"></ref>
                  <ref bean="course2"></ref>
              </list>
          </property>
      </bean>

<!--    创建多个cource对象-->
    <bean id="course1" class="collection.Course">
        <property name="cname" value="spring5框架"></property>
    </bean>

    <bean id="course2" class="collection.Course">
        <property name="cname" value="Mybatis框架"></property>
    </bean>

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

(1)在 spring 配置文件中引入名称空间 util,使用 util 标签完成 list 集合注入提取

<?xml version="1.0" encoding="UTF-8"?>
<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"
       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">

<!--    提取list集合类型属性的注入-->
    <util:list id="bookList">
        <value>三国演义</value>
        <value>西游记</value>
        <value>水浒传</value>
    </util:list>

<!--    提取list集合类型属性注入使用-->
    <bean id="book" class="collection.Book">
        <property name="list" ref="bookList">

        </property>
    </bean>
</beans>

Book类:

package collection;

import java.util.List;

public class Book {
    private List<String>list;

   public void test(){
       System.out.println(list);
   }

    public void setList(List<String> list) {
        this.list = list;
    }
}

测试类的编写

 @Test
    public void test7(){
        ApplicationContext context=new ClassPathXmlApplicationContext("bean5.xml");
        Book book=context.getBean("book",Book.class);
        book.test();
    }

IOC 操作 Bean 管理(FactoryBean)

1、Spring 有两种类型 bean,一种普通 bean,另外一种工厂 bean(FactoryBean) 2、普通 bean:在配置文件中定义 bean 类型就是返回类型
3、工厂 bean:在配置文件定义 bean 类型可以和返回类型不一样
第一步 创建类,让这个类作为工厂 bean,实现接口 FactoryBean
第二步 实现接口里面的方法,在实现的方法中定义返回的 bean 类型

package collection;

import org.springframework.beans.factory.FactoryBean;

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;
    }

    @Override
    public boolean isSingleton() {
        return FactoryBean.super.isSingleton();
    }
}

配置类

 @Test
    public void test2(){
        ApplicationContext context=new ClassPathXmlApplicationContext("bean5.xml");
        Course myBean=context.getBean("myBean", Course.class);
        System.out.println(myBean);
    }
}

测试类:

 @Test
    public void test2(){
        ApplicationContext context=new ClassPathXmlApplicationContext("bean5.xml");
        Course myBean=context.getBean("myBean", Course.class);
        System.out.println(myBean);
    }
}

(bean 作用域)
1、在 Spring 里面,设置创建 bean 实例是单实例还是多实例
2、在 Spring 里面,默认情况下,bean 是单实例对象
3、如何设置单实例还是多实例
(1)在 spring 配置文件 bean 标签里面有属性(scope)用于设置单实例还是多实例
(2)scope 属性值
第一个值 默认值,singleton,表示是单实例对象
第二个值 prototype,表示是多实例对象

 <bean id="book" class="collection.Book" scope="prototype">
        <property name="list" ref="bookList">

        </property>
    </bean>

(3)singleton 和 prototype 区别
第一 singleton 单实例,prototype 多实例
第二 设置 scope 值是 singleton 时候,加载 spring 配置文件时候就会创建单实例对象
设置 scope 值是 prototype 时候,不是在加载 spring 配置文件时候创建 对象,在调用
getBean 方法时候创建多实例对象

第二部分

1、生命周期

(1)从对象创建到对象销毁的过程

2、bean 生命周期

(1)通过构造器创建 bean 实例(无参数构造)
(2)为 bean 的属性设置值和对其他 bean 引用(调用 set 方法)
(3)调用 bean 的初始化的方法(需要进行配置初始化的方法)
(4)bean 可以使用了(对象获取到了)
(5)当容器关闭时候,调用 bean 的销毁的方法(需要进行配置销毁的方法)

3.接下来就是代码来说明上面这几个步骤

首先创建一个实体类

package bean;

public class Orders {


    private String oname;
    //无参构造方法
    public Orders() {
        System.out.println("第一步 执行无参构造创建bean实例");
    }

    public void setOname(String oname) {
        this.oname = oname;
        System.out.println("第二步:调用set方法设置属性值");
    }

    //创建执行的初始化的方法
    public void initMethod(){
        System.out.println("第三部:执行初始化的方法");
    }

    //创建执行的销毁的方法
    public void destroyMethod(){
        System.out.println("第五步 执行销毁的方法");
    }
}
配置文件的书写

```xml
 <bean id="orders" class="bean.Orders" init-method="initMethod" destroy-method="destroyMethod">
        <property name="oname" value="手机"></property>
    </bean>

测试类代码的书写

@Test
    public void test3(){
        ApplicationContext context=new ClassPathXmlApplicationContext("bean5.xml");
        Orders orders=context.getBean("orders",Orders.class);
        System.out.println("第四步 获取创建bean实例对象");
        System.out.println(orders);

        //手动销毁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 的销毁的方法(需要进行配置销毁的方法)

代码如下:
创建一个实体类

```java
package bean;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.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="bean.MyBeanPost"></bean>

执行测试类,测试结果如下:
在这里插入图片描述

xml自动装配

创建两个实体类,代码如下:

package bean;

public class Dept1 {
    @Override
    public String toString() {
        return "Dept1{}";
    }
}

package bean;

public class Emp1 {
    public void setDept1(Dept1 dept1) {
        this.dept1 = dept1;
    }

    @Override
    public String toString() {
        return "Emp1{" +
                "dept1=" + dept1 +
                '}';
    }

    private Dept1 dept1;

    public void test(){
        System.out.println(dept1);
    }

}

<!--    实现自动装配-->
    <bean id="emp1" class="bean.Emp1" autowire="byName">
<!--        <property name="dept1" ref="dept1"></property>-->
    </bean>
    <bean id="dept1" class="bean.Dept1"></bean>

测试类代码的编写:

@Test
    public void test4(){
        ApplicationContext context=new ClassPathXmlApplicationContext("bean6.xml");
        Emp1 emp=context.getBean("emp1", Emp1.class);
        System.out.println(emp);
    }

IOC 操作 Bean 管理(外部属性文件)

1、直接配置数据库信息
(1)配置德鲁伊连接池
(2)引入德鲁伊连接池依赖 jar 包
2、引入外部属性文件配置数据库连接池
(1)创建外部属性文件,properties 格式文件,写数据库信息

prop.driverClass=com.mysql.jdbc.Driver
prop.url=jdbc:mysql://localhost:3306/score
prop.username=root
prop.password=root

编写配置文件:

<!--    引入外部属性文件-->
    <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>


spring API
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值