1. 程式人生 > 實用技巧 >Spring IOC(二)

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>