Spring框架:在Ioc容器中配置Bean

版权声明:关注微信公众号:摸鱼科技资讯,联系我们 https://blog.csdn.net/qq_36949176/article/details/86261617

IOC&DI概述

配置Bean:

配置形式:基于XML文件的方式,基于注解的方式

Bean的配置方式:通过全类名(反射)、通过工厂方法(静态工厂方法&实例工厂方法)、FactoryBean

IOC容器BeanFactory&ApplicationContext概述

依赖注入的方式:属性注入,构造器注入

注入属性值细节

自动转配

bean之间的关系:继承;依赖

bean的作用域:singleton;prototype;WEB环境作用域

使用外部属性文件

spEL

IOC容器中Bean的生命周期

Spirng 4.x新特性:泛型依赖注入

在Spring的IOC容器里配置Bean

	<!-- 配置bean
	class:bean的全类名。通过反射的方式在IOC容器中创建Bean。所以要求Bean中必须有无参数的构造器
	id:标识容器中的bean,id唯一。	 
    -->
<bean id="helloworld" class="com.yorkmass.spring.beans.HelloWorld">

id:Bean的名称。

--在IOC容器中必须是唯一的

--若id没有指定,Spring自动将权限定性类名作为Bean的名字

--id可以指定多个名字,名字之间可用逗号,分号,或空格分隔

Spring容器

在Spring IOC容器读取Bean配置创建Bean实例之前,必须对它进行实例化,只有在容器实例化之后,才可以从IOC容器里获取Bean实例并使用。

Spring提供了两种类型的IOC容器实现

--BeanFactory:IOC容器的基本实现

--ApplicationContext提供了更多的高级特性,是BeanFactory的子接口

--BeanFactory是Spring框架的基础设施,面向Spring本身

ApplicationContext面向使用Spring框架的开发者,几乎所有应用场合都直接使用ApplicationContext而非底层的BeanFactory

--无论使用何种方式,配置文件时相同

在eclipse中我们可以使用Ctrl+T查看继承树;ctrl+shift+T: open type快捷键,用于查看继承类的方法

ApplicationContext详解

ApplicationContext 的主要实现类:

ClassPathXmlApplicationContext类路径下加载配置文件

FileSystemXmlApplicationContext: 从文件系统中加载配置文件

ConfigurableApplicationContext 扩展于 ApplicationContext,新增加两个主要方法:refresh() close(), 让 ApplicationContext 具有启动、刷新和关闭上下文的能力

ApplicationContext 在初始化上下文时就实例化所有单例的 Bean

WebApplicationContext 是专门为 WEB 应用而准备的,它允许从相对于 WEB 根目录的路径中完成初始化工作

package com.yorkmass.spring.beans;

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

public class Main {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		/*
		//创建HelloWorld的一个对象
		HelloWorld helloworld=new HelloWorld();
		为name属性赋值
		helloworld.setName("test");
		*/
		//1.创建Spring的IOC容器对象
		//ApplicationContext代表IOC容器
		//ClassPathXmlApplicationContext:是ApplicationContext接口的实现类,该实现类从类路径下来加载配置文件
		ApplicationContext ctx=new ClassPathXmlApplicationContext("applicationContext.xml");
		//2.从IOC容器中获取Bean实例
		//HelloWorld helloworld=(HelloWorld)ctx.getBean("helloworld");
		HelloWorld helloworld=ctx.getBean(HelloWorld.class);
		//当配置了两个bean,如果配置了两个class都是HelloWorld,就会出问题,所以这个方法要求bean里面的类唯一
		//利用类型返回IOC容器中的Bean,但要求IOC容器中必须只能有一个该类型的Bean
		//3.调用hello方法
		//helloworld.hello();
	}

}

 依赖注入的方式

Spring支持3种依赖注入的方式

--属性注入

--构造器注入

--工厂方法注入(很少使用,不推荐)

属性注入

属性注入即通过 setter 方法注入Bean 的属性值或依赖的对象(name为setXxx()方法之后的值Xxx转为xxx)

属性注入使用 <property> 元素, 使用 name 属性指定 Bean 的属性名称,value 属性或 <value> 子节点指定属性值

属性注入是实际应用中最常用的注入方式

这里的配置文件命名为:applicationContext.xml

<bean id="helloworld" class="com.yorkmass.spring.beans.HelloWorld">
<property name="name" value="nihao"></property>
</bean>

构造方法注入

通过构造方法注入Bean 的属性值或依赖的对象,它保证了 Bean 实例在实例化后就可以使用。

构造器注入在 <constructor-arg> 元素里声明属性, <constructor-arg> 中没有 name 属性

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

	<!-- 
	配置bean
	class:bean的全类名。通过反射的方式在IOC容器中创建Bean。所以要求Bean中必须由无参数的构造器
	id:标识容器中的bean,id唯一。	 -->
<bean id="helloworld" class="com.yorkmass.spring.beans.HelloWorld">
<property name="name" value="nihao"></property>
</bean>

	<!-- 通过构造方法来配置bean的属性 
	使用构造器注入属性可以指定参数的位置和参数的类型!以区分重载的构造器!-->
	<bean id="car" class="com.yorkmass.spring.beans.Car">
	<constructor-arg value="Audi" index="0"></constructor-arg>
	<constructor-arg value="shanghai" index="1"></constructor-arg>
	<constructor-arg value="300000" index="2" type="double"></constructor-arg>
	</bean>
	
	<bean id="car2" class="com.yorkmass.spring.beans.Car">
	<constructor-arg value="baoma" type="java.lang.String"></constructor-arg>
	<constructor-arg value="shanghai" type="java.lang.String"></constructor-arg>
	<constructor-arg value="240" type="int"/>
	</bean>

</beans>

类Car:

package com.yorkmass.spring.beans;

public class Car {
	private String brand;
	private String corp;
	private double price;
	private int maxSpeed;
	public Car(String brand, String corp, double price) {
		super();
		this.brand = brand;
		this.corp = corp;
		this.price = price;
	}
	@Override
	public String toString() {
		return "Car [brand=" + brand + ", corp=" + corp + ", price=" + price + ", maxSpeed=" + maxSpeed + "]";
	}
	public Car(String brand, String corp, int maxSpeed) {
		super();
		this.brand = brand;
		this.corp = corp;
		this.maxSpeed = maxSpeed;
	}
	
}

主类Main:

package com.yorkmass.spring.beans;

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

public class Main {

	public static void main(String[] args) {
		
	ApplicationContext ctx=newClassPathXmlApplicationContext("applicationContext.xml");
	Car car=(Car)ctx.getBean("car");
	System.out.println(car);
	car=(Car)ctx.getBean("car2");
	System.out.println(car);
  }

}

运行结果:

猜你喜欢

转载自blog.csdn.net/qq_36949176/article/details/86261617