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

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作用基本一样,默认扫描当前文件所在包
 *
 */
@Component
public 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"})
 *
 */
@ComponentScan
public 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)使用构造器注入

@Component
public class Player {

    private Ball ball;

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

    public void play(){
        ball.play();
    }
}

(2)使用setter注入

@Component
public 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注解

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

    public void play(){
        ball.play();
    }
}

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

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值