这里有新鲜出炉的精品教程, 程序狗速度看过来!
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>
来源: http://www.phperz.com/article/18/0318/355015.html