基於XmlBeanFactory加載bean分析(一)

本例子講解是基於IDEA做爲開發工具,使用maven進行包管理java

1、項目pom.xml配置web

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>spring-mvc-sample</groupId>
    <artifactId>spring-mvc-sample</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>war</packaging>
    <name>spring-mvc入門簡單</name>
    <url>http://maven.apache.org</url>

    <dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>4.3.9.RELEASE</version>
        </dependency>
        <!--servlet -->
        <!--<dependency>-->
            <!--<groupId>javax.servlet</groupId>-->
            <!--<artifactId>servlet-api</artifactId>-->
            <!--<version>2.5</version>-->
        <!--</dependency>-->
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>3.1.0</version>
            <scope>provided</scope>
        </dependency>
        <!--servlet -->
        <dependency>
            <groupId>jstl</groupId>
            <artifactId>jstl</artifactId>
            <version>1.2</version>
        </dependency>
        <!--spring -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>4.2.1.RELEASE</version>
        </dependency>

        <!--commons-fileupload -->
        <dependency>
            <groupId>commons-fileupload</groupId>
            <artifactId>commons-fileupload</artifactId>
            <version>1.3.1</version>
        </dependency>

        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>test</scope>
        </dependency>

        <!--spring junit-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-test</artifactId>
            <version>4.2.1.RELEASE</version>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <finalName>spring-mvc-sample</finalName>
        <plugins>
            <plugin>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <configuration>
                    <skip>true</skip>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>

2、項目關鍵代碼spring

  • spring配置文件applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
       xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:task="http://www.springframework.org/schema/task"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd
    http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.2.xsd
    http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.2.xsd
    http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-4.2.xsd">
    
    <!-- <context:annotation-config /> -->
    <context:component-scan base-package="com.wolf"/>

    <!--<bean name="ApplicationContextUtil;zwApplicationUtil" class="com.wolf.util.ApplicationContextUtil">-->

    <!--</bean>-->

    <bean name="myBean,gagBean" class="com.wolf.bean.Mybean1">
        <property name="name">
            <value>鄭先生</value>
        </property>
        <property name="pwd">
            <value>666666888888</value>
        </property>
    </bean>

</beans>
  • Mybean1類
public class Mybean1 {

    private String name;

    private String pwd;

    public String getName() {
        return name;
    }

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

    public String getPwd() {
        return pwd;
    }

    public void setPwd(String pwd) {
        this.pwd = pwd;
    }
}
  • testCase
public class BeanFactoryTest {

    @Test
    public void testBeanFactory(){
        BeanFactory bf = new XmlBeanFactory(new ClassPathResource("myresource/applicationContext.xml"));
        System.out.println("判斷myBean是否存在。返回結果:"+bf.containsBean("myBean"));
        System.out.println("判斷zwBean是否存在。返回結果:"+bf.containsBean("gagBean"));
        Mybean1 mybean1 = bf.getBean("myBean",Mybean1.class);
        System.out.println("---show myBean properties");
        System.out.println("myBean name:"+mybean1.getName());
        System.out.println("myBean pwd:"+mybean1.getPwd());
        System.out.println("---end show myBean properties");
    }

    //MyComponentBean zwBean = bf.getBean("zwBean",MyComponentBean.class);
}

控制檯輸出:apache

七月 26, 2017 11:44:04 上午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
信息: Loading XML bean definitions from class path resource [myresource/applicationContext.xml]
判斷myBean是否存在。返回結果:true
判斷zwBean是否存在。返回結果:true
---show myBean properties
myBean name:鄭先生
myBean pwd:666666888888
---end show myBean propertiesapi

3、基於XmlBeanFactory源代碼分析第一句代碼spring-mvc

BeanFactory bf = new XmlBeanFactory(new ClassPathResource("myresource/applicationContext.xml"));

也就是BeanFactory究竟是怎麼來的?怎麼被建立出來的?在執行邏輯中又作了哪些事情,這裏幾乎完成了spring-beans的初始化。裏面的執行的邏輯很是複雜,可是咱們不要被複雜所矇蔽,咱們分析代碼必定要了解主線(它作了些什麼),而後在具體深刻某一個點了解(例如:對xml文件進行初始化時,到底作了哪些事;對註解初始化時又作了哪些事)mvc

經過序列圖咱們看到具體的執行過程,這裏咱們並不會具體深刻分析到某個分支,由於到某個分支時,太過於複雜了。app

【這裏設計到了一個核心的類XmlBeanDefinitionReader】maven

相關文章
相關標籤/搜索