Spring IOC(二)

Spring IOC(二)

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

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

    @Override
    public String toString() {
        return "Stu{" +
                "courses=" + Arrays.toString(courses) +
                ", list=" + list +
                ", maps=" + maps +
                ", sets=" + sets +
                '}';
    }
}
<!--1 集合类型属性注入-->
<bean id="stu" class="cn.xupengzhuang.spring5.pojo.Stu">
    <!--数组类型属性注入-->
    <property name="courses">
        <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>
</bean>

把集合注入部分提取出来

在 spring 配置文件中引入名称空间 util

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

    <util:list id="bookList">
        <value>易筋经</value>
        <value>九阴真经</value>
        <value>九阳神功</value>
    </util:list>
    <!--2 提取 list 集合类型属性注入使用-->
    <bean id="books" class="cn.xupengzhuang.spring5.pojo.Books">
        <property name="bookList" ref="bookList"></property>
    </bean>

</beans>

IOC操作Bean管理(FactoryBean)

Spring 有两种类型 bean,一种普通 bean,另外一种工厂 bean(FactoryBean)。 普通 bean:在配置文件中定义 bean 类型就是返回类型。 工厂 bean:在配置文件定义 bean 类型可以和返回类型不一样。

测试代码

public class MyBean implements FactoryBean<Course> {
    @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;
    }
}
<bean id="myBean" class="cn.xupengzhuang.spring5.pojo.MyBean"></bean>
//1 加载 spring 配置文件
ApplicationContext context =
        new ClassPathXmlApplicationContext("bean4.xml");
//2 获取配置创建的对象
Course course = context.getBean("myBean", Course.class);
System.out.println(course);

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

什么是自动装配

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

autowire 属性常用两个值:

  • byName 根据属性名称注入 ,注入值 bean 的 id 值和类属性名称一样
  • byType 根据属性类型注入

代码演示

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

<bean id="emp" class="cn.xupengzhuang.spring5.pojo.Emp" autowire="byName">
    <property name="ename" value="张三"></property>
    <property name="gender" value="男"></property>
</bean>

<bean id="dept" class="cn.xupengzhuang.spring5.pojo.Dept">
    <property name="dname" value="技术部"></property>
</bean>

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

<bean id="emp" class="cn.xupengzhuang.spring5.pojo.Emp" autowire="byType">
    <property name="ename" value="张三"></property>
    <property name="gender" value="男"></property>
</bean>

<bean id="dept" class="cn.xupengzhuang.spring5.pojo.Dept">
    <property name="dname" value="技术部"></property>
</bean>
@Test
public void testAutowire(){
    //1 加载 spring 配置文件
    ClassPathXmlApplicationContext context =
            new ClassPathXmlApplicationContext("bean6.xml");
    //2 获取配置创建的对象
    Emp emp = context.getBean("emp", Emp.class);
    System.out.println(emp);
}

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

jdbc.properties

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

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

    <!--引入外部属性文件-->
    <context:property-placeholder location="classpath:jdbc.properties"/>
    <!--配置连接池-->
    <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
        <property name="driverClassName" value="${jdbc.driverClass}"></property>
        <property name="url" value="${jdbc.url}"></property>
        <property name="username" value="${jdbc.username}"></property>
        <property name="password" value="${jdbc.password}"></property>
    </bean>

</beans>
原文地址:https://www.cnblogs.com/beanbag/p/13412379.html