Spring02、工厂+xml配置详解+创建bean的三种方式

JAVA学习网 2017-10-20 15:00:03

前言

一、Spring的工厂(容器)

1.1ApplicationContext:

ApplicationContext下面有两个接口的实现类,用于加载Spring的配置文件:

如上所示,这两个实现类有着不同的加载配置文件的方式:

ClassPathXmlApplicationContext:加载类路径下面的spring的配置文件;

FileSystemXmlApplicationContext:加载本地磁盘中的spring的配置文件。

1.2BeanFactory(过时)

1.3BeanFactory和ApplicationContext的区别

BeanFactory:是在 getBean 的时候才会生成类的实例
ApplicationContext:在加载 applicationContext.xml(容器启动)时候就会创建

二、配置 STS 的 XML 的提示

复制路径:
* http://www.springframework.org/schema/beans/spring-beans.xsd
查找 XML Catalog:

点击“Add”

三、spring配置文件详解

3.1关于Spring配置文件中的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" xmlns:p="http://www.springframework.org/schema/p"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
    http://www.springframework.org/schema/context     
    http://www.springframework.org/schema/context/spring-context-3.0.xsd
    http://www.springframework.org/schema/tx 
    http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
    http://www.springframework.org/schema/aop 
    http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">
    
    <bean
        id="first" 
        name=""
        abstract="true" 
     autowire="default" autowire-candidate
="default" class="" depends-on="" destroy-method="" factory-bean="" factory-method="" init-method="" lazy-init="default" parent="" primary="true" scope=""> </bean> </beans>

主要介绍以上的属性:

id:Bean 起个名字,在约束中采用 ID 的约束,唯一,必须以字母开始,可以使用字母、数字、连字符、下划线、句话、冒号, id不能出现特殊字符

name:Bean 起个名字。没有采用 ID 的约束。name:出现特殊字符。如果<bean>没有 id 的话,name可以当做 id 使用。

abstract:设定ApplicationContext是否对bean进行预先的初始化。

autowire:

autowire-candidate:

class:

depends-on:

destroy-method:

factory-bean:

factory-method:

init-method:

lazy-init:

parent:

primary:

scope:

3.2bean生命周期的配置

通过配置<bean>标签上的 init-method 作为 Bean 的初始化的时候执行的方法,配置 destroy-method作为 Bean 的销毁的时候执行的方法。销毁方法想要执行,需要是单例创建的 Bean 而且在工厂关闭的时候,Bean 才会被销毁。

四、下面介绍Spring生成Bean的三种方式

4.1使用没有参数的构造函数的方式来生成

三种创建Bean的方式类:

package com.ygh.createBean;
/**
 * Spring创建bean的方式一之使用无参数的构造函数创建bean
 * @author 夜孤寒
 * @version 1.1.1
 */
public class BeanFirstFactory {
    public BeanFirstFactory(){
        System.out.println("使用无参数的构造函数创建bean...");
    }
    
    public void test(){
        System.out.println("Hello World!");
    }
}
package com.ygh.createBean;

/**
 * Spring创建bean的方式二之使用静态工厂实例化的方式
 * 
 * @author Administrator
 *
 */
public class BeanSecondFactory {
    public static BeanFirstFactory getBean2() {
        return new BeanFirstFactory();
    }
}
package com.ygh.createBean;

/**
 * Spring创建bean的方式三之使用实例工厂实例化的方式来创建bean
 * 
 * @author 夜孤寒
 * @version 1.1.1
 */
public class BeanThirdFactory {
    public BeanFirstFactory getBean3(){
        return new BeanFirstFactory();
    }
}

配置文件的配置:

<?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:p="http://www.springframework.org/schema/p"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
    http://www.springframework.org/schema/context     
    http://www.springframework.org/schema/context/spring-context-3.0.xsd
    http://www.springframework.org/schema/tx 
    http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
    http://www.springframework.org/schema/aop 
    http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">
    <!-- 使用无参数的构造函数创建bean的方式 -->
    <bean name="BeanFirst" class="com.ygh.createBean.BeanFirstFactory"></bean>
    
    <!-- 使用静态工厂实例化的方式来创建 -->
    <bean name="BeanSecond" class="com.ygh.createBean.BeanSecondFactory" factory-method="getBean2"></bean>

    <bean id="BeanThird" class="com.ygh.createBean.BeanThirdFactory"></bean>
    <bean id="Bean3" factory-bean="BeanThird" factory-method="getBean3"></bean>
</beans>

测试类:

package com.ygh.test;

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

import com.ygh.createBean.BeanFirstFactory;
import com.ygh.createBean.BeanSecondFactory;

/**
 * 测试类
 * 
 * @author 夜孤寒
 * @version 1.1.1
 *
 */
public class Test {
    /**
     * 测试第一种方式
     */
    public static void test_01() {
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring.xml");
        BeanFirstFactory bff = (BeanFirstFactory) applicationContext.getBean("BeanFirst");
        bff.test();// 调用方法
    }

    /**
     * 测试第二种方式
     */
    public static void test_02() {
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring.xml");
        applicationContext.getBean("BeanSecond");
    }

    /**
     * 测试第三种方式
     */
    public static void test_03() {
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring.xml");
        applicationContext.getBean("BeanThird");
    }

    public static void main(String[] args) {
        test_01();
        // test_02();
        // test_03();
    }
}

 

阅读(754) 评论(0)