1. 程式人生 > 實用技巧 >spring基礎回顧

spring基礎回顧

一、spring專案構建

使用idea建立專案

二、Sping IOC容器

1、簡單例項

package entity;

/**
 * @Author LLF
 * @Date Created in 2020/12/7 10:04
 * @Description
 * @Version 1.0
 **/
public class Phone {

    private String name;
    private String money;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        
this.name = name; } public String getMoney() { return money; } public void setMoney(String money) { this.money = money; } public void init(){ System.out.println("初始化"); } public void destory(){ System.out.println("銷燬"); } }
<?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="Phone" class="entity.Phone"> <property name="name" value="IP12"></property> <property name="money" value="6799軟妹幣"></property> </bean> </beans>
package test;

import entity.Phone;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.support.BeanDefinitionReader;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.beans.factory.xml.XmlBeanDefinitionReader;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;

/**
 * @Author LLF
 * @Date Created in 2020/12/7 10:08
 * @Description
 * @Version 1.0
 **/
public class Main {

    public static void main(String[] args) {
//        BeanFactory
        Resource resource = new ClassPathResource("config/spring.xml");
        BeanFactory factory = new DefaultListableBeanFactory();
        BeanDefinitionReader beanDefinitionReader = new XmlBeanDefinitionReader((BeanDefinitionRegistry) factory);
        beanDefinitionReader.loadBeanDefinitions(resource);
        Phone phone = (Phone) factory.getBean("Phone");
        System.out.println(phone.getName()+"售價為:"+phone.getMoney());

    }
}

執行:

2、BeanFactory和ApplicationContext

Spring ApplicationContext 容器

Application Context 是 BeanFactory的子介面,也被稱為 Spring 上下文。

Application Context 是 spring 中較高階的容器。和 BeanFactory 類似,它可以載入配置檔案中定義的 bean,將所有的 bean 集中在一起,當有請求的時候分配 bean。 另外,它增加了企業所需要的功能,比如,從屬性檔案中解析文字資訊和將事件傳遞給所指定的監聽器。這個容器在 org.springframework.context.ApplicationContext interface 介面中定義。

ApplicationContext 包含 BeanFactory 所有的功能,一般情況下,相對於 BeanFactory,ApplicationContext 會更加優秀。當然,BeanFactory 仍可以在輕量級應用中使用,比如移動裝置或者基於 applet 的應用程式。

最常被使用的 ApplicationContext 介面實現:

  • FileSystemXmlApplicationContext:該容器從 XML 檔案中載入已被定義的 bean。在這裡,你需要提供給構造器 XML 檔案的完整路徑。

  • ClassPathXmlApplicationContext:該容器從 XML 檔案中載入已被定義的 bean。在這裡,你不需要提供 XML 檔案的完整路徑,只需正確配置 CLASSPATH 環境變數即可,因為,容器會從 CLASSPATH 中搜索 bean 配置檔案。

  • WebXmlApplicationContext:該容器會在一個 web 應用程式的範圍內載入在 XML 檔案中已被定義的 bean。
//        BeanFactory
//        Resource resource = new ClassPathResource("config/spring.xml");
//        BeanFactory factory = new DefaultListableBeanFactory();
//        BeanDefinitionReader beanDefinitionReader = new XmlBeanDefinitionReader((BeanDefinitionRegistry) factory);
//        beanDefinitionReader.loadBeanDefinitions(resource);
//        Phone phone = (Phone) factory.getBean("Phone");
//        System.out.println(phone.getName()+"售價為:"+phone.getMoney());

//        ApplicationContext
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("config/spring.xml");
        Phone phone = (Phone) applicationContext.getBean("Phone");
        System.out.println(phone.getName()+"售價為:"+phone.getMoney());

3、Bean的作用域、生命週期、後置處理器、定義繼承。

作用域

singleton
在spring IoC容器僅存在一個Bean例項,Bean以單例方式存在,預設值-->
prototype 每次從容器中呼叫Bean時,都返回一個新的例項,即每次呼叫getBean()時,相當於執行newXxxBean()-->
簡單的說:
singleton 只有一個例項,也即是單例模式。
prototype訪問一次建立一個例項,相當於new。
應用場合:
需要回收重要資源(資料庫連線等)的事宜配置為singleton,如果配置為prototype需要應用確保資源正常回收。
2.有狀態的Bean配置成singleton會引發未知問題,可以考慮配置為prototype。
struts2的action交由spring管理的時候,spring預設是singleton的,而struts2的action顯然是有狀 態的,所以必須顯示設定為scope="prototype",prototype為原型模式,每次action請求過來都會建立一個action但是對 那些Dao的實現類推介scope="singleton" ,因為這些類沒有狀態,用singleton只需維護一個例項,顯然效能高一些

生命週期:

Phone類新增init和destory方法

package entity;

/**
 * @Author LLF
 * @Date Created in 2020/12/7 10:04
 * @Description
 * @Version 1.0
 **/
public class Phone {

    private String name;
    private String money;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getMoney() {
        return money;
    }

    public void setMoney(String money) {
        this.money = money;
    }

    public void init(){
        System.out.println("初始化");
    }

    public void destory(){
        System.out.println("銷燬");
    }
}

    <bean id="Phone" class="entity.Phone" scope="singleton" init-method="init" destroy-method="destory">
        <property name="name" value="IP12"></property>
        <property name="money" value="6799軟妹幣"></property>
    </bean>
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("config/spring.xml");
        Phone phone = (Phone) applicationContext.getBean("Phone");
        System.out.println(phone.getName()+"售價為:"+phone.getMoney());
//        關閉hook執行銷燬方法
        ((ClassPathXmlApplicationContext) applicationContext).registerShutdownHook();

執行:

後置處理器:

新增MyPostProcess類

package entity;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;

/**
 * @Author LLF
 * @Date Created in 2020/12/7 11:17
 * @Description
 * @Version 1.0
 **/
public class MyPostProcess implements BeanPostProcessor {

    public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
        System.out.println("前置 : " + beanName);
        return bean;  // you can return any other object as well
    }
    public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
        System.out.println("後置 : " + beanName);
        return bean;  // you can return any other object as well
    }

}

bean新增

    <bean class="entity.MyPostProcess"></bean>

執行:

定義繼承

新增Phone2。

package entity;

/**
 * @Author LLF
 * @Date Created in 2020/12/7 11:49
 * @Description
 * @Version 1.0
 **/
public class Phone2 {

    private String name;
    private String money;
    private String city;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getMoney() {
        return money;
    }

    public void setMoney(String money) {
        this.money = money;
    }

    public String getCity() {
        return city;
    }

    public void setCity(String city) {
        this.city = city;
    }
}

bean配置

<?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="Phone" class="entity.Phone" scope="singleton" abstract="true">
        <property name="name" value="IP12"></property>
        <property name="money" value="6799軟妹幣"></property>
    </bean>

    <bean id="Phone2" class="entity.Phone2" parent="Phone">
        <property name="city" value="洛杉磯"></property>
    </bean>

    <bean class="entity.MyPostProcess"></bean>
</beans>
package test;

import entity.Phone;
import entity.Phone2;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.config.DeprecatedBeanWarner;
import org.springframework.beans.factory.support.BeanDefinitionReader;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.beans.factory.xml.XmlBeanDefinitionReader;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;

/**
 * @Author LLF
 * @Date Created in 2020/12/7 10:08
 * @Description
 * @Version 1.0
 **/
public class Main {

    public static void main(String[] args) {
//        BeanFactory
//        Resource resource = new ClassPathResource("config/spring.xml");
//        BeanFactory factory = new DefaultListableBeanFactory();
//        BeanDefinitionReader beanDefinitionReader = new XmlBeanDefinitionReader((BeanDefinitionRegistry) factory);
//        beanDefinitionReader.loadBeanDefinitions(resource);
//        Phone phone = (Phone) factory.getBean("Phone");
//        System.out.println(phone.getName()+"售價為:"+phone.getMoney());

//        ApplicationContext
//        Application Context 是 BeanFactory 的子介面,也被稱為 Spring 上下文。
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("config/spring.xml");
        Phone2 phone = (Phone2) applicationContext.getBean("Phone2");
        System.out.println(phone.getName()+"售價為:"+phone.getMoney());
        System.out.println(phone.getCity()+"產地為:"+phone.getCity());
//        關閉hook執行銷燬方法
        ((ClassPathXmlApplicationContext) applicationContext).registerShutdownHook();

    }
}

執行:

三、依賴注入

1、建構函式依賴注入

package di;

/**
 * @Author LLF
 * @Date Created in 2020/12/7 17:06
 * @Description
 * @Version 1.0
 **/
public class Text {

    public void textm(){
        System.out.println("Text的方法___________11111111111");
    }
}
package di;

/**
 * @Author LLF
 * @Date Created in 2020/12/7 16:52
 * @Description
 * @Version 1.0
 **/
public class Text1 {

    public void textm1(){
        System.out.println("Text1方法_____________111111");
    }

    public void textm2(){
        System.out.println("Text1方法_____________22222222");
    }
}
package di;

/**
 * @Author LLF
 * @Date Created in 2020/12/7 16:54
 * @Description
 * @Version 1.0
 **/
public class Text2 {

    private Text1 text1;

    private Text text;

    public Text2(Text1 text1,Text text){
        this.text1 = text1;
        this.text = text;
    }

    public void text2m1(){
        text.textm();
        text1.textm1();
        text1.textm2();
    }
}

bean配置:

    <bean id="Text" class="di.Text">
    </bean>

    <bean id="Text1" class="di.Text1">
    </bean>

    <bean id="Text2" class="di.Text2">
        <constructor-arg ref="Text1"></constructor-arg>
        <constructor-arg ref="Text"></constructor-arg>
    </bean>
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("config/spring.xml");
        Text2 text2 = (Text2) applicationContext.getBean("Text2");
        text2.text2m1();
//        關閉hook執行銷燬方法
        ((ClassPathXmlApplicationContext) applicationContext).registerShutdownHook();

執行輸出:

2、設定函式的依賴注入

package di;

/**
 * @Author LLF
 * @Date Created in 2020/12/7 17:24
 * @Description
 * @Version 1.0
 **/
public class Text3 {

    public void text3m(){
        System.out.println("text3方法。。。。。。。。。。。。。。。");
    }
}
package di;

/**
 * @Author LLF
 * @Date Created in 2020/12/7 17:24
 * @Description
 * @Version 1.0
 **/
public class Text4 {

    public void text4m(){
        System.out.println("text4方法。。。。。。。。。。。。。。。");
    }
}
package di;

/**
 * @Author LLF
 * @Date Created in 2020/12/7 17:24
 * @Description
 * @Version 1.0
 **/
public class Text5 {

    private Text3 text3;

    private Text4 text4;

    public void setText3(Text3 text3) {
        this.text3 = text3;
    }

    public void setText4(Text4 text4) {
        this.text4 = text4;
    }

    public void text5m(){
        text3.text3m();
        text4.text4m();
    }
}

bean配置:

    <bean id="Text3" class="di.Text3">
    </bean>

    <bean id="Text4" class="di.Text4">
    </bean>

    <bean id="Text5" class="di.Text5">
        <property name="Text3" ref="Text3"></property>
        <property name="Text4" ref="Text4"></property>
    </bean>
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("config/spring.xml");
        Text5 text5 = (Text5) applicationContext.getBean("Text5");
        text5.text5m();
//        關閉hook執行銷燬方法
        ((ClassPathXmlApplicationContext) applicationContext).registerShutdownHook();

執行結果:

package di;

/**
 * @Author LLF
 * @Date Created in 2020/12/7 17:34
 * @Description
 * @Version 1.0
 **/
public class Text6 {

    public void text6m(){
        System.out.println("text6方法。。。。。。。。。。。。");
    }
}
package di;

/**
 * @Author LLF
 * @Date Created in 2020/12/7 17:34
 * @Description
 * @Version 1.0
 **/
public class Text7 {

    private Text6 text6;

    public Text6 getText6() {
        return text6;
    }

    public void setText6(Text6 text6) {
        this.text6 = text6;
    }

    public void text7m(){
        text6.text6m();
    }
}

bean配置:

    <bean id="Text7" class="di.Text7">
        <property name="Text6">
            <bean id="Text6" class="di.Text6"></bean>
        </property>
    </bean>
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("config/spring.xml");
        Text7 text7 = (Text7) applicationContext.getBean("Text7");
        text7.text7m();
//        關閉hook執行銷燬方法
        ((ClassPathXmlApplicationContext) applicationContext).registerShutdownHook();

執行結果: