Spring MVC學習入門筆記

課前預習

Spring 第一天

學習Spring的容器功能,就是建立管理對象的能力java

使用Spring

  1. 添加依賴spring

  2. 建立XML文件app

  3. 配置beanmaven

  4. 實例化上下文類ide

  5. getBean獲得對象學習

name

分隔符是什麼? 分隔符能夠混用嗎? 多個bean的name配置能夠有相同的嗎?測試

配置的3種方式

  • 直接class配置,實例化的是class表明的對象ui

  • 工廠方法idea

    • factory-methodspa

    • factory-bean factory-method ,factory-bean能夠指向本身嗎?

  • FactoryBean

做用域

Singleton :由容器管理對象的生命週期,也就是說容器存在,對象就建立出來(設定的init方法會執行,容器銷燬,管理的對象也會銷燬

    (設定的銷燬方法會執行)

Prototype:getBean的臨時產生一個,產生以後的對象,spring容器就無論理,由程序本身去管理它的銷燬

全局的配置:default-xxxx

調用銷燬方法


((ConfigurableApplicationContext)applicationContext).close();

多配置文件

1 import2 new xxx("","")驗證:每一個文件裏面配置一個beangetBean的時候都能獲得

 

 

 

 

 

 

 

 

 

 

使用Spring步驟

  1. 添加依賴
  2. 建立xml文件
  3. 配置(須要被管理的類)bean
  4. 實例化上下文類
  5. GetBean對象

 

使用idea新建一個maven項目,在pom.xml中添加依賴:

<dependencies>
        <!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>4.3.13.RELEASE</version>
        </dependency>
</dependencies>

建立一個實體,Student.java:

package entity;

public class Student {
    public void hello(){
        System.out.println("hello SPring MVC!");
    }
}

選中項目右鍵--建立一個Spring Config:

<?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:aop="http://www.springframework.org/schema/aop"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">
    
    <bean id="stu" class="entity.Student"></bean>
</beans>

在實體包下寫一個Main.java類用於測試:

package entity;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Main {
    public static void main(String[] args) {
        //超類ApplicationContext(是一個接口)--因此須要new子類
        //ClassPath類路徑==和代碼放在一塊兒的,同在main文件夾下面
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("springMVC.xml");
        //Ctrl+H (快捷鍵)-->可看類型的層次 
        Student student = (Student) applicationContext.getBean("stu");     //xml的id名
        student.hello();
}

項目目錄結構:

 

 

 

問題:

  

 

答:分隔符之間能夠混用,分別可使用,;(空格)三種符號做爲分隔符,而:則會報錯,測試代碼以下:

    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:aop="http://www.springframework.org/schema/aop"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">
    <bean id="stu" name="student s,st" class="entity.Student"></bean>
    <!--能夠混用-->

    <!--<bean id="stu" name="student,s" class="entity.Student"></bean>
    <bean id="stu" name="student;s" class="entity.Student"></bean>-->

</beans>

    Main.java測試的代碼:

ApplicationContext applicationContext = new ClassPathXmlApplicationContext("springMVC.xml");
        //Ctrl+H -->類型的層次 xsd
        /*Student student = (Student) applicationContext.getBean("stu");
        student.hello();

        Student student1 = (Student) applicationContext.getBean("student");
        student1.hello();

        Student student2 = (Student) applicationContext.getBean("s");
        student2.hello();

        Student student3 = (Student) applicationContext.getBean("st");
        student3.hello();*/

 

  <!--建立對象有三種形式:1.直接new;2.間接使用工廠類new-爲何用:;3.實現接口-->

第一種:即上邊提到的一種

第二種:

    實體代碼:

package entity;

public class Student {
  
    public static Student hello(){
        System.out.println("hello SPring MVC!");
        return new Student();
    }

}

    xml代碼:

<bean id="xx" class="entity.Student" factory-method="hello"></bean>

    Main測試代碼:

Student student4 = (Student) applicationContext.getBean("xx");

 

第三種:

    實體代碼:

public Student hello (){
       System.out.println("hello SPring MVC!");
       return new Student();
   }

    xml代碼(方法一):

<bean id="factory" class="entity.Student"></bean>
    <bean id="yy" class="entity.Student" factory-bean="factory" factory-method="hello"></bean>

    xml代碼(方法二):  

<!--xml中可否寫本身:不能夠?別名呢?能夠-->
<bean id="factory" class="entity.Student" name="student s"></bean>
    <bean id="yy" class="entity.Student" factory-bean="s" factory-method="hello"></bean>

    Main測試代碼:

Student student5 = (Student) applicationContext.getBean("yy");

 

  <!--xml配置中可否去id,怎麼寫-->

實體代碼:

public static Student hello(){
        System.out.println("hello SPring MVC!");
        return new Student();
    }
 

 xml代碼:

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

   Main測試代碼:

Student student6 =  applicationContext.getBean(Student.class);
student6.hello();

 

做用域

Singleton :由容器管理對象的生命週期,也就是說容器存在,對象就建立出來(設定的init方法會執行,容器銷燬,管理的對象也會銷燬(默認即爲Singleton做用域)

 
    (設定的銷燬方法會執行)

代碼示例:

實體代碼:

package test02.beans;

public class Emps {
    public Emps() {
        System.out.println("Emps的構造方法");
    }

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

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

}

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="emp" class="test02.beans.Emps" scope="singleton" init-method="aa" destroy-method="bb"></bean>

</beans>

Main.java測試代碼:

package test02.beans;

import org.springframework.context.ApplicationContext;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Main {
    public static void main(String[] args) {
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("EmpsMVC.xml");

        ((ConfigurableApplicationContext)applicationContext).close();
    }
}

運行效果圖:

 

 

 
 

Prototype:getBean的臨時產生一個,產生以後的對象,spring容器就無論理,由程序本身去管理它的銷燬

 

代碼示例:

實體代碼:同上

xml代碼:

<bean id="emp" class="test02.beans.Emps" scope="prototype" init-method="aa" destroy-method="bb"></bean>

Main測試代碼:

1.若是同上代碼運行是沒有結果的

2.

public class Main {
    public static void main(String[] args) {
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("EmpsMVC.xml");
        Emps emps = (Emps) applicationContext.getBean("emp");

        ((ConfigurableApplicationContext)applicationContext).close();
    }
}

運行結果同上。

 

 

若是有不少的實體都有相同的初始化方法,和銷燬方法,那麼能夠選擇在配置文件中定義全局的初始化或者銷燬的方法

default-init-method=""

default-destroy-method=""

 

 

 

多配置文件(兩個xml的的關聯方法)

1.在Main.java中new兩個ApplicationContext,再關聯:

驗證:每一個文件裏面配置一個beangetBean的時候都能獲得

2.ApplicationContext中指定多個參數「,」相隔":new xxx("","")

 

ApplicationContext applicationContext = new ClassPathXmlApplicationContext("EmpsMVC.xml","springMVC.xml");

 

3..搞一個總的配置文件,導入左右相關聯的,在xml中寫以下代碼

<import resource="springMVC.xml"></import>

項目目錄結構:

 

EmpMVC.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">

    <import resource="springMVC.xml"></import>

    <!--默認爲單例做用域-->
    <bean id="emp" class="test02.beans.Emps" scope="singleton" init-method="aa" destroy-method="bb"/>

</beans>

Main測試代碼:

package test02.beans;

import entity.Student;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Main {
    public static void main(String[] args) {
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("EmpsMVC.xml");

        Student student = (Student) applicationContext.getBean("stu");

        ((ConfigurableApplicationContext)applicationContext).close();
    }
}

運行結果:

 

 

 

補充:接口:

InitializingBean

DisposableBean

代碼示例:
ClassBean.java實體代碼:
package test03;

import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean;

public class ClassBean implements InitializingBean,DisposableBean {

    public void init (){
        System.out.println("init----------");
    }

    public void xiaohui (){
        System.out.println("xiaohui----------");
    }

    public void afterPropertiesSet() throws Exception {
        System.out.println("afterPropertiesSet--------------");
    }
    
    public void destroy() throws Exception {
        System.out.println("destroy----------------");
    }
}

xml代碼:

<bean id="clb" class="test03.ClassBean" init-method="init" destroy-method="xiaohui"></bean>

   Main測試代碼:

public class Main {
    public static void main(String[] args) {

        /*優先級不一樣,接口的初始化與銷燬會先輸出*/
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("classBeanMVC.xml");
        ClassBean classBean = (ClassBean) applicationContext.getBean("clb");
        ((ConfigurableApplicationContext)applicationContext).close();

    }
}
相關文章
相關標籤/搜索