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之间的关系,然后使用容器上下文进行装配
阅读全文
0 0
- Spring实战4之DI(依赖注入)
- Spring入门之依赖注入(DI)
- Spring 之依赖注入DI
- spring依赖注入(DI)
- Spring 依赖注入(DI)
- spring-DI(依赖注入)
- Spring依赖注入(DI)
- Spring依赖注入(DI)
- Spring依赖注入(DI)
- Spring 依赖注入(DI)
- Spring依赖注入(DI)
- 【Java学习】spring入门之依赖注入(DI)
- Spring之依赖注入(IOC、DI)详解---配置Bean
- 8、spring依赖注入(DI)之参数歧义问题
- Spring DI(依赖注入)-之SETTER方法
- Spring 学习之 DI 依赖注入
- spring(依赖注入-DI)
- Spring DI 依赖注入
- 通过build.xml在Eclipse中导入工程
- MySQL触发器
- ubuntu终止进程
- 简短代码实现原生定位
- LLVM(clang)介绍
- Spring实战4之DI(依赖注入)
- 操作系统第二章 进程管理
- Swift学习
- 提交网址到Bing搜索引擎
- Java基础编程1—素数
- Linux GDB调试命令详解
- springmvc中的spring-servlet.xml 和 applicationContext.xml
- 课上习题的总结
- 《神探tcpdump第一招》