装配Bean

①、装配wiring,即开立应用对象之间的同盟关系的行为,者也是借助注入的真面目。
2、创建Spring配置
从Sring三.0最先,Spring容器提供了二种配备Bean的措施:
XML文件配置方式
基于Java注明的布署形式
3、典型的xml配置文件:
XML 1

beans命名空间不是唯壹的Spring命名空间,Spring大旨框架自带了拾叁个命名空间陈设,如下:
XML 2

四、当Spring容器加载Bean时,Spring使用反射来成立Bean。

5、覆盖Spring私下认可的单例配置:Spring
Bean私下认可都以单例,但有时候我们每趟都需求获得唯壹的Bean实例,比如每种人的入场券要唯一:
我们只要求安顿Bean的scope属性为prototype即可:

<bean id="ticket" class="com....Ticket" scope="prototype" />

Spring提供的效应域选项:
XML 3

陆、开始化和销毁Bean
奥迪torium(舞台)要力保做的第三和终极两件业务:
开灯,关灯。
XML 4

需要在Bean中使用init-method和destory-method属性:
XML 5

当有无数上下文定义的Bean有一致名字的开首化方法和销毁方法时,能够直接在上层beans成分中宣示default-init-method和default-destory-method属性,从而防止每3个都要安装三回的题材。

示例:
一、程序结构:

XML 6

2、简单的Spring装配

Performer.java接口

//<start id="performer_java" /> 
package com.springinaction.springidol;
public interface Performer {
  void perform() throws PerformanceException;
}
//<end id="performer_java" /> 

Juggler.java实现类

//<start id="juggler_java" /> 
package com.springinaction.springidol;
public class Juggler implements Performer {
  private int beanBags = 3;
  public Juggler() {
  }
  public Juggler(int beanBags) {
    this.beanBags = beanBags;
  }
  public void perform() throws PerformanceException {
    System.out.println("JUGGLING " + beanBags + " BEANBAGS");
  }
}
//<end id="juggler_java" />

spring-idol.xml配置文件

<?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:util="http://www.springframework.org/schema/util"
 xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd">

  <bean id="duke" 
      class="com.springinaction.springidol.Juggler" />
</beans>

JugglerTest.java测试类

package com.springinaction.springidol;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
 * Created by LTN on 2016/4/17.
 */
public class JugglerTest {
    public static void main(String[] args) throws  Exception{
        ApplicationContext context =new  ClassPathXmlApplicationContext("com/springinaction/springidol/spring-idol.xml");
        Performer performer = (Performer) context.getBean("duke");
//        Performer performer = (Performer) context.getBean("poeticDuke");
        performer.perform();
    }
}

三、有参数的装配进度
PoeticJuggler.java
其构造器必要二个int和Poem的参数

//<start id="poeticjuggler_java" /> 
package com.springinaction.springidol;
public class PoeticJuggler extends Juggler {
  private Poem poem;
  public PoeticJuggler(Poem poem) { //<co id="co_injectPoem"/>
    super();
    this.poem = poem;
  }
  public PoeticJuggler(int beanBags, Poem poem) { // <co id="co_injectPoemBeanBags"/>
    super(beanBags);
    this.poem = poem;
  }
  public void perform() throws PerformanceException {
    super.perform();
    System.out.println("While reciting...");
    poem.recite();
  }
}
//<end id="poeticjuggler_java" />

参数在xml文件中,需求以下配置:

 <constructor-arg value="15" />
 <constructor-arg ref="sonnet29" />

value能够钦点基本项目;
ref是援引别的的bean定义。

Poem.java接口

//<start id="poem_java" /> 
package com.springinaction.springidol;
public interface Poem {
  void recite();
}
//<end id="poem_java" />

Sonnet29.java实现类

//<start id="sonnet29_java" /> 
package com.springinaction.springidol;
public class Sonnet29 implements Poem {
  private static String[] LINES = {
      "When, in disgrace with fortune and men's eyes,",
      "I all alone beweep my outcast state",
      "And trouble deaf heaven with my bootless cries",
      "And look upon myself and curse my fate,",
      "Wishing me like to one more rich in hope,",
      "Featured like him, like him with friends possess'd,",
      "Desiring this man's art and that man's scope,",
      "With what I most enjoy contented least;",
      "Yet in these thoughts myself almost despising,",
      "Haply I think on thee, and then my state,",
      "Like to the lark at break of day arising",
      "From sullen earth, sings hymns at heaven's gate;",
      "For thy sweet love remember'd such wealth brings",
      "That then I scorn to change my state with kings." };
  public Sonnet29() {
  }
  public void recite() {
    for (int i = 0; i < LINES.length; i++) {
      System.out.println(LINES[i]);
    }
  }
}
//<end id="sonnet29_java" />

spring-idol.java配置文件

<?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:util="http://www.springframework.org/schema/util"
 xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd">
  <!--<start id="duke_bean" />--> 
  <bean id="duke" 
      class="com.springinaction.springidol.Juggler" />
  <!--<end id="duke_bean" />-->
  <!--<start id="poeticduke_bean" />--> 
  <bean id="poeticDuke"
      class="com.springinaction.springidol.PoeticJuggler">
    <constructor-arg value="15" />
    <constructor-arg ref="sonnet29" />
  </bean>
  <!--<end id="poeticduke_bean" />-->

  <!--<start id="sonnet29_bean" />--> 
  <bean id="sonnet29"
      class="com.springinaction.springidol.Sonnet29" />
  <!--<end id="sonnet29_bean" />-->
</beans>

相关文章

网站地图xml地图