比较官方的解释是 Spring Bean是事物处理组件类和实体类(POJO)对象的总称,是可以被实例化、可以被spring容器管理的java对象。能够把bean看作是一个组件,这个组件用来具体实现某个业务功能。总结性的讲,Bean就是由IOC容器初始化、装配及管理的对象,除此以外,和程序中的其余对象没有区别。java
在spring中bean是由spring容器建立和管理的,各组件之间的依赖关系也是由spring容器管理的,这在很大程度上减小了开发者的工做量。可是你也不是什么都不须要作的,你要告诉spring要建立哪些bean而且如何将这些bean装配在一块儿。看个图吧:spring
①基于xml配置装配函数
声明一个简单的bean,在xml中能够按照以下形式声明一个bean:this
<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类的定义以下:spa
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元素,做用是相同的,简洁配置以下:code
<bean id=”foo” class="com.test.Foo" p:fooPro=”proVal” p:otherObj-ref=“otherObj” ></bean>
复制代码
常量值属性的注入为:p:属性名=”属性值”,Bean属性的注入:p:属性名-ref=”引用的Bean的id”。以上使用的是setter方法注入,Spring还提供了一种构造器注入的方式,简单介绍一下(使用的较少,通常都是用setter注入的形式):component
<bean id=”foo” class="com.test.Foo" >
<constructor-arg value=”proVal”>
<constructor-arg ref=”otherObj”>
</bean>
复制代码
属性值的注入是按构造函数中的参数顺序依次赋值的。cdn
②基于Java代码装配xml
使用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;
……
}
@Component
public class Foo{
private OtherObj otherObj;
//构造方法使用@AutoWired
@AutoWried
public Foo (OtherObj otherObj){
this.otherObj = otherObj;
}
}
@Component
public 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")……)。
欢迎你们关注公众号,了解更多文章,你们一块儿进步!