前回の記事では、単純な値を注入する方法を学びましたが、注入できるタイプはたくさんあるので、ここで試してみましょう。
例えば、2つのクラスを定義し、User.java
そしてUserAddress.java
:次の属性定義
でのUser.java
クラス:
private int age;
private String name;
private String[] hobbeys;
private List<String> cards;
private Map<String, String> girlfirends;
private Set<String> houses;
private UserAddress address;
次に、対応するset
合計get
メソッドを生成します。でUserAddress.java
定義:
private String address;
同様の生産set
、get
およびtoString
方法が可能です。
次に、対応するbeans.xml
ドキュメントの作成を開始します。公式ドキュメントを参照することをお勧め
します:https://docs.spring.io/spring-framework/docs/current/reference/html/core.html#beans-collection-elements forwriting便利なように、beans.xml
ファイルの上部を右クリックするSplit Vertically
と、効果は次のようになります。
より便利です。その後、開始できます。
<?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="weizu" class="com.weizu.pojo.User">
<property name="age" value="23"></property>
<property name="name" value="张三"></property>
<property name="hobbeys">
<array>
<value>抽烟</value>
<value>喝酒</value>
</array>
</property>
<property name="cards">
<list>
<value>123131231312</value>
<value>123131231312</value>
</list>
</property>
<property name="girlfirends">
<map>
<entry key="one" value="对象1"/>
<entry key="two" value="对象2"/>
</map>
</property>
<property name="houses">
<set>
<value>北京</value>
<value>上海</value>
</set>
</property>
<property name="address">
<null/>
</property>
</bean>
</beans>
テスト:
import com.weizu.pojo.User;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class myTest {
@Test
public void Test(){
// create and configure beans
ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
User userService = (User) context.getBean("weizu");
System.out.println(userService.toString());
}
}
結果:
参照ビデオ:https://www.bilibili.com/video/BV1WE411d7Dv?p = 9&spm_id_from = pageDriver
参照ドキュメント:https://docs.spring.io/spring-framework/docs/current/reference/html/core.html #beans-collection-elements
複数の異なるbeans-xxx.xml
ファイルがある場合は、合計を定義してXML
から、次のようにimport
インポートを使用できます。
<beans>
<import resource="services.xml"/>
<import resource="resources/messageSource.xml"/>
<import resource="/resources/themeSource.xml"/>
<bean id="bean1" class="..."/>
<bean id="bean2" class="..."/>
</beans>
services.xml
位置になければならないclasspath location
2つの後の位置になければならないが、resources
下に、これらのパスは、パスに対するものです。もちろん、このパスは、:file:C:/config/services.xml
やclasspath:/config/services.xml
。などの絶対パスとして指定できます。
ではSpring IoC
容器は、複数のホストすることができbean
、次のように、設定時に、データの一部を構成することができます。
class
資格、指定されたbean
カテゴリ。name
指定されたbean
インスタンス化名。scope
指定されたbean
アクションの範囲は、任意ありsingleton
、、 、prototype
、request
、、デフォルトでは、それぞれが表す単一の容器の存在、オブジェクト・インスタンスを表すことが可能有望な複数のインスタンスを、単一表す単一の要求インスタンスを、同様にまたへのシングルインスタンス;媒体内の単一インスタンスを意味します;媒体内の単一インスタンスを意味します;session
application
websocket
singleton
Spring IoC
bean
prototype
bean
request
HTTP
session
HTTP
Session
application
ServletContext
websocket
WebSocket
具体的な指定方法は以下のとおりです。
<bean id="accountService" class="com.something.DefaultAccountService" scope="singleton"/>