Spring Dependency Injection (DI)
来源:互联网 发布:可以手写的软件 编辑:程序博客网 时间:2024/05/16 23:02
In Spring frameowork, Dependency Injection (DI) design pattern is used to define the object dependencies between each other. It exits in two major types :
- Setter Injection
- Constructor Injection
1. Setter Injection
This is the most popular and simple DI method, it will injects the dependency via a setter method.
Example
A helper class with a setter method.
package com.mkyong.output;import com.mkyong.output.IOutputGenerator;public class OutputHelper{ IOutputGenerator outputGenerator; public void setOutputGenerator(IOutputGenerator outputGenerator){ this.outputGenerator = outputGenerator; }}
A bean configuration file to declare the beans and set the dependency via setter injection (property
tag).
<beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-2.5.xsd"> <bean id="OutputHelper" class="com.mkyong.output.OutputHelper"> <property name="outputGenerator"> <ref bean="CsvOutputGenerator" /> </property> </bean><bean id="CsvOutputGenerator" class="com.mkyong.output.impl.CsvOutputGenerator" /><bean id="JsonOutputGenerator" class="com.mkyong.output.impl.JsonOutputGenerator" /></beans>
You just injects a ‘CsvOutputGenerator
’ bean into ‘OutputHelper
’ object via a setter method (setOutputGenerator
).
2. Constructor Injection
This DI method will injects the dependency via a constructor.
Example
A helper class with a constructor.
package com.mkyong.output;import com.mkyong.output.IOutputGenerator;public class OutputHelper{ IOutputGenerator outputGenerator; OutputHelper(IOutputGenerator outputGenerator){ this.outputGenerator = outputGenerator; }}
A bean configuration file to declare the beans and set the dependency via constructor injection (constructor-arg
tag).
<beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-2.5.xsd"> <bean id="OutputHelper" class="com.mkyong.output.OutputHelper"> <constructor-arg> <bean class="com.mkyong.output.impl.CsvOutputGenerator" /> </constructor-arg> </bean><bean id="CsvOutputGenerator" class="com.mkyong.output.impl.CsvOutputGenerator" /><bean id="JsonOutputGenerator" class="com.mkyong.output.impl.JsonOutputGenerator" /></beans>
You just injects a ‘CsvOutputGenerator
’ bean into ‘OutputHelper
’ object via a constructor.
Setter or Constructor injection?
There are no hard rule set by Spring framework, just use whatever type of DI that suit your project needs. However, due to the simplicity of the setter injection, it’s always selected for most of the scenarios.
- Spring Dependency Injection (DI)
- Spring Dependency Injection (DI)
- Spring(2)-Spring Dependency Injection (DI)
- DI, Dependency Injection
- IoC--Inversion of Contro and DI---Dependency Injection(Spring)
- Spring学习四:依赖注入DI(Dependency Injection)
- Spring之注入类型(DI:dependency injection)
- 再谈angularjs DI(Dependency Injection)
- 依赖注入 Dependency Injection (DI)
- Spring 3 Dependency injection
- Ioc模式(又称DI:Dependency Injection)
- Ioc模式(又称DI:Dependency Injection)
- Ioc概念(又称DI:Dependency Injection)
- Ioc模式(又称DI:Dependency Injection)
- Ioc概念(又称DI:Dependency Injection)
- IOC(DI)- Inversion of Control(Dependency Injection)
- 依赖注入DI(Dependency Injection)--属性注入
- Spring, DIP, dependency inversin, dependency injection
- ORA-01034: ORACLE not available
- codeforces 567F mausoleum dp
- HBase表设计——Column Family列族的设计数量不宜过多
- PostgreSQL使用pg_basebackup搭建主备流复制环境
- 欢迎使用CSDN-markdown编辑器
- Spring Dependency Injection (DI)
- xStream完美转换XML、JSON
- 使用Unity3D的50个技巧:Unity3D最佳实践
- Spring mvc 的基本使用说明
- greenDao_2.0.0学习笔记3-Hello World
- SAP EP7.0 LoginModule JAAS 自定义登录控制
- Android C层如何加载.so库文件
- Bone Collector 2602 (01背包)(模板)
- tomcat启动时报:IOException while loading persisted sessions: java.io.EOFException的解决方案