啥是Spring Bean
比较官方的解释是 Spring Bean是事物处理组件类和实体类(POJO)对象的总称,是能够被实例化、能够被spring容器管理的java对象。可以把bean看做是一个组件,这个组件用来具体实现某个业务功能。总结性的讲,Bean就是由IOC容器初始化、装配及管理的对象,除此之外,和程序中的其他对象没有区别。
怎么使用Bean呢
在spring中bean是由spring容器创建和管理的,各组件之间的依赖关系也是由spring容器管理的,这在很大程度上减少了开发者的工作量。但是你也不是什么都不需要做的,你要告诉spring要创建哪些bean并且如何将这些bean装配在一起。看个图吧:
Spring Bean 的装配
①基于xml配置装配
声明一个简单的bean,在xml中可以按照如下形式声明一个bean:
<bean id=”foo” class="com.test.Foo" >
<property name=”fooPro” value=”proVal”>
<property name=”otherObj” ref=”otherObj”>
</bean>
这里声明了一个简单的Bean,创建Bean的类由class属性指定(需要全限定类名),id是Bean的唯一标识,如果没有指定id属性的值则取权限定类名作为id,com.test.Foo类的定义如下:
Public Class Foo{
private string fooPro;
private OtherObj otherObj;
public void setFooPro(string fooPro){
this.fooPro = fooPro;
} public void setOtherObj (OtherObj otherObj){
this.otherObj = otherObj;
} …}
<property>元素实现属性注入,即将“proVal”赋值给com.test.Foo的“fooPro”属性,这里注入的是一个常量值。如果该Bean依赖于另一个Bean,那么使用ref属性代替value属性将所依赖的Bean注入,ref属性的值指向的是依赖的Bean的id。为了让配置文件变得简洁,Spring提供了p-命名空间来替代property元素,作用是相同的,简洁配置如下:
<bean id=”foo” class="com.test.Foo" p:fooPro=”proVal” p:otherObj-ref=“otherObj” ></bean>
常量值属性的注入为:p:属性名=”属性值”,Bean属性的注入:p:属性名-ref=”引用的Bean的id”。以上使用的是setter方法注入,Spring还提供了一种构造器注入的方式,简单介绍一下(使用的较少,一般都是用setter注入的形式):
<bean id=”foo” class="com.test.Foo" >
<constructor-arg value=”proVal”>
<constructor-arg ref=”otherObj”>
</bean>
属性值的注入是按构造函数中的参数顺序依次赋值的。
②基于Java代码装配
使用Java代码装配首先需要创建一个配置类(JavaConfig)
@Configuration
public class FooConf{
@Bean
public OtherObj otherObj(){
return new OtherObj();
}}
@Configuration表明该类是一个配置类,@Bean注解用于声明一个Bean,@Bean注解的方法通知Spring该方***返回一个Bean并且会注册为Spring应用上下文的Bean。
③基于注解的装配(自动化装配)
Spring从两个角度实现自动化装配:
第一步:组件扫描(component scanning),Spring会自动发现应用上下文中所创建的bean。
第二不步:自动装配( autowiring),Spring自动满足bean之间的依赖。
创建能被扫描到的Bean:
@Component
public class Foo(){
}
@Component注解表明该类是一个组件类,它将通知Spring为该类创建一个Bean。那么该如何让Spring去扫描@Component注解的类呢?有两种方式:
使用@ComponentScan注解启用组件扫描
@Configuration
@ComponentScan
public class FooConf{
}
如果没有其他配置的话,@ComponentScan默认会扫描与配置类相同的包,查找带有@Component注解的类,可以使用basePackages属性设置扫描的基础包。
使用xml启用组件扫描
在xml配置文件中添加<context:component-scan base-package=”要扫描的基础包” />,作用同@ComponentScan注解。
@Component注解能够让Foo类注入到Spring容器中,但是如果Foo类依赖于其他类怎么办呢?使用@AutoWried注解。
@Component
public class Foo{ //成员变量使用@AutoWired
@AutoWried private OtherObj otherObj; ……}@Componentpublic class Foo{ private OtherObj otherObj; //构造方法使用@AutoWired
@AutoWried public Foo (OtherObj otherObj){ this.otherObj = otherObj; }}@Componentpublic class Foo{ private OtherObj otherObj; //Setter方法使用@AutoWired
@AutoWried Public void setOtherObj (OtherObj otherObj){ this.otherObj = otherObj; }}
如上,@AutoWried注解能够用在构造器或者setter方法或者成员变量上,作用就是把Foo类依赖的Bean注入进来。
其实在启动spring IoC时,容器自动装载了一个AutowiredAnnotationBeanPostProcessor后置处理器,当容器扫描到@Autowied、@Resource或@Inject时,就会在IoC容器自动查找需要的bean,并装配给该对象的属性。
@Resource也可以把Foo类以来的Bean注入进来,但是@AutoWired默认是按类型装配,@Resource是按名字装配。当依赖Bean有多个实现类时,就可以使用@Resource注解装配指定的实现类(@Resource(name="otherObjImpl1")……)。
作者:加哇er
链接:https://juejin.im/post/5e814f076fb9a03c6422f576
来源:掘金