1. 程式人生 > >Spring --03.Spring配置檔案

Spring --03.Spring配置檔案

1、約束

Spring核心配置檔案約束可以在doc文件中查詢到、約束 如下

<?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="..." class="...">
        <!-- collaborators and configuration for this bean go here -->
    </bean>

    <bean id="..." class="...">
        <!-- collaborators and configuration for this bean go here -->
    </bean>

    <!-- more bean definitions go here -->

</beans>

2、id屬性

id屬性是bean的唯一標識

3、class屬性

class為Bean類的全路徑名、當我們在測試類中找到上面id、在從這個id找到class對應的類、從而建立物件

4、scope屬性

Scope屬性代表Bean的作用範圍

singleton:單例(預設值)

prototype:多例, 在Spring框架整合Struts2框架的時候,Action類也需要交給Spring做管理,配置把Action類配置成多例!!

request:應用在web工程中 ,將建立的物件存入到request域中。

session:應用在web工程中, 將建立的物件存入到session域中。

globalsession:應用在porlet環境下使用。將建立的物件存入到全域性的session中。

修改applicationContext.xml 將UserDaoImpl的作用域改為prototype

<?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="userDaos" class="com.day01.dao.Impl.UserDaoImpl" scope="prototype"></bean>
</beans>

測試類TestIoc.java

package com.day01.test;

import com.day01.dao.UserDao;
import org.junit.Test;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;
import org.springframework.core.io.ClassPathResource;

/**
 * @ Author     :ShaoWei Sun.
 * @ Date       :Created in 19:30 2018/11/7
 */
public class TestIoC {

    @Test
    public void test4(){
        ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
        UserDao userDao = (UserDao) ac.getBean("userDaos");
        UserDao userDao1 = (UserDao) ac.getBean("userDaos");
        System.out.println(userDao==userDao1);
    }
}

5、init-method屬性

當bean被載入到容器的時候呼叫init-method屬性指定的方法

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

<bean id="userDaos" class="com.day01.dao.Impl.UserDaoImpl" init-method="init"></bean>
</beans>

UserDaoImpl.java提供init-method對應的init方法

package com.day01.dao.Impl;

import com.day01.dao.UserDao;

/**
 * @ Author     :ShaoWei Sun.
 * @ Date       :Created in 19:28 2018/11/7
 */
public class UserDaoImpl implements UserDao {
    public UserDaoImpl(){
        System.out.println("呼叫了無參構造方法。。。");
    }

    public void init(){
        System.out.println("呼叫了init方法。。。。。");
    }
    @Override
    public void save() {
        System.out.println("持久層。。。。操作");
    }
}

測試類TestIoc.java

package com.day01.test;

import com.day01.dao.UserDao;
import org.junit.Test;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;
import org.springframework.core.io.ClassPathResource;

/**
 * @ Author     :ShaoWei Sun.
 * @ Date       :Created in 19:30 2018/11/7
 */
public class TestIoC {

    /**
     * 建立ioc容器時就已把物件建立好了
     */
    @Test
    public void test1(){
        //建立Spring工廠(建立 IOC容器)
        ApplicationContext ac  = new ClassPathXmlApplicationContext("applicationContext.xml");
        System.out.println("--------------");
        UserDao userDao = (UserDao) ac.getBean("userDaos");
        userDao.save();
    }
}

6、destroy-method屬性

當bean從容器中刪除的時候呼叫destroy-method屬性指定的方法

使用條件:

1)scope=singleton 有效

2)web容器中會自動呼叫,但是main函式或測試用例需要手動呼叫(需要使用ClassPathXmlApplicationContext的close()方法) 

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

<bean id="userDaos" class="com.day01.dao.Impl.UserDaoImpl" scope="singleton" init-method="init" destroy-method="destroy"></bean>
</beans>

UserDaoImpl.java

package com.day01.dao.Impl;

import com.day01.dao.UserDao;

/**
 * @ Author     :ShaoWei Sun.
 * @ Date       :Created in 19:28 2018/11/7
 */
public class UserDaoImpl implements UserDao {
    public UserDaoImpl(){
        System.out.println("呼叫了無參構造方法。。。");
    }

    public void init(){
        System.out.println("呼叫了init方法。。。。");
    }
    @Override
    public void save() {
        System.out.println("持久層。。。。操作");
    }

    public void destroy(){
        System.out.println("呼叫了銷燬方法.....");
    }
}

測試類TestIoc.java

package com.day01.test;

import com.day01.dao.UserDao;
import org.junit.Test;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;
import org.springframework.core.io.ClassPathResource;

/**
 * @ Author     :ShaoWei Sun.
 * @ Date       :Created in 19:30 2018/11/7
 */
public class TestIoC {

    /**
     * 建立ioc容器時就已把物件建立好了
     */
    @Test
    public void test1(){
        //建立Spring工廠(建立 IOC容器)
        ApplicationContext ac  = new ClassPathXmlApplicationContext("applicationContext.xml");
        System.out.println("--------------");
        UserDao userDao = (UserDao) ac.getBean("userDaos");
        userDao.save();
        ((ClassPathXmlApplicationContext) ac).close();
    }
}