首页 > 其他 > 详细

Spring中bean配置的学习笔记

时间:2014-03-03 13:52:34      阅读:568      评论:0      收藏:0      [点我收藏+]

学习Spring,时间一长就把最开始学的基础生疏掉了...算是写个笔记以便以后复习吧

什么bean,在Spring里可以理解为组件。可以联系一下我在前一篇笔记《理解Spring框架的控制反转——学习笔记》中放进IoC容器里的东西,比如说放在容器实例化的类,在容器中依赖注入的类...等等。可以先配置一个简单的bean看看,代码如下:

首先是一个创建一个Bean类,其实也就是个普通的类,为其中属性提供了setter而已:

bubuko.com,布布扣
public class Fruit{
  private String color;
  private String name;

  public Fruit(){}
  public Fruit(String name,String color){
      this.color=color;
      this.name=name;          
  }        
  public void setColor(String color) {
    this.color = color;
  }
  public void setName(String name) {
    this.name = name;
  }
 public void mySaid(){
  System.out.println("我是"+name+",我的颜色是:"+color);
} }
bubuko.com,布布扣

 配置文件就索性名为bean.xml,放在classpath下,以便待会实例化容器的时候找到它,代码如下:

bubuko.com,布布扣
<?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-2.5.xsd">
    <bean id="fruit" class="com.czh.Fruit">
     <!--下面是setter注入-->
<property name="color" value="red"/> <property name="name"> <value>apple</value> </property>
    <!--下面是构造器注入-->  
    <constructor-arg>
      <value>red</value>
</constructor-arg>
       <constructor-arg>
<value>red</value>
</constructor-arg>
    </bean>
</beans>
bubuko.com,布布扣

这样一来,bean就配置好了。上面代码里setter注入用了两种写法,效果是相同的,都会将value值注入到相应的属性中。

而且要注意的是,<property>标签是指要注入的属性,它是通过name的值来找到相应属性的。正如上面代码所示,<property name="color" value="red"/>,那么它就会去找这个bean所设置的类里(上面代码里,类为”com.czh.Fruit")的一个名为setColor的方法,然后注入value值。同理,如果property的name为“abc",则容器也会到类里找setAbc方法。

组件写好了,也配置进了容器中,下一步就是实例化容器:

bubuko.com,布布扣
public class Main{
    public static void main(String[] args) {
        ApplicationContext context=new ClassPathXmlApplicationContext("bean.xml");//实例化容器
        Fruit fruit=context.getBean("fruit");//找到配置好的bean
        fruit.mySaid();            
    }
}    
bubuko.com,布布扣

容器实例化的方法还有很多种,用ClassPathXmlApplicationContext类可以从classpath路径上实例化容器。实例化容器之后,就可以用getBean方法去找配置在容器中的组件了,比如context.getBean("fruit");这个时候容器就把该注入的内容注入进组件,并实例化该组件,于是这时候调用该组件的mySaid()方法,可以看到输出"我是apple,我的颜色是:red".

 

上面介绍了最简单的配置,和实例化容器。现在来看看如何在Bean配置文件指定对Bean的引用..这句话可以理解成这样:A类中需要引用B类的方法,首先需要在A类中实例化B类,然后使用B类方法。但是在使用容器的情况下,可以通过Setter或者构造器把B类注入到A类中...这就是一个Bean对另一个Bean的引用了,具体看下面代码:

bubuko.com,布布扣
public class Fruit{
  private String color;
  private String name;
  private Price price;     //Price类

  public Fruit(){}
  public Fruit(String name,String color,Price price){
      this.color=color;
      this.name=name;          
      this.price=price;  
  }        
  public void setColor(String color) {
    this.color = color;
  }
  public void setName(String name) {
    this.name = name;
  }
  public void setPrice(Price price){
    this.price=price;
  }
 public void mySaid(){
  System.out.println("我是"+name+",我的颜色是:"+color+",我的价格是:"+price.getPrice());  
  }
}    
bubuko.com,布布扣

这里需要用到Price类:

bubuko.com,布布扣
public class Price{

  private String price;    

  public void setPrice(String price){
    this.price=price;
  }
 public String getPrice(){
    return price;
  }
}    
bubuko.com,布布扣

于此同时,bean的配置应该改为:声明另一个bean(即price),并注入到Fruit中

bubuko.com,布布扣
<?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-2.5.xsd">
    <bean id="fruit" class="com.czh.Fruit">
     <!--下面是setter注入-->
        <property name="color" value="red"/>
        <property name="name">        
            <value>apple</value>
        </property>
        <property name="price">
            <ref bean="price"/><!-- 用ref的bean属性可以引用其他XML的bean -->
            <ref local="price"/><!-- 用ref的bean属性只能引用本XML的bean -->
        </property>
    <!--下面是构造器注入-->  
    <constructor-arg>
      <value>red</value>
       </constructor-arg>
       <constructor-arg>
          <value>red</value>
       </constructor-arg>
       <constructor-arg>
          <ref bean="price"/>
       </constructor-arg>
    </bean>

    <bean id="price" class="com.czh.Price">
        <property name="price" value="10"/>
    </bean>
</beans>    
bubuko.com,布布扣

从上面的配置中,应该可以很清楚看到,要引用bean只需要在property下用<ref>标签去获得bean的id即可。

当然,也可以这么写:<property name="price" ref="price">

还有另一个引用bean的方式,那就是:内部bean。内部bean的意思就是说直接把<bean id="price" class="com.czh.price">...</bean>写在Fruit Bean的property下。这样说可能不是很明白,那就看看下面的代码吧:(内部bean的缺点是,放在的Fruit Bean里面,则只有Fruit Bean可以引用它,其他的Bean是无法找到它的)

bubuko.com,布布扣
<?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-2.5.xsd">
    <bean id="fruit" class="com.czh.Fruit">
     <!--下面是setter注入-->
        ...
        <property name="price">
             <bean id="price" class="com.czh.Price">
                 <property name="price" value="10"/>
              </bean>
        </property>
    <!--下面是构造器注入-->  
   ...
       <constructor-arg>
              <bean id="price" class="com.czh.Price">
                <property name="price" value="10"/>
            </bean>
       </constructor-arg>
    </bean>
</beans>                
bubuko.com,布布扣

 

 最基本的注入配置已经了解了。接下来应该考虑的两个问题是:1、假如配置的时候忘记了注入怎么办,漏掉了几个property导致输出都是null却很难从那么多配置中一下找到写少的部分。2、每一个Bean都要自己配置和注入,有没有什么办法可以减少手工配置?

这两个问题的解决就要看“基于注解的配置:@Required和@Autowired"...

首先来看看@Requried,顺便理解一下注解在框架中起的作用

检查属性是否注入是上面所说要考虑的两个问题之一。在spring中可以使用依赖检查的特性来解决这个问题,依赖检查很简单,只需要在bean里添加一个dependency-check属性就可以了,具体的看看下面代码:

bubuko.com,布布扣
<?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-2.5.xsd">
    <bean id="fruit" class="com.czh.Fruit" dependency-check="all">
     <!--下面是setter注入-->
        ...
        <property name="price">
             <bean id="price" class="com.czh.Price">
                 <property name="price" value="10"/>
              </bean>
        </property>
    <!--下面是构造器注入-->  
   ...
       <constructor-arg>
              <bean id="price" class="com.czh.Price">
                <property name="price" value="10"/>
            </bean>
       </constructor-arg>
    </bean>
</beans>  
bubuko.com,布布扣
dependency-check="all"意味着对简单类型的注入,和对象的注入(即bean的引用)都进行检查。
dependency-check="simple"意味着对简单类型(int,string...)的注入进行检查。
dependency-check="object"意味着对 对象的注入(即bean的引用)进行检查。它们一旦检查未setter,就会抛出UnsatisfiedDenpendencyException。
不过这个dependency-check好像在spring 3.0中使用会报错,所以当基于注解的@Requried可以用于与它一样的作用的时候,就使用@Requried注解吧。
Spring的一个Bean后置处理器(RequiredAnnotationBeanPostProcessor)可以检查所有具有@Requried注解的属性是否被设置,可以继续上面水果类的例子看看这个@Requried如何放置,代码如下:
bubuko.com,布布扣
public class Fruit{
  private String color;
  private String name;
  private Price price;     //Price类

  public Fruit(){}
  public Fruit(String name,String color,Price price){
      this.color=color;
      this.name=name;          
      this.price=price;  
  }
  @Requried         
  public void setColor(String color) {
    this.color = color;
  }
  @Requried 
  public void setName(String name) {
    this.name = name;
  }
  @Requried 
  public void setPrice(Price price){
    this.price=price;
  }
 public void mySaid(){
  System.out.println("我是"+name+",我的颜色是:"+color+",我的价格是:"+price.getPrice());  
  }
}   
bubuko.com,布布扣

放置@Required注解很简单,像上面代码那样就可以了。Bean.xml那里还得做点功夫,不过也很简单,Spring 2.5以上的版本,我们只需要在xml中加上一句话就可以了,像这样:<context:annotation-config/>,不过还是看看代码吧,在beans那块加了些东西,否则会无法使用<context>标签。代码如下:

bubuko.com,布布扣
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="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/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd"
> <context:annotation-config/> ... </beans>
bubuko.com,布布扣

至此,就配置完成了,如果任何具有@Requried注解的属性为设置,就会抛出BeanInitializationException

 

之前所提出的第二个问题是:每一个Bean都要自己配置和注入,有没有什么办法可以减少手工配置?

这时候就要用到”自动装配",与@Requried注解一样,自动装配也可以通过注解来完成,即@Autowired。同样,在Spring 2.5版本之前,自动装配是由<bean>中autowire属性来完成的,大概像这样<bean id="fruit" class="com.czh.Fruit" autowire="byName">,就可以将 “与要注入的属性名相同的bean” 自动装配(也可以直接理解为自动setter),相应的还有autowire="byType",autowire="constructor"...我就不贴出代码详细说明了,直接来看看基于注解的自动装配。

 

先来看看@Autowired如何设置:

bubuko.com,布布扣
public class Fruit{
  private String color;
  private String name;
  private Price price;     //Price类

  public Fruit(){}
  public Fruit(String name,String color,Price price){
      this.color=color;
      this.name=name;          
      this.price=price;  
  }
     
  public void setColor(String color) {
    this.color = color;
  }
  
  public void setName(String name) {
    this.name = name;
  }
  @Autowired
  public void setPrice(Price price){
    this.price=price;
  }
 public void mySaid(){
  System.out.println("我是"+name+",我的颜色是:"+color+",我的价格是:"+price.getPrice());  
  }
}   
bubuko.com,布布扣

如上面代码所示,在setter或者构造器前加上@Autowired即可,Spring会去容器中查找与其setter类型兼容的组件,然后注入。

xml配置代码与@Required相同,加一个<context:annotation-config/>就可以了。

需要注意的一点是,@Autowired会自动检查是否被注入,效果与@Requried类似,如果发现未注入就会抛出异常

为了图方便,我们还可以这样做:(直接在字段上加@Autowired注解,这样就不用自己写setter方法了)

bubuko.com,布布扣
public class Fruit{
  private String color;
  private String name;

  @Autowired
  private Price price;     //Price类

  public Fruit(){}
  public Fruit(String name,String color,Price price){
      this.color=color;
      this.name=name;          
      this.price=price;  
  }
     
  public void setColor(String color) {
    this.color = color;
  }
  
  public void setName(String name) {
    this.name = name;
  }
  
 public void mySaid(){
  System.out.println("我是"+name+",我的颜色是:"+color+",我的价格是:"+price.getPrice());  
  }
} 
bubuko.com,布布扣

如果你想使用@Autowired注解,又想注入特定的组件,可以使用@Qualifier注解指定特定组件的名字。或者直接使用@Resource注解指定要注入组件的名字。具体的还是来看看代码:

bubuko.com,布布扣
public class Fruit{
  private String color;
  private String name;

  @Autowired
  @Qualifier("price");    
  private Price price;     //Price类
   ...
} 
bubuko.com,布布扣

 

基于注解的自动装配已经为我们减少了部分要亲手配置的代码...但是觉得这个还不够,还可以看看spring 2.5提供的又一个强大特性:自动扫描组件

它能够从classpath里自动扫描、侦测和实例化组件,它所侦测的是具有特定注解的组件,该注解为@Component。

看看代码吧:

bubuko.com,布布扣
@Component        
public class Fruit{
  private String color;
  private String name;

  @Autowired
  @Qualifier("price");    
  private Price price;     //Price类
   ...
} 
bubuko.com,布布扣

而在xml中的配置,也很简单,就像@Autowired那样只需要一句话:<context:component-scan base-package="com.czh.Fruit"/>

base-package指明了要扫描的类,各类之间可以用逗号分隔。

此时的xml中已经不需要手动配置各个组件的bean了,它们默认的id为首字母小写的类名(比如:fruit),代码如下:

bubuko.com,布布扣
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="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/beans 
                        http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
                        http://www.springframework.org/schema/context                                        
                        http://www.springframework.org/schema/context/spring-context-2.5.xsd">
    <context:annotation-config/>
    <context:component-scan base-package="com.czh.Fruit"/></beans> 
bubuko.com,布布扣

 @Component只是一般组件的注解,@Service和@Respository则分别指服务层组件和持久层组件,具体用法和@Component相同,即都只需要放置在要扫描的组件之前即可。

 

Spring中bean配置的学习笔记,布布扣,bubuko.com

Spring中bean配置的学习笔记

原文:http://www.cnblogs.com/czhfuture/p/3575777.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!