你的位置:首页 > 软件开发 > Java > Spring02、工厂+xml配置详解+创建bean的三种方式

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

发布时间:2017-10-19 21:00:22
前言一、Spring的工厂(容器)1.1ApplicationContext:ApplicationContext下面有两个接口的实现类,用于加载Spring的配置文件:如上所示,这两个实现类有着不同的加载配置文件的方式:ClassPathFileSystem1.2BeanFac ...

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

前言

一、Spring的工厂(容器)

1.1ApplicationContext:

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

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

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

ClassPath

FileSystem

1.2BeanFactory(过时)

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

1.3BeanFactory和ApplicationContext的区别

BeanFactory:是在 getBean 的时候才会生成类的实例
ApplicationContext:在加载 applicationContext.

二、配置 STS 的

复制路径:
* />查找

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

点击“Add”

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

三、spring配置文件详解

3.1关于Spring配置文件中的bean标签中的属性介绍:

<??><beans ="http://www.springframework.org/schema/beans" ="http://www.w3.org/2001/ ="http://www.springframework.org/schema/p" ="http://www.springframework.org/schema/context" ="http://www.springframework.org/schema/aop" ="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans       >>  <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(); }}

配置文件的配置:

<??><beans ="http://www.springframework.org/schema/beans" ="http://www.w3.org/2001/ ="http://www.springframework.org/schema/p" ="http://www.springframework.org/schema/context" ="http://www.springframework.org/schema/aop" ="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans       >> <!-- 使用无参数的构造函数创建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.ClassPathimport 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 ClassPath);  BeanFirstFactory bff = (BeanFirstFactory) applicationContext.getBean("BeanFirst");  bff.test();// 调用方法 } /**  * 测试第二种方式  */ public static void test_02() {  ApplicationContext applicationContext = new ClassPath);  applicationContext.getBean("BeanSecond"); } /**  * 测试第三种方式  */ public static void test_03() {  ApplicationContext applicationContext = new ClassPath);  applicationContext.getBean("BeanThird"); } public static void main(String[] args) {  test_01();  // test_02();  // test_03(); }}

 

 

海外公司注册、海外银行开户、跨境平台代入驻、VAT、EPR等知识和在线办理:https://www.xlkjsw.com

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

关键词:Spring

*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。

可能感兴趣文章

我的浏览记录