ClassPathXmlApplicationContext的使用

核心在创建IoC容器对象,用容器获取bean实例,有了实例就可以根据API提供的方法使用了。 而且,一个IoC容器可以获取多个实例:
// 1. 创建 Spring 的 IOC 容器
ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml");

//2. 从 IOC 容器中获取 bean 的实例
HelloWorld helloWorld = (HelloWorld) ctx.getBean("helloWorld"); Car car = (Car) ctx.getBean("car");

//这里是重复的例子,用bean.xml中的id = car
Car car2 = (Car) ctx.getBean(“car2”);
//3. 使用 bean //有了实例,就可以通过实例来调用实例的方法,那么实例有哪些方法,文档中是要写清楚的。方法可以直接用。

helloWorld.setUser("chao");
		helloWorld.hello();//hello这个方法,要做什么事情,也是要在方法里面写清楚的。比如这里我输入了setUser的"chao"参数,那么返回的是hello chaobin
		

测试类Main:

package com.atguigu.spring.helloworld;

import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Main {
	
	public static void main(String[] args) {
		
//		HelloWorld helloWorld = new HelloWorld();
//		helloWorld.setUser("Tom");
//		helloWorld.hello(); 
		
		//1. 创建 Spring 的 IOC 容器
		ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml");

		//2. 从 IOC 容器中获取 bean 的实例
		HelloWorld  helloWorld = (HelloWorld) ctx.getBean("helloWorld");
		//是通过bean.xml里面定义的id来定位获取哪个bean实例的。@lcb
         System.out.println("bean实例的样子是"+ ctx);
         ctx.getBean("car");//获取的bean实例是什么样子的?
		//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
		
		Car car = (Car) ctx.getBean("car"); //这里是重复的例子,用bean.xml中的id = car 得到实例。
		System.out.println(car);
		
		Car car2 = (Car) ctx.getBean("car2");
		//@lcb 这里必须进行一次墙砖,原因如下:
		//Incompatible types.
		//Required:
		//com.atguigu.spring.helloworld.Car
		//Found:
		//java.lang.Object

		System.out.println(car2);
		
		//4. 测试使用集合属性
		User user = (User) ctx.getBean("user5");
		System.out.println(user);
	}
	
}

beans.xml 文件 (会被作为参数传给ClassPathXmlApplicationContext)

<?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="com.atguigu.spring.helloworld.HelloWorld">
		<!-- 为属性赋值 -->
		<property name="user" value="Jerry"></property>
	</bean>
	
	<!-- 配置一个 bean -->
	<bean id="helloWorld2" class="com.atguigu.spring.helloworld.HelloWorld">
		<!-- 为属性赋值 -->
		<!-- 通过属性注入: 通过 setter 方法注入属性值 -->
		<property name="user" value="Tom"></property>
	</bean>
	
	<!-- 通过构造器注入属性值 -->
	<bean id="helloWorld3" class="com.atguigu.spring.helloworld.HelloWorld">
		<!-- 要求: 在 Bean 中必须有对应的构造器.  -->
		<constructor-arg value="Mike"></constructor-arg>
	</bean>
	
	<!-- 若一个 bean 有多个构造器, 如何通过构造器来为 bean 的属性赋值 -->
	<!-- 可以根据 index 和 value 进行更加精确的定位. (了解) -->
	<bean id="car" class="com.atguigu.spring.helloworld.Car">
		<constructor-arg value="KUGA" index="1"></constructor-arg>
		<constructor-arg value="ChangAnFord" index="0"></constructor-arg>
		<constructor-arg value="250000" type="float"></constructor-arg>
	</bean>
	
	<bean id="car2" class="com.atguigu.spring.helloworld.Car">
		<constructor-arg value="ChangAnMazda"></constructor-arg>
		<!-- 若字面值中包含特殊字符, 则可以使用 DCDATA 来进行赋值. (了解) -->
		<constructor-arg>
			<value><![CDATA[<ATARZA>]]></value>
		</constructor-arg>
		<constructor-arg value="180" type="int"></constructor-arg>
	</bean>
	
	<!-- 配置 bean -->
	<bean id="dao5" class="com.atguigu.spring.ref.Dao"></bean>

	<bean id="service" class="com.atguigu.spring.ref.Service">
		<!-- 通过 ref 属性值指定当前属性指向哪一个 bean! -->
		<property name="dao" ref="dao5"></property>
	</bean>
	
	<!-- 声明使用内部 bean -->
	<bean id="service2" class="com.atguigu.spring.ref.Service">
		<property name="dao">
			<!-- 内部 bean, 类似于匿名内部类对象. 不能被外部的 bean 来引用, 也没有必要设置 id 属性 -->
			<bean class="com.atguigu.spring.ref.Dao">
				<property name="dataSource" value="c3p0"></property>
			</bean>
		</property>
	</bean>
	
	<bean id="action" class="com.atguigu.spring.ref.Action">
		<property name="service" ref="service2"></property>
		<!-- 设置级联属性(了解) -->
		<property name="service.dao.dataSource" value="DBCP2"></property>
	</bean>
	
	<bean id="dao2" class="com.atguigu.spring.ref.Dao">
		<!-- 为 Dao 的 dataSource 属性赋值为 null, 若某一个 bean 的属性值不是 null, 使用时需要为其设置为 null(了解) -->
		<property name="dataSource"><null/></property>
	</bean>
	
	<!-- 装配集合属性 -->
	<bean id="user" class="com.atguigu.spring.helloworld.User">
		<property name="userName" value="Jack"></property>
		<property name="cars">
			<!-- 使用 list 元素来装配集合属性 -->
			<list>
				<ref bean="car"/>
				<ref bean="car2"/>
			</list>
		</property>
	</bean>
	
	<!-- 声明集合类型的 bean -->
	<util:list id="cars">
		<ref bean="car"/>
		<ref bean="car2"/>
	</util:list>
	
	<bean id="user2" class="com.atguigu.spring.helloworld.User">
		<property name="userName" value="Rose"></property>
		<!-- 引用外部声明的 list -->
		<property name="cars" ref="cars"></property>
	</bean>
	
	<bean id="user3" class="com.atguigu.spring.helloworld.User"
		p:cars-ref="cars" p:userName="Titannic"></bean>
		
	<!-- bean 的配置能够继承吗 ? 使用 parent 来完成继承 -->	
	<bean id="user4" parent="user" p:userName="Bob"></bean>
	
	<bean id="user6" parent="user" p:userName="维多利亚"></bean>
	
	<!-- 测试 depents-on -->	
	<bean id="user5" parent="user" p:userName="Backham" depends-on="user6"></bean>
	
</beans>

HelloWorld类

package com.atguigu.spring.helloworld;

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);
	}
	
}
  • 8
    点赞
  • 32
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值