当前位置:必发365电子游戏 > 编程 > 必发365电子游戏Spring容器提供了三种配备Bean的方法,正视注入DI和面向切面编制程序AOP
必发365电子游戏Spring容器提供了三种配备Bean的方法,正视注入DI和面向切面编制程序AOP
2019-12-19

1、Spring的最首要特征:依赖注入DI和面向切面编制程序AOP。

装配Bean

1、装配wiring,即创建应用对象时期的合作关系的一言一行,者也是凭仗注入的庐山面目目。
2、创建Spring配置
从Sring3.0开始,Spring容器提供了三种配备Bean的办法:
XML文件配置情势
必发365电子游戏,基于Java证明的布局方式
3、标准的xml配置文件:
必发365电子游戏 1

beans命名空间不是当世无双的Spring命名空间,Spring宗旨框架自带了12个命名空间部署,如下:
必发365电子游戏 2

4、当Spring容器加载Bean时,Spring使用反射来创设Bean。

5、覆盖Spring暗中同意的单例配置:Spring Bean默许都是单例,但奇迹大家每一趟都亟待拿到唯生龙活虎的Bean实例,举个例子每个人的门票要唯大器晚成:
咱俩只须求配备Bean的scope属性为prototype即可:

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

Spring提供的作用域选项:
必发365电子游戏 3

6、开头化和销毁Bean
奥迪torium(舞台卡塔尔(قطر‎要保管做的第风姿罗曼蒂克和结尾两件职业:
开灯,关灯。
必发365电子游戏 4

需要在Bean中使用init-method和destory-method属性:
必发365电子游戏 5

当有那一个上下文定义的Bean有生龙活虎致名字的开头化方法和销毁方法时,能够一向在上层beans成分中扬言default-init-method和default-destory-method属性,进而防止每三个都要安装二回的难点。

示例:
1、程序构造:

必发365电子游戏 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();
    }
}

3、有参数的装配进度
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>

2、关键战略:
轻量级和细小侵入性编制程序:POJO
松耦合:DI和AOP
注明式编制程序:切面和规矩
调减样本式代码:切面和模板

3、Spring不会反逼你兑现Spring标准的接口或一而再再而三Spring规范的类。往往未有任何迹象申明你利用了Spring!

4、正视注入DI
装配(wiring):创制应用组件之间同盟的行为;
Spring的装配:有二种装配Bean的情势,最分布的是XML配置格局;
做事办法:Spring通过使用上下文(Application Context)装载、组装Bean;
Spring的上下文:有各个,差别在于怎么样加载它们的结构。常用的如ClassPathXmlApplicationContext,可用来加载坐落于系统classpath下的三个或多个XML文件。
(信任注入更加的多参照他事他说加以考察《Dependency Injection》,Dhanji LX570. Prasanna卡塔尔

5、应用切面AOP
DI让互相合营的软件组件保持松散耦合,而AOP编程允许你把分布应用处处的功能分离出来产生可选择的零器件。

6、通过“模板封装”扑灭样本式代码
卓绝的如Spring JdbcTemplate,使得在实施数据库操作时,防止守旧的JDBC样品式代码成为大概。

7、Bean
必发365电子游戏Spring容器提供了三种配备Bean的方法,正视注入DI和面向切面编制程序AOP。在依靠Spring的运用中,应用对象生活于Spring容器中。
容器:担负运用对象的创造、装配、配置、处理等总体生命周期,即肩负了从创设到销毁的历程。
Spring容器:Spring自带集中容器达成,可归咎为Bean工厂(bean factories)和使用上下文(application)两类,珍视应用的是运用上下文。
行使上下文:Spring首要的三种接受上下文:
ClassPathXMLApplicationContext——从类路线下的XML配置文件中加载上下文定义,把利用上下文定义文件作为类财富;
FileSystemXMLApplicationContext——读取文件系统下的XML配置文件并加载上下文定义;
XmlWebApplicationContext——读取Web应用下的XML配置文件并装载上下文;