spring容器启动的三种方式以及抽象工厂模式的应用

spring容器启动的三种方式以及抽象工厂模式的应用(AbstractApplicationContext-ClassPathXmlApplicationContext,FileSystemXmlApplicationContext,AnnotationConfigApplicationContext)

  • ApplicationContext的继承和子类的关系
    在这里插入图片描述
    在这里插入图片描述

  • 使用idea创建spring项目
    在这里插入图片描述

注解方式获取bean(AnnotationConfigApplicationContext)

@Configuration
public class AppConfig {
    @Bean(name="entitlement")
    public Entitlement entitlement() {
        Entitlement ent= new Entitlement();
        ent.setName("Entitlement");
        ent.setTime(1);
        return ent;
    }

    @Bean(name="entitlement2")
    public Entitlement entitlement2() {
        Entitlement ent= new Entitlement();
        ent.setName("Entitlement2");
        ent.setTime(2);
        return ent;
    }
}
public class Entitlement {
    private String name;
    private int time;

    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public int getTime() {
        return time;
    }
    public void setTime(int time) {
        this.time = time;
    }
}

测试

import org.springframework.context.annotation.AnnotationConfigApplicationContext;
public class JavaConfigTest {
    /**
     * https://www.cnblogs.com/kaituorensheng/p/8024199.html
     * @param arg
     */
    public static void main(String[] arg) {
        AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
        ctx.register(AppConfig.class);
        ctx.refresh();

        Entitlement ent = (Entitlement)ctx.getBean("entitlement");
        System.out.println(ent.getName());
        System.out.println(ent.getTime());

        Entitlement ent2 = (Entitlement)ctx.getBean("entitlement2");
        System.out.println(ent2.getName());
        System.out.println(ent2.getTime());

        ctx.close();
    }
}

配置文件xml方式(ClassPathXmlApplicationContext,FileSystemXmlApplicationContext)

二者区别

>ClassPathXmlApplicationContext 

默认文件路径是src下那一级
classpath:和classpath*:的区别: 
classpath: 只能加载一个配置文件,如果配置了多个,则只加载第一个 

classpath*: 可以加载多个配置文件,如果有多个配置文件,就用这个

>FileSystemXmlApplicationContext 
这个类,默认获取的是项目路径,默认文件路径是项目名下一级,与src同级。
如果前边加了file:则说明后边的路径就要写全路径了,就是绝对路径
file:D:/workspace/applicationContext.xml

resources下面创建bean.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:util="http://www.springframework.org/schema/util"
       xmlns:p="http://www.springframework.org/schema/p"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
		http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd">

    <!-- 配置一个 bean -->
    <bean id="helloWorld" class="org.lmj.test.xmlget.HelloWorld">
        <!-- 为属性赋值 -->
        <property name="user" value="Jerry"></property>
    </bean>

    <!-- 配置一个 bean -->
    <bean id="helloWorld2" class="org.lmj.test.xmlget.HelloWorld">
        <!-- 为属性赋值 -->
        <!-- 通过属性注入: 通过 setter 方法注入属性值 -->
        <property name="user" value="Tom"></property>
    </bean>

    <!-- 通过构造器注入属性值 -->
    <bean id="helloWorld3" class="org.lmj.test.xmlget.HelloWorld">
        <!-- 要求: 在 Bean 中必须有对应的构造器.  -->
        <constructor-arg value="Mike"></constructor-arg>
    </bean>
</beans>

helloworld如下:

package org.lmj.test.xmlget;

public class HelloWorld {

	private String user;
	
	public HelloWorld() {
		System.out.println("HelloWorld's constructor...");
	}
	
	public void setUser(String user) {
		System.out.println("setUser:" + user);
		this.user = user;
	}
	
	public HelloWorld(String user) {
		this.user = user;
	}

	public void hello(){
		System.out.println("Hello: " + user);
	}
	
}

测试类如下:

public class Main {
	public static void main(String[] args) {
		FileSystemXmlApplicationContext fileSystemXmlApplicationContext = new FileSystemXmlApplicationContext("resources/beans.xml");
		HelloWorld  helloWorld1 = (HelloWorld) fileSystemXmlApplicationContext.getBean("helloWorld");
		helloWorld1.setUser("fileSystemXmlApplicationContext.getbean()");
		helloWorld1.hello();

		//1. 创建 Spring 的 IOC 容器
		ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml");
		//是通过bean.xml里面定义的id来定位获取哪个bean实例的。@lcb
		//2. 从 IOC 容器中获取 bean 的实例
		HelloWorld  helloWorld = (HelloWorld) ctx.getBean("helloWorld");
		//是通过bean.xml里面定义的id来定位获取哪个bean实例的。@lcb
         System.out.println("bean实例的样子是"+ ctx);
		//org.springframework.context.support.ClassPathXmlApplicationContext@b81eda8: startup date [Sun Feb 17 17:40:32 CST 2019]; root of context hierarchy
		//可以看到bean实例,是一个runtime instance. 有一个生命周期。
		//根据类型来获取 bean 的实例: 要求在  IOC 容器中只有一个与之类型匹配的 bean, 若有多个则会抛出异常. 
		//一般情况下, 该方法可用, 因为一般情况下, 在一个 IOC 容器中一个类型对应的 bean 也只有一个. 
//		HelloWorld helloWorld1 = ctx.getBean(HelloWorld.class);
		//3. 使用 bean
		//有了实例,就可以通过实例来调用实例的方法,那么实例有哪些方法,文档中是要写清楚的。方法可以直接用。
		helloWorld.setUser("chao");
		helloWorld.hello();//hello这个方法,要做什么事情,也是要在方法里面写清楚的。比如这里我输入了setUser的"chao"参数,那么返回的是hello chaobin
	}
}

总结

FileSystemXmlApplicationContext,ClassPathXmlApplicationContext,AnnotationConfigApplicationContext三者都继承了BeanFactory。应用了抽象工厂模式(可参考:菜鸟教程-抽象工厂模式

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值