依存性注入(DI)
- 依存性注入(依存性注入、DI)。
- 依存性:Beanのオブジェクトを作成するために参照するコンテナリソースに依存するBeanオブジェクトに依存します。
- 注入は:Beanオブジェクトがコンテナおよびアセンブリによって設定された依存リソース、であることを意味します。
コンストラクタ・インジェクション
私たちは03を詳細に話をしている前に、
settet注射(フォーカス)
プロパティの中に注入するように求めプロパティはboolean型である場合、あなたは、資本セット+イニシャルプロパティの方法により、setメソッド、メソッド名を設定する必要があり、決まった方法が、そこにされているではありません。
pojo类
public class Address {
private String address;
public void setAddress(String address) {
this.address = address;
}
@Override
public String toString() {
return "Address{" +
"address='" + address + '\'' +
'}';
}
}
public class Student {
private String name;
private Address address;
private String[] books;
private List<String> hobbies;
private Map<String,String> card;
private Set<String> games;
private String wife;
private Properties info;
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", address=" + address.toString() +
", books=" + Arrays.toString(books) +
", hobbys=" + hobbies +
", card=" + card +
", games=" + games +
", wife='" + wife + '\'' +
", info=" + info +
'}';
}
public void setName(String name) {
this.name = name;
}
public void setAddress(Address address) {
this.address = address;
}
public void setBooks(String[] books) {
this.books = books;
}
public void setHobbies(List<String> hobbies) {
this.hobbies = hobbies;
}
public void setCard(Map<String, String> card) {
this.card = card;
}
public void setGames(Set<String> games) {
this.games = games;
}
public void setWife(String wife) {
this.wife = wife;
}
public void setInfo(Properties info) {
this.info = info;
}
}
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
https://www.springframework.org/schema/beans/spring-beans.xsd">
<!--常量注入-->
<bean id="addr" class="com.cong.pojo.Address">
<property name="address" value="耀华里***"/>
</bean>
<bean id="cong" class="com.cong.pojo.Student">
<property name="name" value="cong"/>
<!--bean注入,用ref-->
<property name="address" ref="addr"/>
<!--数组注入-->
<property name="books">
<array>
<value>红楼梦</value>
<value>水浒传</value>
<value>西游记</value>
<value>三国演义</value>
</array>
</property>
<!--list注入-->
<property name="hobbies">
<list>
<value>听歌</value>
<value>码字</value>
<value>看电影</value>
</list>
</property>
<!--map注入-->
<property name="card">
<map>
<entry key="idCard" value="4408*******************"/>
<entry key="countCard" value="6201***************"/>
<entry key="studentCard" value="201*****"/>
</map>
</property>
<!--set注入-->
<property name="games">
<set>
<value>lol</value>
<value>魔兽</value>
<value>刀塔</value>
</set>
</property>
<!--null注入-->
<property name="wife">
<null/>
</property>
<!--properties注入-->
<property name="info">
<props>
<prop key="姓名">rainbow</prop>
<prop key="学号">2014****</prop>
<prop key="性别">female</prop>
</props>
</property>
</bean>
</beans>
テストカテゴリ
public class TestDI {
@Test
public void test1(){
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
Student cong = (Student) context.getBean("cong");
System.out.println(cong.toString());
}
}
結果
Student{
name='cong',
address=Address{address='耀华里***'},
books=[红楼梦, 水浒传, 西游记, 三国演义], hobbys=[听歌, 码字, 看电影],
card={idCard=4408*******************,
countCard=6201***************,
studentCard=201*****},
games=[lol, 魔兽, 刀塔],
wife='null',
info={学号=2014****, 性别=female, 姓名=rainbow}
}
拡張名前空間の注入
pojo类
package com.cong.pojo;
public class User {
private String name;
private int age;
public User() {
}
public User(String name, int age) {
this.name = name;
this.age = age;
}
public void setName(String name) {
this.name = name;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "User{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
}
beans.xmlの
インポートの制約に注意
xmlns:P = "http://www.springframework.org/schema/p"
のxmlns:C = "http://www.springframework.org/schema/c"
<?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"
xmlns:c="http://www.springframework.org/schema/c"
xsi:schemaLocation="http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd">
<!--P(属性: properties)命名空间 , 属性依然要设置set方法-->
<bean id="user" class="com.cong.pojo.User" p:name="cong" p:age="3"/>
<!--C(构造: Constructor)命名空间 , 属性依然要设置set方法-->
<bean id="user2" class="com.cong.pojo.User" c:name ="cong2" c:age="3"/>
</beans>
テストカテゴリ
@Test
public void test2(){
ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
User user = (User) context.getBean("user");
System.out.println(user.toString());
User user2 = (User) context.getBean("user2");
System.out.println(user2.toString());
}