1、DI
-
IoC(Inversion Of Control)控制翻转,Spring反向控制应用程序所需要使用的外部资源
-
DI(Dependency Injection)依赖注入,应用程序运行依赖的资源由Spring为其提供,资源进入应用程序的方式称为注入
IoC与DI的关系
-
IoC与DI是同一件事站在不同角度看待问题
2、set注入(主流)
-
名称:property
-
类型:标签
-
归属:bean标签
-
作用:使用set方法的形式为bean提供资源
- 格式:
<bean>
<property />
</bean> - 基本属性:
<property name="propertyName" value="propertyValue" ref="beanId"/>
name:对应bean中的属性名,要求该属性必须提供可访问的set方法(严格规范为此名称是set方法对应名称)
value(其他):设定非引用类型属性对应的值,不能与ref同时使用
ref(对象):设定引用类型属性对应bean的id ,不能与value同时使用
-
注意:一个bean可以有多个property标签
示例:
2.1、项目结构图
2.2、UserDao接口代码
package com.itheima.dao;
public interface UserDao {
void save();
}
2.3、UserDaoImpl实现类代码:
package com.itheima.dao.impl;
import com.itheima.dao.UserDao;
public class UserDaoImpl implements UserDao {
public void save() {
System.out.println("userDao running...");
}
}
2.4、UserService接口代码:
package com.itheima.service;
public interface UserService {
public void save();
}
2.5、UserServiceImpl实现类代码:
package com.itheima.service.impl;
import com.itheima.dao.UserDao;
import com.itheima.service.UserService;
public class UserServiceImpl implements UserService {
private UserDao userDao;
private int num;
private String version;
public void setVersion(String version) {
this.version = version;
}
public void setNum(int num) {
this.num = num;
}
public void setUserDao(UserDao userDao) {
this.userDao = userDao;
}
public void save() {
System.out.println("user service running..." + num + version);
userDao.save();
}
}
2.6、UserApp代码:
import com.itheima.service.UserService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class UserApp {
public static void main(String[] args) {
// 加载配置文件
ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
// 获取资源
UserService userService = (UserService) ctx.getBean("userService");
userService.save();
}
}
2.7、applicationContext.xml配置文件:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="userService" class="com.itheima.service.impl.UserServiceImpl">
<property name="userDao" ref="userDao"/>
<property name="num" value="9999"/>
<property name="version" value="我爱Java"/>
</bean>
<bean id="userDao" class="com.itheima.dao.impl.UserDaoImpl"/>
</beans>
2.8、运行效果:
3、构造器注入(了解)
-
名称:constructor-arg
-
类型:标签
-
归属:bean标签
-
作用:使用构造方法的形式为bean提供资源,兼容早期遗留系统的升级工作
- 格式:
<bean>
<constructor-arg />
</bean> - 基本属性:
<constructor-arg name="argsName" value="argsValue />
name:对应bean中的构造方法所携带的参数名
value:设定非引用类型构造方法参数对应的值,不能与ref同时使用
- 其他属性:
<constructor-arg index="arg-index" type="arg-type" ref="beanId"/>
ref:设定引用类型构造方法参数对应bean的id ,不能与value同时使用
type :设定构造方法参数的类型,用于按类型匹配参数或进行类型校验
index :设定构造方法参数的位置,用于按位置匹配参数,参数index值从0开始计数
-
注意:一个bean可以有多个constructor-arg标签
代码示例:
注:大部分代码与上面代码相同,所以本次只写出不同的代码部分。
3.1、UserServiceImpl实现类代码:
package com.itheima.service.impl;
import com.itheima.dao.UserDao;
import com.itheima.service.UserService;
public class UserServiceImpl implements UserService {
private UserDao userDao;
private int num;
private String version;
public UserServiceImpl(UserDao userDao, int num, String version) {
this.userDao = userDao;
this.num = num;
this.version = version;
}
public void save() {
System.out.println("user service running..." + num + version);
userDao.save();
}
}
3.2.applicationContext.xml配置文件:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="userDao" class="com.itheima.dao.impl.UserDaoImpl"/>
<bean id="userService" class="com.itheima.service.impl.UserServiceImpl">
<constructor-arg name="userDao" ref="userDao"/>
<constructor-arg name="num" value="66666"/>
<constructor-arg name="version" value="我爱Java"/>
</bean>
</beans>
通过改变以上这个代码,实现上面案例相同的功能。
4、集合类型数据注入
-
名称:array,list,set,map,props
-
类型:标签
-
归属:property标签 或 constructor-arg标签
-
作用:注入集合数据类型属性
- 格式:
<property>
<list></list>
</property>
(1)集合类型数据注入——list
<property name="al">
<list>
<value>itheima</value>
<value>66666</value>
</list>
</property>
(2)集合类型数据注入——props
<property name="properties">
<props>
<prop key="name">itheima666</prop>
<prop key="value">666666</prop>
</props>
</property>
(3)集合类型数据注入——array (了解)
<property name="arr">
<array>
<value>123456</value>
<value>66666</value>
</array>
</property>
(4)集合类型数据注入——set(了解)
<property name="hs">
<set>
<value>itheima</value>
<value>66666</value>
</set>
</property>
(5)集合类型数据注入——map(了解)
<property name="hm">
<map>
<entry key="name" value="itheima66666"/>
<entry key="value" value="6666666666"/>
</map>
</property>
代码示例:
注:大部分代码与上面代码相同,所以本次只写出不同的代码部分。
4.1、BookDao接口代码:
package com.itheima.dao;
public interface BookDao {
public void save();
}
4.2、BookDaoImpl实现类代码:
package com.itheima.dao.impl;
import com.itheima.dao.BookDao;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Properties;
public class BookDaoImpl implements BookDao {
private ArrayList al;
private Properties properties;
private int[] arr;
private HashSet hs;
private HashMap hm ;
public void setAl(ArrayList al) {
this.al = al;
}
public void setProperties(Properties properties) {
this.properties = properties;
}
public void setArr(int[] arr) {
this.arr = arr;
}
public void setHs(HashSet hs) {
this.hs = hs;
}
public void setHm(HashMap hm) {
this.hm = hm;
}
public void save() {
System.out.println("book dao running...");
System.out.println("ArrayList:"+al);
System.out.println("Properties:"+properties);
for (int i = 0; i < arr.length; i++) {
System.out.println(arr[i]);
}
System.out.println("HashSet:"+hs);
System.out.println("HashMap:"+hm);
}
}
4.3、UserServiceImpl实现类代码:
package com.itheima.service.impl;
import com.itheima.dao.BookDao;
import com.itheima.dao.UserDao;
import com.itheima.service.UserService;
public class UserServiceImpl implements UserService {
private BookDao bookDao;
public UserServiceImpl() {
}
public void setBookDao(BookDao bookDao) {
this.bookDao = bookDao;
}
public void save() {
system.out.println("running.....");
bookDao.save();
}
}
4.4、applicationContext.xml配置文件:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="userService" class="com.itheima.service.impl.UserServiceImpl">
<property name="bookDao" ref="bookDao"/>
</bean>
<bean id="bookDao" class="com.itheima.dao.impl.BookDaoImpl">
<property name="al">
<list>
<value>itheima</value>
<value>66666</value>
</list>
</property>
<property name="properties">
<props>
<prop key="name">itheima666</prop>
<prop key="value">666666</prop>
</props>
</property>
<property name="arr">
<array>
<value>123456</value>
<value>66666</value>
</array>
</property>
<property name="hs">
<set>
<value>itheima</value>
<value>66666</value>
</set>
</property>
<property name="hm">
<map>
<entry key="name" value="itheima66666"/>
<entry key="value" value="6666666666"/>
</map>
</property>
</bean>
</beans>