spring5_IOC_2

目录

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

1.1、注入数组类型属性

1.2、注入 List 集合类型属性

1.3、注入 Map 集合类型属性

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

Stu类

package com.atguigu.spring5.collectiontype;

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

public class Stu
{
    //1 数组类型属性
    private String[] courses;
    //2 list集合类型属性
    private List<String> list;
    //3 map集合类型属性
    private Map<String, String> maps;
    //4 set集合类型属性
    private Set<String> sets;

    //学生所学多门课程
    private List<Course> courseList;

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

    public void setSets(Set<String> sets)
    {
        this.sets = 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 test()
    {
        System.out.println(Arrays.toString(courses));
        System.out.println(list);
        System.out.println(maps);
        System.out.println(sets);
        System.out.println(courseList);
    }
}

(2)在 spring 配置文件进行配置

bean1.xml

<?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"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

<!--1 集合类型属性注入-->
    <bean id="stu" class="com.atguigu.spring5.collectiontype.Stu">

<!--数组类型属性注入-->
        <property name="courses">
            <array>
        <!--数组类型注入时,用<list></list><array></array>都可以-->
                <value>java课程</value>
                <value>数据库课程</value>
            </array>
        </property>



<!--list类型属性注入-->
        <property name="list">
            <list>
                <value>张三</value>
                <value>小三</value>
            </list>
        </property>


<!--map类型属性注入-->
        <property name="maps">
            <map>
                <entry key="JAVA" value="java"></entry>
                <entry key="PHP" value="php"></entry>
            </map>
        </property>


<!--set类型属性注入-->
        <property name="sets">
            <set>
                <value>MySQL</value>
                <value>Redis</value>
            </set>
        </property>


<!--注入list集合类型,值是对象-->
<!--        需要先创建多个course对象-->
        <property name="courseList">
            <list>
                <ref bean="course1"></ref>
                <ref bean="course2"></ref>
            </list>
        </property>
    </bean>

    <!--创建多个course对象-->
    <bean id="course1" class="com.atguigu.spring5.collectiontype.Course">
        <property name="cname" value="Spring5框架"></property>
    </bean>
    <bean id="course2" class="com.atguigu.spring5.collectiontype.Course">
        <property name="cname" value="MyBatis框架"></property>
    </bean>

</beans>

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

Course 类

package com.atguigu.spring5.collectiontype;

//课程类
public class Course {
    private String cname; //课程名称
    public void setCname(String cname) {
        this.cname = cname;
    }

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

bean1.xml

<?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"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

<!--1 集合类型属性注入-->
    <bean id="stu" class="com.atguigu.spring5.collectiontype.Stu">

<!--数组类型属性注入-->
        <property name="courses">
            <array>
        <!--数组类型注入时,用<list></list><array></array>都可以-->
                <value>java课程</value>
                <value>数据库课程</value>
            </array>
        </property>



<!--list类型属性注入-->
        <property name="list">
            <list>
                <value>张三</value>
                <value>小三</value>
            </list>
        </property>


<!--map类型属性注入-->
        <property name="maps">
            <map>
                <entry key="JAVA" value="java"></entry>
                <entry key="PHP" value="php"></entry>
            </map>
        </property>


<!--set类型属性注入-->
        <property name="sets">
            <set>
                <value>MySQL</value>
                <value>Redis</value>
            </set>
        </property>


<!--注入list集合类型,值是对象-->
<!--        需要先创建多个course对象-->
        <property name="courseList">
            <list>
                <ref bean="course1"></ref>
                <ref bean="course2"></ref>
            </list>
        </property>
    </bean>

    <!--创建多个course对象-->
    <bean id="course1" class="com.atguigu.spring5.collectiontype.Course">
        <property name="cname" value="Spring5框架"></property>
    </bean>
    <bean id="course2" class="com.atguigu.spring5.collectiontype.Course">
        <property name="cname" value="MyBatis框架"></property>
    </bean>

</beans>

1.5、把集合注入部分提取出来

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

bean2.xml

<?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">
<!--
将       xmlns:p="http://www.springframework.org/schema/p" 中的p改成util
将最后一行的beans改成util即可

如果有多个bean就可以,都添加公共的ref="bookList"

-->

<!--1 提取list集合类型属性注入-->
    <util:list id="bookList">
<!--
        <ref bean="course1"></ref>
        如果是对象,那么就写成这个样子
-->
        <value>易筋经</value>
        <value>九阴真经</value>
        <value>九阳神功</value>
    </util:list>

<!--2 提取list集合类型属性注入使用-->
    <bean id="book" class="com.atguigu.spring5.collectiontype.Book" scope="prototype">
        <property name="list" ref="bookList"></property>
    </bean>
</beans>

Book类

package com.atguigu.spring5.collectiontype;

import java.util.List;

public class Book
{
    private List<String> list;

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

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

test部分

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

1.6、在集合里面设置对象类型值和把集合注入部分提取出来综合应用

Stu类

package com.atguigu.spring5.pack1;

import java.util.List;

/**
 * @author shkstart
 * @create 2022-12-12 20:32
 */
public class Stu
{
    private List<Book> list1;
    private List<String> list2;

    public void setList1(List<Book> list1)
    {
        this.list1 = list1;
    }

    public void setList2(List<String> list2)
    {
        this.list2 = list2;
    }

    @Override
    public String toString()
    {
        return "Stu{" +
                "list1=" + list1 +
                ", list2=" + list2 +
                '}';
    }
}

Book类

package com.atguigu.spring5.pack1;

/**
 * @author shkstart
 * @create 2022-12-12 20:33
 */
public class Book
{
    private String name1;
    private double price;

    public void setName1(String name1)
    {
        this.name1 = name1;
    }

    public void setPrice(double price)
    {
        this.price = price;
    }

    @Override
    public String toString()
    {
        return "Book{" +
                "name='" + name1 + '\'' +
                ", price='" + price + '\'' +
                '}';
    }
}

bean1.xml

<?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: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">


    <bean id="book1" class="com.atguigu.spring5.pack1.Book">
        <property name="name1" value="三国演义"></property>
        <property name="price" value="3232"></property>
    </bean>

    <bean id="book2" class="com.atguigu.spring5.pack1.Book">
        <property name="name1" value="西游记"></property>
        <property name="price" value="3223"></property>
    </bean>



    <util:list id="list1">
        <ref bean="book1"></ref>
        <ref bean="book2"></ref>
    </util:list>

    <util:list id="list2">
        <value>wocao</value>
        <value>wocao2</value>
        <value>wocao23</value>
        <value>wocao234</value>
    </util:list>


    <bean id="stu1" class="com.atguigu.spring5.pack1.Stu">
        <property name="list1" ref="list1"></property>
        <property name="list2" ref="list2"></property>
    </bean>





</beans>

test部分

package com.atguigu.spring5.pack1.HhTest;

import com.atguigu.spring5.pack1.Stu;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * @author shkstart
 * @create 2022-12-12 20:51
 */
public class CeShi
{
    @Test
    public void test1()
    {
        ApplicationContext context=
                new ClassPathXmlApplicationContext("bean1.xml");

        Stu stu1 = context.getBean("stu1", Stu.class);
        System.out.println(stu1);
    }
}

2、 IOC 操作 Bean 管理(FactoryBean)

2.1、Spring 有两种类型 bean,一种普通 bean,另外一种工厂 bean(FactoryBean)

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

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

第一步 创建类,让这个类作为工厂 bean,实现接口 FactoryBean
第二步 实现接口里面的方法,在实现的方法中定义返回的 bean 类型

MyBean 类

package com.atguigu.spring5.factorybean;

import com.atguigu.spring5.collectiontype.Course;
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 false;
    }
}

Course 类

package com.atguigu.spring5.collectiontype;

//课程类
public class Course {
    private String cname; //课程名称
    public void setCname(String cname) {
        this.cname = cname;
    }

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

bean3.xml

<?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">

    <bean id="myBean" class="com.atguigu.spring5.factorybean.MyBean">
    </bean>
</beans>

test部分

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

3、IOC 操作 Bean 管理(bean 作用域)

3.1、在 Spring 里面,设置创建 bean 实例是单实例还是多实例

3.2、在 Spring 里面,默认情况下,bean 是单实例对象

Book类

package com.atguigu.spring5.collectiontype;

import java.util.List;

public class Book
{
    private List<String> list;

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

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

bean2.xml

<?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">
<!--
将       xmlns:p="http://www.springframework.org/schema/p" 中的p改成util
将最后一行的beans改成util即可

如果有多个bean就可以,都添加公共的ref="bookList"

-->

<!--1 提取list集合类型属性注入-->
    <util:list id="bookList">
<!--
        <ref bean="course1"></ref>
        如果是对象,那么就写成这个样子
-->
        <value>易筋经</value>
        <value>九阴真经</value>
        <value>九阳神功</value>
    </util:list>

<!--2 提取list集合类型属性注入使用-->
    <bean id="book" class="com.atguigu.spring5.collectiontype.Book" scope="prototype">
        <property name="list" ref="bookList"></property>
    </bean>
</beans>

test部分

    public void testCollection2() {
//        验证是单实例还是多实例:将对象多次输出,比较他们的地址:
//        单实例:只有一个对象,下边两个实例的地址应该是一样的
//        多实例:每次都建一个新对象,下边两个实例的地址应该是不同的
        ApplicationContext context =
                new ClassPathXmlApplicationContext("bean2.xml");
        Book book1 = context.getBean("book", Book.class);
        Book book2 = context.getBean("book", Book.class);
       // book.test();
        System.out.println(book1);
        System.out.println(book2);
    }

在这里插入图片描述

3.3、如何设置单实例还是多实例

1)在 spring 配置文件 bean 标签里面有属性(scope)用于设置单实例还是多实例
(2)scope 属性值
第一个值 默认值,singleton,表示是单实例对象
第二个值 prototype,表示是多实例对象

bean2.xml

<?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">
<!--
将       xmlns:p="http://www.springframework.org/schema/p" 中的p改成util
将最后一行的beans改成util即可

如果有多个bean就可以,都添加公共的ref="bookList"

-->

<!--1 提取list集合类型属性注入-->
    <util:list id="bookList">
<!--
        <ref bean="course1"></ref>
        如果是对象,那么就写成这个样子
-->
        <value>易筋经</value>
        <value>九阴真经</value>
        <value>九阳神功</value>
    </util:list>

<!--2 提取list集合类型属性注入使用-->
    <bean id="book" class="com.atguigu.spring5.collectiontype.Book" scope="prototype">
        <property name="list" ref="bookList"></property>
    </bean>
</beans>

在这里插入图片描述

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

4、IOC 操作 Bean 管理(bean 生命周期)

4.1、生命周期

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

4.2、bean 生命周期

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

4.3、演示 bean 生命周期

Orders类

package com.atguigu.spring5.bean;

public class Orders
{

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

    private String oname;

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

//创建执行的初始化的方法(这个方法默认肯定不会执行,因此我们要在bean标签中进行配置)
//    s1定义一个初始化的方法;s2并在bean.xml中设置
    public void initMethod()
    {
        System.out.println("第三步 执行初始化的方法");
    }

//    第四步获取对象,在测试方法中进行



//创建执行的销毁的方法(s1定义一个销毁的方法;s2并在bean.xml中设置;s3在测试中手动调用销毁方法)
    public void destroyMethod()
    {
        System.out.println("第五步 执行销毁的方法");
    }
}

bean4.xml

<?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">

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

    <!--配置后置处理器-->
    <bean id="myBeanPost" class="com.atguigu.spring5.bean.MyBeanPost"></bean>
<!--    过程:当加载配置文件的时候,会将各个bean进行创建,以及这个后置处理器进行创建,
(myBeanPost实现了哪个接口,所以spring就将它作为后置处理器进行执行)
后置处理器会对当前配置文件中的所有bean实例都添加后置处理器

-->
</beans>

test部分

    public void testBean3() {
//ApplicationContext接口中没有下边的close方法,需要用她的子接口的实现类ClassPathXmlApplicationContext


//        ApplicationContext context =
//                new ClassPathXmlApplicationContext("bean4.xml");
        ClassPathXmlApplicationContext context =
                new ClassPathXmlApplicationContext("bean4.xml");
        Orders orders = context.getBean("orders", Orders.class);
        System.out.println("第四步 获取创建bean实例对象");
        System.out.println(orders);

//手动让bean实例销毁(当执行close方法,就会把bean销毁,会自动调用order中的destroyMethod方法。
        // (如果上边是用ApplicationContext接口,下边context会自动进行强转成ClassPathXmlApplicationContext,
        //因为ApplicationContext中没有close方法,要想用到子接口实现类中的独有的方法就要进行强转
        // 另一种方法是:可以直接定义成ClassPathXmlApplicationContext类型的context)
        context.close();
    }

运行结果

在这里插入图片描述

4.4、bean 的后置处理器,bean 生命周期有七步

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

4.5、演示添加后置处理器效果

(1)创建类,实现接口 BeanPostProcessor,创建后置处理器

MyBeanPost 类

package com.atguigu.spring5.bean;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.lang.Nullable;
import org.springframework.util.SocketUtils;

public class MyBeanPost implements BeanPostProcessor {

/*
创建好这个类后要在bean中进行配置,
* */

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

bean4.xml

<?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">

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

    <!--配置后置处理器-->
    <bean id="myBeanPost" class="com.atguigu.spring5.bean.MyBeanPost"></bean>
<!--    过程:当加载配置文件的时候,会将各个bean进行创建,以及这个后置处理器进行创建,
(myBeanPost实现了哪个接口,所以spring就将它作为后置处理器进行执行)
后置处理器会对当前配置文件中的所有bean实例都添加后置处理器

-->
</beans>

test部分

    public void testBean3() {
//ApplicationContext接口中没有下边的close方法,需要用她的子接口的实现类ClassPathXmlApplicationContext


//        ApplicationContext context =
//                new ClassPathXmlApplicationContext("bean4.xml");
        ClassPathXmlApplicationContext context =
                new ClassPathXmlApplicationContext("bean4.xml");
        Orders orders = context.getBean("orders", Orders.class);
        System.out.println("第四步 获取创建bean实例对象");
        System.out.println(orders);

//手动让bean实例销毁(当执行close方法,就会把bean销毁,会自动调用order中的destroyMethod方法。
        // (如果上边是用ApplicationContext接口,下边context会自动进行强转成ClassPathXmlApplicationContext,
        //因为ApplicationContext中没有close方法,要想用到子接口实现类中的独有的方法就要进行强转
        // 另一种方法是:可以直接定义成ClassPathXmlApplicationContext类型的context)
        context.close();
    }

结果

在这里插入图片描述

5、IOC 操作 Bean 管理(xml 自动装配)

5.1、1、什么是自动装配

(1)根据指定装配规则(属性名称或者属性类型),Spring 自动将匹配的属性值进行注入

5.2、演示自动装配过程

Emp 类

package com.atguigu.spring5.autowire;

public class Emp {
    private Dept dept;
    public void setDept(Dept dept) {
        this.dept = dept;
    }

    @Override
    public String toString() {
        return "Emp{" +
                "dept=" + dept +
                '}';
    }

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

Dept 类

package com.atguigu.spring5.autowire;

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

(1)根据属性名称自动注入

bean5.xml

<?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">

    <!--实现自动装配
        1、bean标签属性autowire,配置自动装配
       2、 autowire属性常用两个值:
            2.1、byName根据属性名称注入 ,要求:注入值bean的id值(dept)和类属性名称(dept)一样
            2.2、byType根据属性类型注入,此时相同类型的bean不能定义多个,否则他不知道是哪个bean而报错

        一般基于xml自动装配用的很少,一般为了方便用注解方式进行装配
    -->
    <bean id="emp" class="com.atguigu.spring5.autowire.Emp" autowire="byName">
        <!--  原来是这样写:   <property name="dept" ref="dept"></property>-->
    </bean>
    <bean id="dept" class="com.atguigu.spring5.autowire.Dept"></bean>

</beans>

(2)根据属性类型自动注入

bean5.xml

<?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">

    <!--实现自动装配
        1、bean标签属性autowire,配置自动装配
       2、 autowire属性常用两个值:
            2.1、byName根据属性名称注入 ,要求:注入值bean的id值(dept)和类属性名称(dept)一样
            2.2、byType根据属性类型注入,此时相同类型的bean不能定义多个,否则他不知道是哪个bean而报错

        一般基于xml自动装配用的很少,一般为了方便用注解方式进行装配
    -->
    <bean id="emp" class="com.atguigu.spring5.autowire.Emp" autowire="byType">
        <!--  原来是这样写:   <property name="dept" ref="dept"></property>-->
    </bean>
    <bean id="dept" class="com.atguigu.spring5.autowire.Dept"></bean>

</beans>

test部分

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

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

6.1、直接配置数据库信息

(1)配置德鲁伊连接池
(2)引入德鲁伊连接池依赖 jar 包

复制
在这里插入图片描述

粘贴到lib目录下
在这里插入图片描述

复制后引入到当前的项目中来
在这里插入图片描述

点击蓝色框框在这里插入图片描述

点击蓝色框框(将连接池加入到里边来)
在这里插入图片描述

直接配置连接池

    <!--方式1  直接配置连接池-->
    <!--<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>-->

    <!--
            driverClassName:驱动名称
            url:数据库地址
            username:连接数据库的用户名
            password:连接数据库的密码
    -->

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

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

在这里插入图片描述

jdbc.properties文件

prop.driverClass=com.mysql.jdbc.Driver
prop.url=jdbc:mysql://localhost:3306/userDb
prop.userName=root
prop.password=root

#注意等号左边可以随便写,写一个单词容易冲突,所以如上
#这里边是 key=value 的结构

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

step1 引入 context 名称空间

<?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"
       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/util http://www.springframework.org/schema/util/spring-util.xsd
                           http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">

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

bean6.xml

<?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"
       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/util http://www.springframework.org/schema/util/spring-util.xsd
                           http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">


    <!--方式1  直接配置连接池-->
    <!--<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>-->

    <!--
            driverClassName:驱动名称
            url:数据库地址
            username:连接数据库的用户名
            password:连接数据库的密码
    -->




    <!--方式2  引入外部属性文件-->
    <context:property-placeholder location="classpath:jdbc.properties"/>

    <!--配置连接池-->
<!--    注意:value是加表达式  ${}  引入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>

</beans>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值