Spring学习笔记[3] -- DI依赖注入
2018-03-03 23:18
483 查看
DI 概述
DI:Dependency Injection,依赖注入,在Spring框架负责创建Bean对象的时候,动态的将依赖对象注入到Bean组件中
入门程序
在IOC入门程序中,如果UserServiceImpl的实现类中有一个属性,那么使用Spring框架的IOC功能时,可以通过依赖注把该属性的值传递进来。
创建接口
public interface UserService { public void sayHello(); }
编写接口的实现类
public class UserServiceImpl implements UserService { private String name; public void setName(String name) { this.name = name; } public void sayHello(){ System.out.println("Demo01: Hello Spring "+name); } }
配置文件
在applicationContest.xml中使用
bean标签
<bean id="userService" class="gongfukangee.Demo01.UserServiceImpl"> <!-- 容器在创建 userService 对象的时候,同时给成员属性赋值 --> <property name="name" value="Jack"/> </bean>
创建测试类
/** * @Auther gongfukang *依赖注入 */ @org.junit.Test public void run4(){ ApplicationContext applicationContext=new ClassPathXmlApplicationContext("applicationContext.xml"); UserService userService=(UserService)applicationContext.getBean("userService"); userService.sayHello(); }
类成员变量注入
Setter方法注入
成4000
员属性注入
//实现类 public class UserServiceImpl implements UserService { private String name; public void setName(String name) { this.name = name; } }
<!-- xml配置 --> <bean id="userService" class="gongfukangee.Demo01.UserServiceImpl"> <property name="name" value="Jack"/> </bean>
引用变量注入
//引用变量实现类 public class CustomerDaoImpl { public void save(){ System.out.println("我是持久层 DAO ..."); } } //实现类 public class CustomerServiceImpl { //提供成员属性 private CustomerDaoImpl custDao; public void setCustDao(CustomerDaoImpl custDao) { this.custDao = custDao; } public void save(){ System.out.println("我是业务层 Service..."); custDao.save(); } }
<!-- xml配置 --> <bean id="customerDao" class="gongfukangee.Demo02.CustomerDaoImpl"/> <!-- customerDao 注入到 customerService --> <bean id="customerService" class="gongfukangee.Demo02.CustomerServiceImpl"> <!-- name: 与private 成员属性相同 values :字符串 ref :引用对象 --> <property name="custDao" ref="customerDao"/> </bean>
构造方法注入
成员属性//实现类 public class Car1 { private String carName; private double carPrice; public Car1(String carName,double carPrice){ super(); this.carName=carName; this.carPrice=carPrice; } }
<!-- xml配置 --> <bean id="car1" class="gongfukangee.Demo03.Car1"> <!-- 第一种注入方式 --> <constructor-arg name="carName" value="大众汽车"/> <constructor-arg name="carPrice" value="250000.01"/> <!-- 第二种注入方式 [index中,0代表构造方法中第一个成员变量] --> <constructor-arg index="0" value="奔驰汽车"/> <constructor-arg index="1" value="500000"/> </bean>
引用变量注入
//实现类 public class Person { private String PersonName; private Car1 car1; public Person(String personName, Car1 car1) { PersonName = personName; this.car1 = car1; } }
<!-- xml配置 --> <bean id="person" class="gongfukangee.Demo03.Person"> <constructor-arg name="personName" value="gong"/> <constructor-arg name="car1" ref="car1"/> </bean>
集合注入和配置文件注入
public class User { private String[] arrs; public void setArrs(String[] arrs) { this.arrs = arrs; } private List<String> list; public void setList(List<String> list) { this.list = list; } private Map<String,String> map; public void setMap(Map<String, String> map) { this.map = map; } //配置文件 private Properties pro; public void setPro(Properties pro) { this.pro = pro; } }
<!-- 注入集合 --> <bean id="user" class="gongfukangee.Demo03.User"> <!-- 注入数组 --> <property name="arrs"> <list> <value>哈哈</value> <value>呵呵</value> <value>嘿嘿</value> </list> </property> <!-- 注入List --> <!-- List 存放引用对象 <property name="list" > <list> <ref bean=""></ref> </list> </property> --> <property name="list" > <list> <value>小王</value> <value>张三</value> <value>李四</value> </list> </property> <!-- Map 注入 --> <property name="map"> <map> <entry key="AAA" value="刘邦"/> <entry key="BBB" value="关羽"/> </map> </property> <!-- Properties 属性文件注入 --> <property name="pro"> <props> <prop key="username">root</prop> <prop key="password">123456</prop> </props> </property> </bean>
相关文章推荐
- Spring 学习笔记(4)—— Bean 的基本配置、依赖注入(DI)、注入参数引用其他Bean
- Spring 4 学习笔记2:控制反转(IoC)和依赖注入(DI)
- spring学习笔记 -- day03 IOC中的bean标签、DI依赖注入
- spring 依赖注入DI-个人学习笔记
- Spring 4 学习笔记3:依赖注入(DI)
- [跟我学spring学习笔记][DI循环依赖]
- Spring 学习笔记 ----依赖注入
- Spring2.5学习笔记1-控制反转-依赖注入类型
- Spring学习笔记(6)----编码剖析Spring依赖注入的原理
- [跟我学spring学习笔记][DI循环依赖]
- java学习笔记之Spring依赖注入和控制反转
- Spring学习笔记——Spring依赖注入原理分析
- Spring 学习笔记 - IOC/依赖注入
- Spring 4 学习笔记6:依赖注入(配置)
- Asp.net core 学习笔记 ( DI 依赖注入 )
- ASP.NET Core 2.0系列学习笔记-DI依赖注入
- 【Spring学习笔记】依赖注入
- Spring学习笔记(二)依赖注入Dependency Injection
- spring 源码学习笔记(二)—— spring ioc 之依赖注入
- Spring学习笔记--依赖注入