发布于 2018-03-18 05:25:15 | 139 次阅读 | 评论: 0 | 来源: 网友投递

这里有新鲜出炉的精品教程,程序狗速度看过来!

Spring Framework 开源j2ee框架

Spring是什么呢?首先它是一个开源的项目,而且目前非常活跃;它是一个基于IOC和AOP的构架多层j2ee系统的框架,但它不强迫你必须在每一层 中必须使用Spring,因为它模块化的很好,允许你根据自己的需要选择使用它的某一个模块;它实现了很优雅的MVC,对不同的数据访问技术提供了统一的接口,采用IOC使得可以很容易的实现bean的装配,提供了简洁的AOP并据此实现Transcation Managment,等等


本篇文章主要介绍了Spring依赖注入的三种方式小结,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧

Spring的主要特性包括IOC和DI,其中DI是IOC的基础。在以前的Spring使用过程中大部分都是使用XML配置文件显式配置spring组件,导致大量的XML配置文件以及冗余的XML配置代码。阅读《Spring in Action》后总结Spring的DI功能的三种主要装配方式以及混合装配方式

根据注解自动装配

Spring中有非常丰富的注解,通过这些注解可以方便地配置Spring容器,使得Spring容器可以自动识别相关Bean并做自动注入装配。

使用注解

  • @Component注解:标注一个类,标识此类为一个Java Bean。
  • @Configuration注解:标注一个类,标识此类为一个Java配置类
  • @ComponentScan注解:标注一个类,用以配置扫描的包名称

示例代码

这里使用一个最简单的例子来展示自动装配的配置,代码结构如图所示。

ActionConfig类用于做全局配置,我们知道,一名骑士一般都会有一匹马,所以这里Horse和Knight类作为Java Bean被Spring容器创建,并将Horse类的一个bean注入到Knight中。所以在Horse和Knight中必须使用@Component注解进行修饰。

Knight代码:


package entities;

import org.springframework.stereotype.Component;

/**
 * Created by Administrator on 2017/8/3 0003.
 */
@Component
public class Knight {
  private Horse horse;

  Knight(Horse horse){
    this.horse = horse;
  }

  public void ride(){
    horse.bark();
  }
}

Horse代码:


package entities;

import org.springframework.stereotype.Component;

/**
 * Created by Administrator on 2017/8/3 0003.
 */
@Component
public class Horse {
  void bark(){
    System.out.println("Horse!!!!");
  }
}

ActionConfig代码:


package config;

import entities.Knight;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

/**
 * Created by Administrator on 2017/8/3 0003.
 */
@Configuration
@ComponentScan(basePackageClasses = {Knight.class})
public class ActionConfig {
}

这里的ComponentScan注解内的属性用以注明Knight类所在的包为扫描的基础包。凡是这个基础包及其子包内的标注了@Component的类都将被视为Java Bean,这些Bean被Spring容器创建和管理。

Java配置类装配

从自动装配的代码中,可以看出自动装配是非常简单方便的。Java配置类进行装配的方式是采用对配置类中的方法进行注解标注,实现bean的创建和管理。

还是采用以上的例子,现在我们将上面的自动装配改成Java配置类进行装配。

首先,删掉Knight和Horse中的@Component注解,删掉ActionConfig中的@ComponentScn注解,因为这些注解都是自动装配才需要的。


package entities;


/**
 * Created by Administrator on 2017/8/3 0003.
 */
public class Knight {
  private Horse horse;

  public Knight(Horse horse){
    this.horse = horse;
  }

  public void ride(){
    horse.bark();
  }
}


package entities;

/**
 * Created by Administrator on 2017/8/3 0003.
 */
public class Horse {
  void bark(){
    System.out.println("Horse!!!!");
  }
}

ActionConfig类:


package config;

import entities.Horse;
import entities.Knight;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * Created by Administrator on 2017/8/3 0003.
 */
@Configuration
public class ActionConfig {
  @Bean
  public Knight getKnight(){
    return new Knight(getHorse());
  }
  @Bean
  public Horse getHorse(){
    return new Horse();
  }
}

通过@Bean注解对相关的方法进行修饰,Spring就可以知道调用这些方法来构建相应的bean,并注入到依赖的对象中。

XML配置文件装配

使用XML配置文件来装配组件是最为繁琐的,需要对各个bean做一一配置,特别是需要配置构造器或者setter的参数。


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

</beans>

以上是一个空的XML配置文件,看一下就知道,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:aop="http://www.springframework.org/schema/aop"
    xmlns:context="http://www.springframework.org/schema/context" xmlns:c="http://www.springframework.org/schema/c"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
  <bean id="knight" class="entities.Knight">
    <constructor-arg ref="horse"></constructor-arg>
  </bean>
  
  <bean id="horse" class="entities.Horse"></bean>
</beans>

通过配置两个bean元素,并且通过constructor-arg元素来配置构造函数注入的bean,达到依赖注入的目的。

导入和混合配置

有时候单纯的XML配置或者Java配置不满足我们的需求怎么办呢?Spring支持混合配置的方式来管理bean,通过Java和XML配置的混合,达到你想要的效果。

Java配置中引用Java配置

在配置类中使用@Import注解进行引入另一个Java配置类。


package config;

import entities.Horse;
import entities.Knight;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;

/**
 * Created by Administrator on 2017/8/3 0003.
 */
@Configuration
@Import(BeautyConfig.class)
public class ActionConfig {
  @Bean
  public Knight getKnight(){
    return new Knight(getHorse());
  }
  @Bean(name = "horse")
  public Horse getHorse(){
    return new Horse();
  }
}

BeautyConfig代码:


package config;

import entities.Beauty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * Created by Administrator on 2017/8/3 0003.
 */
@Configuration
public class BeautyConfig {
  @Bean
  Beauty getBeauty(){
    return new Beauty();
  }
}

在Main中,只需要通过ActionConfig就可以获取到所有的上下文对象。


package main;

import config.ActionConfig;
import entities.Beauty;
import entities.Knight;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
/**
 * Created by Administrator on 2017/8/3 0003.
 */
public class Main {
  public static void main(String[] args) {
    AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(ActionConfig.class);
    Knight knight = context.getBean(Knight.class);
    knight.ride();
    Beauty beauty = context.getBean(Beauty.class);
    beauty.sleep();
  }
}

这里简单使用Import引入了另一个配置的Java文件,就实现了将多个bean分别配置到多个Java配置类中。

Java配置中引入XML配置

在Java中引入xml配置的基本方法与引入Java代码配置类似,只需要将@Import改为@ImportResource,配上xml的文件地址即可。


@ImportResource("classpath:spring.xml")

以上,就是Java配置spring依赖注入的方法。

XML配置中引入XML配置

同理,在XML配置中引入XML也需要使用Import,但是在XML中是import标签,通过使用import标签导入另一个xml文件。

这里我新建了一个another.xml的文件,用以作为另一个配置文件示例。在原有的spring.xml中加入以下代码:


<import resource="another.xml"></import>

这样就简单导入了anoter.xml配置文件。

XML配置中引入Java配置

乍看之下,在XML中似乎没有标签可以导入Java配置类的,不过任何Java配置类都是一个bean,所以可以通过配置bean标签来导入Java配置类!


<bean class="config.BeautyConfig"></bean>

通过在XML中配置这个Java配置类的bean,就直接导入了在Java配置类中的所有bean,真是神奇!

以下是配置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:aop="http://www.springframework.org/schema/aop"
    xmlns:context="http://www.springframework.org/schema/context" xmlns:c="http://www.springframework.org/schema/c"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
  <import resource="another.xml"></import>
  <bean class="config.BeautyConfig"></bean>
  <bean id="knight" class="entities.Knight">
    <constructor-arg ref="horse"></constructor-arg>
  </bean>

  <bean id="horse" class="entities.Horse"></bean>
</beans>

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持PHPERZ。



最新网友评论  共有(0)条评论 发布评论 返回顶部

Copyright © 2007-2017 PHPERZ.COM All Rights Reserved   冀ICP备14009818号  版权声明  广告服务