Spring的艺术(三):关于Spring中的Bean,一文搞定

简介: spring中的bean默认使用单例作用域,即全局只创建这一个bean对象,在程序中获取到的bean都是同一个。当然也可以显式的配置作用域

听说微信搜索《Java鱼仔》会变更强哦!


本文收录于JavaStarter ,里面有我完整的Java系列文章,学习或面试都可以看看哦


本章的内容基于官方文档编写:docs.spring.io/spring-fram…


Spring版本为5.2.9


(一)Bean的作用域


Spring中Bean的作用域一共有六种


网络异常,图片无法展示
|


(1.1)singleton单例作用域


spring中的bean默认使用单例作用域,即全局只创建这一个bean对象,在程序中获取到的bean都是同一个。当然也可以显式的配置作用域:



<beanid="user"class="com.javayz.pojo.User"scope="singleton">

(1.2)prototype原型作用域


原型模式是指每次从容器中get一个Bean对象的时候都会产生一个新对象


<beanid="user"class="com.javayz.pojo.User"scope="prototype">

(1.3)web开发中使用的另外四种作用域


request、session、application和websocket只能在web开发中使用,分别表示:


request:bean在一个请求中共用一个。


session:bean在同一个session中共用一个。


application:bean在单个ServletContext中共用一个。


websocket:bean在单个Websocket中共用一个。


(二)Bean的自动装配


在之前的内容中,所有的bean都是手动去给其赋值,除了手动赋值外,Bean还支持隐式地自动装配


首先搭建一个简单的例子用于测试:


2.1、例子搭建


两个实体类:


publicclassTeacher {
publicvoidteach(){
System.out.println("上课");
    }
}
publicclassStudent {
privateStringname;
privateTeacherteacher;
//省略get、set方法}

bean.xml


<?xmlversion="1.0" encoding="UTF-8"?><beansxmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://www.springframework.org/schema/beanshttps://www.springframework.org/schema/beans/spring-beans.xsd"><beanid="teacher"class="com.javayz.pojo.Teacher"/><beanid="student"class="com.javayz.pojo.Student"><propertyname="name"value="javayz"/><propertyname="teacher"ref="teacher"/></bean></beans>

测试:


@org.junit.Testpublicvoidtest(){
ApplicationContextcontext=newClassPathXmlApplicationContext("bean.xml");
Studentstudent= (Student) context.getBean("student");
student.getTeacher().teach();
}

这是一种手动装配的方式,在bean.xml中,我们给teacher这个变量手动装配了teacher这个id的bean。现在我们使用自动装配:


2.2 autowire自动装配


<beanid="teacher"class="com.javayz.pojo.Teacher"/><beanid="student"class="com.javayz.pojo.Student"autowire="byName"><propertyname="name"value="javayz"/></bean>

在这里我们没有写teacher属性对应哪个bean,而是用了autowire="byName",Spring容器就会自动帮我们根据bean的名称去寻找对应的bean。除了byName后,还有其他的几种方式:


byName:自动在上下文中查找,找到和自己set方法后的值名称相同的bean;


byType:自动在上下文中查找,找到和自己对象类型相同的bean,使用byType需要保证bean全局唯一;


(三) 使用注解实现自动装配


使用注解需要我们配置开启:


<?xmlversion="1.0" encoding="UTF-8"?><beansxmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:context="http://www.springframework.org/schema/context"xsi:schemaLocation="http://www.springframework.org/schema/beanshttps://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/contexthttps://www.springframework.org/schema/context/spring-context.xsd"><context:annotation-config/></beans>

还是上面的这个例子,这次在xml文件中只申明一个bean:


<?xmlversion="1.0" encoding="UTF-8"?><beansxmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:context="http://www.springframework.org/schema/context"xsi:schemaLocation="http://www.springframework.org/schema/beanshttps://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/contexthttps://www.springframework.org/schema/context/spring-context.xsd"><context:annotation-config/><beanid="teacher"class="com.javayz.pojo.Teacher"/><beanid="student"class="com.javayz.pojo.Student"/></beans>

在实体类中通过@Autowired注解注入Bean


publicclassStudent {
privateStringname;
@AutowiredprivateTeacherteacher;
//省略get、set方法}

最终实现的效果和手动的注入是一样的。如果你在xml中为同一个类定义了多个bean,则可以使用@Qualifier()指定某个bean:


publicclassStudent {
privateStringname;
@Autowired@Qualifier(value="teacher2")
privateTeacherteacher;
}

除了使用@Autowired外,还可以使用@Resocure注解装配bean


publicclassStudent {
privateStringname;
@ResourceprivateTeacherteacher;
}

@Autowired和@Resource的区别在于:


@Autowired通过ByName方式实现,如果找不到对象就报空指针异常;


@Resource通过Byname方式实现,如果找不到名字,就通过ByType实现,否则就报错;

相关文章
|
13天前
|
Java 开发者 Spring
解析Spring中Bean的生命周期
解析Spring中Bean的生命周期
16 2
|
4天前
|
XML Java 数据格式
Spring5系列学习文章分享---第一篇(概述+特点+IOC原理+IOC并操作之bean的XML管理操作)
Spring5系列学习文章分享---第一篇(概述+特点+IOC原理+IOC并操作之bean的XML管理操作)
11 1
|
13天前
|
XML Java 数据格式
深度解析 Spring 源码:从 BeanDefinition 源码探索 Bean 的本质
深度解析 Spring 源码:从 BeanDefinition 源码探索 Bean 的本质
23 3
|
4天前
|
XML druid Java
Spring5系列学习文章分享---第二篇(IOC的bean管理factory+Bean作用域与生命周期+自动装配+基于注解管理+外部属性管理之druid)
Spring5系列学习文章分享---第二篇(IOC的bean管理factory+Bean作用域与生命周期+自动装配+基于注解管理+外部属性管理之druid)
7 0
|
5天前
|
Java Spring
Spring的BeanPostProcessor后置处理器与bean的生命周期
Spring的BeanPostProcessor后置处理器与bean的生命周期
|
6天前
|
Java Spring 容器
解读spring5源码中实例化单例bean的调用链
解读spring5源码中实例化单例bean的调用链
|
8天前
|
运维 Java 关系型数据库
Spring运维之boot项目bean属性的绑定读取与校验
Spring运维之boot项目bean属性的绑定读取与校验
13 2
|
13天前
|
Java 开发者 Spring
Spring 中 Bean 的生命周期
Spring 中 Bean 的生命周期
12 2
|
2月前
|
Java 容器 Spring
Spring的加载配置文件、容器和获取bean的方式
Spring的加载配置文件、容器和获取bean的方式
30 3
Spring的加载配置文件、容器和获取bean的方式
|
2月前
|
Java Spring 容器
Spring注解开发,bean的作用范围及生命周期、Spring注解开发依赖注入
Spring注解开发,bean的作用范围及生命周期、Spring注解开发依赖注入
35 1
Spring注解开发,bean的作用范围及生命周期、Spring注解开发依赖注入