最近、Springで依存性注入の方法を学び、メモを取りました。クラスAは、コード内の他のオブジェクトによって呼び出されます。以前の方法では、呼び出された場所にnewを介してクラスAのインスタンスを作成し、setメソッドを介してクラスAオブジェクトのプロパティに値を割り当てると、面倒になります。メンテナンス。オブジェクトをインスタンス化するためのSpringIocコンテナを学ぶことは非常に便利です。次に、xml構成ファイルを介して依存性注入の方法を記録します。
準備:最初にMavenプロジェクトを作成し、パッケージをビルドしてパッケージ名から自由に開始し、UserServiceインターフェイスとUserServiceImplインターフェイス実装クラスを記述します。
UserService:
/*
* 保存用户接口
* */
public interface UserService {
/*
* 保存用户
* */
void saveUser();
}
pomファイル:
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.2.8.RELEASE</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<dependency>
</dependencies>
1.パラメーター構築メソッドを使用した属性インジェクション:
UserServiceImpl:
public class UserServiceImp implements UserService {
private String name;
private Integer age;
private Date brithday;
//通过有参构造方法为对象属性赋值
public UserServiceImpl(String name, Integer age, Date brithday) {
this.name = name;
this.age = age;
this.brithday = brithday;
}
public void saveUser() {
System.out.println("saveUser方法执行了");
System.out.println("姓名:" + name);
System.out.println("年龄:" + age);
System.out.println("生日:" + brithday);
}
}
bean.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
http://www.springframework.org/schema/beans/spring-beans.xsd">
<!--构造方法注入
constructor-arg标签:
属性:
index:指定参数在构造函数参数列表的索引位置
type:指定参数在构造函数中的数据类型
name:指定参数在构造函数中的名称用这个找给谁赋值
value:它能赋的值是基本数据类型和String类型
ref:它能赋的值是其他bean类型,也就是说必须得是在配置文件中配置过的bean
-->
<bean id="userService" class="com.zy.service.impl.UserServiceImpl">
<constructor-arg name="name" value="小杨"></constructor-arg>
<constructor-arg name="age" value="20"></constructor-arg>
<constructor-arg name="brithday" ref="date"></constructor-arg>
</bean>
<bean id="date" class="java.util.Date"></bean>
</beans>
テストクラスと実行結果:
public class TestDemo01 {
public static void main(String[] args) {
//加载配置文件
ApplicationContext context = new ClassPathXmlApplicationContext("bean.xml");
//根据bean标签的id获取实例对象
UserService userService = (UserService) context.getBean("userService");
//调用方法
userService.saveUser();
}
}
2.メソッド属性インジェクションの設定(一般的に使用)
UserServiceImplは次のコードに変更されます。
public class UserServiceImpl implements UserService {
private String name;
private Integer age;
private Date brithday;
public void setName(String name) {
this.name = name;
}
public void setAge(Integer age) {
this.age = age;
}
public void setBrithday(Date brithday) {
this.brithday = brithday;
}
public void saveUser() {
System.out.println("saveUser方法执行了");
System.out.println("姓名:" + name);
System.out.println("年龄:" + age);
System.out.println("生日:" + brithday);
}
}
bean.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
http://www.springframework.org/schema/beans/spring-beans.xsd">
<!--
set方法注入:常用
标签:property
属性:
name:找的是类中set方法后面的部分
ref:给属性赋值是其他bean类型的
value:给属性赋值是基本数据类型和string类型的
-->
<bean id="userService" class="com.zy.service.impl.UserServiceImpl">
<property name="name" value="小张"/>
<property name="age" value="20"/>
<property name="brithday" ref="date"/>
</bean>
<bean id="date" class="java.util.Date"></bean>
</beans>
テストコードは上記と同じで、実行結果は次のとおりです
。3。p名前空間属性インジェクション(本質はsetメソッドを呼び出すことであり、一般的には使用されません)
UserServiceImpl:
public class UserServiceImpl implements UserService {
private String name;
private Integer age;
private Date brithday;
public void setName(String name) {
this.name = name;
}
public void setAge(Integer age) {
this.age = age;
}
public void setBrithday(Date brithday) {
this.brithday = brithday;
}
public void saveUser() {
System.out.println("saveUser方法执行了");
System.out.println("姓名:" + name);
System.out.println("年龄:" + age);
System.out.println("生日:" + brithday);
}
}
bean.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
http://www.springframework.org/schema/beans/spring-beans.xsd">
<!--
p名称空间注入:不常用
-->
<bean id="userService" class="com.zy.service.impl.UserServiceImpl"
p:name="小李" p:age="20" p:brithday-ref="date">
</bean>
<bean id="date" class="java.util.Date"></bean>
</beans>
実行結果:
4。SpEL属性インジェクション(一般的には使用されません):
SpEL:Spring式言語、Springの式言語。
構文:#{SpEL}
public class UserServiceImpl implements UserService {
private String name;
private Integer age;
private Date brithday;
public void setName(String name) {
this.name = name;
}
public void setAge(Integer age) {
this.age = age;
}
public void setBrithday(Date brithday) {
this.brithday = brithday;
}
public void saveUser() {
System.out.println("saveUser方法执行了");
System.out.println("姓名:" + name);
System.out.println("年龄:" + age);
System.out.println("生日:" + brithday);
}
}
bean.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
http://www.springframework.org/schema/beans/spring-beans.xsd">
<!--
SpEL注入:不常用
-->
<bean id="userService" class="com.zy.service.impl.UserServiceImpl">
<property name="name" value="#{'小明'}"></property>
<property name="age" value="#{23}"></property>
<property name="brithday" ref="#{'date'}"></property>
</bean>
<bean id="date" class="java.util.Date"></bean>
</beans>
手術結果:
さあ、男の子