Spring实战4之DI(依赖注入)

来源:互联网 发布:bluegriffon mac 编辑:程序博客网 时间:2024/05/16 06:39

Spring容器创建应用对象之间的协作关系称为装配。下面介绍最常见的三种装配方法。

一、自动化装配bean
1.组建扫描(component scanning):Spring会自动发现应用上下文中所创建的bean。
(1)使用@Component注解标记要被实例的bean

/** * 球类接口 * @author shier * */public interface Ball {    /**     * 提供一个玩的方法     */    public void play();}/** * 篮球类Basketball实现Ball球类的接口 * 重写play()方法 * @Component注解可以添加值来给bean制定名称,@Name与@Component作用基本一样,默认扫描当前文件所在包 * */@Componentpublic class Basketball implements Ball{    @Override    public void play() {        System.out.println("篮球使用双手击打!!!");    }}

(2)开启组件扫描的功能
使用javaConfig方式开启组件扫描的功能

/** * java配置类,定义Spring 的装配规则 * @author shier * */@Configuration/** * Spring中@ComponentScan注解启用组件扫描,@ComponentScan注解 * 后可以写入值或者使用basePackages属性设置组件扫描的基础包 * 例如,@ComponentScan("cn.edu.sy"),@ComponentScan(basePackages={"cn.edu.sy","basketball"}) * */@ComponentScanpublic class BallConfig {    @Bean    public Basketball getBall(){        return new Basketball();    }}

使用XML文件配置启用组件扫描

<?xml version="1.1" 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"    xmlns:mvc="http://www.springframework.org/schema/mvc"    xmlns:aop="http://www.springframework.org/schema/aop"    xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd">    <!-- 自动扫描且只扫描@Controller -->    <**context:component-scan** base-package="cn.htd.xdpt" use-default-filters="false">        <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>        <context:include-filter type="annotation" expression="org.springframework.web.bind.annotation.ControllerAdvice"/>    </context:component-scan></beans>

2.自动装配(autowiring):Spring自动满足bean之间的依赖。
通过为bean添加@Autowired注解实现自动装配
(1)使用构造器注入

@Componentpublic class Player {    private Ball ball;    /**     * 在构造器上面使用@Autowired注解,构造器注入,在构造器中注入会使代码紧耦合     * @param ball     */    @Autowired    public Player(Ball ball) {        this.ball = ball;    }    public void play(){        ball.play();    }}

(2)使用setter注入

@Componentpublic class Player {    private Ball ball;      public Ball getBall() {        return ball;    }    /**     * 在set方法上面使用@Autowired注解,setter注入,setter注入的使用使得代码松耦合     * @param ball     */    @Autowired    public void setBall(Ball ball) {        this.ball = ball;    }    public void play(){        ball.play();    }}

(3)在成员变量上面使用@Autowired注解

@Componentpublic class Player {    /**     * 在成员变量上面使用@Autowired注解,不需要在定义get和set方法     * @param ball     */    @Autowired    private Ball ball;      public void play(){        ball.play();    }}

二、使用javaConfig装配bean
主要创建一个配置类,然后声明@Bean方法产生实例化bean

三、使用xml配置文件装配bean
主要使用XML配置文件组织bean之间的关系,然后使用容器上下文进行装配

原创粉丝点击