前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >spring 配置bean[通俗易懂]

spring 配置bean[通俗易懂]

作者头像
全栈程序员站长
发布2022-07-07 18:09:50
3550
发布2022-07-07 18:09:50
举报

大家好,又见面了,我是全栈君。

概要:

spring 配置bean[通俗易懂]
spring 配置bean[通俗易懂]

在spring的IOC容器里配置Bean

在xml文件里通过bean节点来配置bean

spring 配置bean[通俗易懂]
spring 配置bean[通俗易懂]

id:Bean的名称

spring 配置bean[通俗易懂]
spring 配置bean[通俗易懂]

spring容器

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

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

  • BeanFactory:IOC容器的基本实现
  • ApplicationContext 提供了很多其它的高级特性。是BeanFactory的子接口
  • BeanFactory是spring框架的基础设施,面向spring本身,ApplicationContext面向使用spring框架的开发人员。差点儿全部的应用场合都直接使用ApplicationContext而非底层的BeanFactory
  • 不管使用何种方式。配置文件是同样的
spring 配置bean[通俗易懂]
spring 配置bean[通俗易懂]

ApplicationContext

  • ApplicationContext的主要实现类:
    • ClassPathXmlApplication:从类路径下载入配置文件
    • FileSystemXmlApplicationContext:从文件系统中载入配置文件
  • ConfigurableApplicationContext扩展于ApplicationContext,新添加两个主要方法:refresh()和close()。让ApplicationContext具有启动,刷新和关闭上下文的能力
  • ApplicationContext在初始化上下文时就实例化全部单例的Bean。
  • WebApplicationContext是专门为WEB应用而准备的,它同意从相对于WEB根文件夹的路径中完毕初始化工作

从IOC容器中获取Bean

  • 调用ApplicationContext的getBean()方法
spring 配置bean[通俗易懂]
spring 配置bean[通俗易懂]

依赖注入的方式

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

  • 属性注入
  • 构造器注入
  • 工厂方法注入(非常少使用,不推荐)

属性注入

  • 属性注入即通过setter方法注入Bean的属性值或依赖的对象
  • 属性注入使用<property>元素,使用name属性指定Bean的属性名称。value属性或<value>子结点指定属性值
  • 属性注入是实际应用中最经常使用的注入方式
spring 配置bean[通俗易懂]
spring 配置bean[通俗易懂]

构造方法注入

  • 通过构造方法注入Bean的属性值或依赖的对象,它保证了Bean实例在实例化后就能够使用
  • 构造器注入在<constructor-arg>元素里声明属性。<constructor-arg>中没有name属性

实例代码:

spring 配置bean[通俗易懂]
spring 配置bean[通俗易懂]

Car.java

代码语言:javascript
复制
package com.coslay.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;
	}
	
	
	
	public Car(String brand, String corp, int maxSpeed) {
		super();
		this.brand = brand;
		this.corp = corp;
		this.maxSpeed = maxSpeed;
	}



	public String toString() {
		return "Car [brand=" + brand + ", corp=" + corp + ", price=" + price
				+ ", maxSpeed=" + maxSpeed + "]";
	}
}

HelloWorld.java

代码语言:javascript
复制
package com.coslay.beans;

public class HelloWorld {
	private String name;
	
	public void setName(String name){
		System.out.println("setName: ");
		this.name = name;
	}
	
	public void hello(){
		System.out.println("hello: "+name);
	}
	
	public HelloWorld(){
		System.out.println("HelloWorld's Constructor...");
	}
}

Main.java

代码语言:javascript
复制
package com.coslay.spring;

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

import com.coslay.beans.Car;
import com.coslay.beans.HelloWorld;

public class Main {
	public static void main(String[] args) {
		
//		//创建HelloWorld的一个对象
//		HelloWorld helloWorld = new HelloWorld();
//		//为name属性赋值
//		helloWorld.setName("yyz");
//      使用spring以后,这两步可交给spring完毕		
		
		
		//1.创建spirng的IOC容器对象
		//ApplicationContext 代表IOC容器
		//ClassPathXmlApplicationContext:是ApplicationContext接口的实现类,该实现类从类路径下载入配置文件
		ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
		//创建容器的时候会调用全部bean对象的构造器,并为bean注入(赋值)
		
		//2.从IOC容器中获取Bean实例
		//利用id定位到IOC容器中的bean
	    //HelloWorld helloWorld = (HelloWorld) ctx.getBean("helloWorld");
		//利用类型返回IOC容器中的Bean。但要求IOC容器中必须仅仅能有一个该类型的Bean
		//HelloWorld helloWorld = ctx.getBean(HelloWorld.class);
		
		//调用hello方法
		//helloWorld.hello();
		
		Car car = (Car) ctx.getBean("car");
		System.out.println(car);
		
		Car car2 = (Car) ctx.getBean("car2");
		System.out.println(car2);
	}
}

applicationContext.xml

代码语言:javascript
复制
<?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.coslay.beans.HelloWorld">		<property name="name" value="yyz"></property>	</bean>		<!-- 通过构造方法来配置bean的属性 -->	<bean id="car" class="com.coslay.beans.Car">		<constructor-arg value="Audi" index="0"></constructor-arg>		<constructor-arg value="ShangHai" index="1"></constructor-arg>		<constructor-arg value="300000" type="double"></constructor-arg>	</bean>		<!-- 使用构造器注入属性值能够指定參数的位置和參数的类型,以区分重载的构造器 -->	<bean id="car2" class="com.coslay.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"></constructor-arg>	</bean></beans>

參考: http://hanyanguo-126-com.iteye.com/blog/406277

发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/116439.html原文链接:https://javaforall.cn

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2022年1月2,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
相关产品与服务
容器服务
腾讯云容器服务(Tencent Kubernetes Engine, TKE)基于原生 kubernetes 提供以容器为核心的、高度可扩展的高性能容器管理服务,覆盖 Serverless、边缘计算、分布式云等多种业务部署场景,业内首创单个集群兼容多种计算节点的容器资源管理模式。同时产品作为云原生 Finops 领先布道者,主导开源项目Crane,全面助力客户实现资源优化、成本控制。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档