Spring控制反转IOC:基于XML装配Bean
控制反转IoC的总介绍:
学习笔记(04):轻松搞定Spring全家桶(初识篇)-控制反转IOC:
1.构造方法注入:
(1)在类中添加构造函数
(2)在xml中,在bean标签下用constructor-arg标签设置参数。
(3)index属性确定引用的位置,如果引用的是基本数据类型,用value传值;如果引用的是bean对象,则用ref传递
(4)简单程序案例
①UserModel.java
package com.ioc;
public class UserModel {
private String id;
private String name;
private String sex;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
//添加构造方法
public UserModel(String id, String name, String sex) {
this.id = id;
this.name = name;
this.sex = sex;
}
}
②UserService.java
package com.ioc;
public class UserService {
private UserModel userModel;
public UserModel getUserModel() {
return userModel;
}
public void setUserModel(UserModel userModel) {
this.userModel = userModel;
}
public void findUser() {
System.out.println("-------user name:"+userModel.getName());
}
//添加构造方法
public UserService(UserModel userModel) {
this.userModel = userModel;
}
}
③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-4.3.xsd">
<!-- 构造方法注入 -->
<bean id = "userModel" class="com.ioc.UserModel">
<constructor-arg index = "0" value = "Tom123"/>
<constructor-arg index = "1" value = "Tom"/>
<constructor-arg index = "2" value = "man"/>
</bean>
<bean id = "userService" class="com.ioc.UserService">
<constructor-arg ref = "userModel"/>
</bean>
</beans>
④TestUserService.java
package com.ioc;
import org.junit.jupiter.api.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
class TestUserService {
@Test
void testFindUser() {
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
UserService userService = context.getBean("userService",UserService.class);//配置文件中定义的id,类的数据类型
userService.findUser();
}
}
2.属性setter注入:
(1)在xml文件中,在bean标 签下用property标签设置属性。
(2)必须用name属性确定引用的位置,如果引用的是基本数据类型,用value传值;如果引用的是bean对象,则用ref传递
(3)简单程序案例:
①UserModel.java
package com.ioc;
public class UserModel {
private String id;
private String name;
private String sex;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
/*
* //添加构造方法 public UserModel(String id, String name, String sex) { this.id = id;
* this.name = name; this.sex = sex; }
*/
}
②UserService.java
package com.ioc;
public class UserService {
private UserModel userModel;
public UserModel getUserModel() {
return userModel;
}
public void setUserModel(UserModel userModel) {
this.userModel = userModel;
}
public void findUser() {
System.out.println("-------user name:"+userModel.getName());
}
/*
* public UserService(UserModel userModel) { this.userModel = userModel; }
*/
}
③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-4.3.xsd">
<!-- 属性setter注入 -->
<bean id = "userModel" class="com.ioc.UserModel">
<property name="id" value="t123"/>
<property name="name" value="Tom2"/>
<property name="sex" value="man"/>
</bean>
<bean id = "userService" class="com.ioc.UserService">
<property name="userModel" ref = "userModel"/>
</bean>
</beans>
④TestUserService.java
package com.ioc;
import org.junit.jupiter.api.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
class TestUserService {
@Test
void testFindUser() {
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
UserService userService = context.getBean("userService",UserService.class);//配置文件中定义的id,类的数据类型
userService.findUser();
}
}
3.使用c标签、p标签装配:
注:简化代码量,使用前注意在beans标签属性中加上这两个标签的namespace,而且p标签和c标签是直接作为bean的属性
(1)c标签:替换构造函数注入
c:<属性名>= “xxx” 引入常量值
c:<属性名>-ref= “xxx” 引用其他bean对象
(2)p标签:替代属性setter注入中的property
p:<属性名>= “xxx” 引入常量值
p:<属性名>-ref= “xxx” 引用其他bean对象
例子:2.的代码中applicationContext.xml改,两者对比代码量
<?xml version="1.0" encoding="UTF-8"?>
<!-- xmlns的意思是xmlnamespace -->
<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"
xmlns:c="http://www.springframework.org/schema/c"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.3.xsd">
<!-- p标签属性setter注入 -->
<bean id = "userModel" class="com.ioc.UserModel" p:id="t123" p:name="Tom3" p:sex="man"/>
<bean id = "userService" class="com.ioc.UserService" p:userModel-ref="userModel"/>
</beans>
4.注入值中有特殊字符:
使用转义序列表示这些字符,或者使用<![CDATA[任意内容]]>这个特殊标签。(任意内容可以包含特殊字符)
5.集合的装配:
详见:学习笔记(06):轻松搞定Spring全家桶(初识篇)-控制反转IOC:集合装配
6.表达式的装配:
详见:学习笔记(07):轻松搞定Spring全家桶(初识篇)-控制反转IOC:装配表达式
以上所有例子所需要的jar包(点击下载)