02 Beanオブジェクト以外のオブジェクト注入(それほど一般的ではない)
4688 ワード
1 Type.java
2ガイド
3 applicationContext.xml
4テスト
package action;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
public class Type {
private String msg;
private int size;
private String[] types;
private List<String> words;
private Set<String> mySet;
private Map<String, Object> myMap;
private Properties props;
public void show() {
System.out.println("-------msg");
System.out.println(msg);
System.out.println("-------size");
System.out.println(size);
// , , , split()
System.out.println("-------types");
for (String s : types) {
System.out.println(s);
}
System.out.println("-------words");
for (String s : words) {
System.out.println(s);
}
System.out.println("-------mySet");
for (String s : mySet) {
System.out.println(s);
}
System.out.println("-------myMap");
Set<String> keySet = myMap.keySet();
for (String s : keySet) {
System.out.println(s + " " + myMap.get(s));
}
System.out.println("-------props");
Set<Object> set = props.keySet();
for (Object o : set) {
System.out.println(o + " " + props.getProperty(o.toString()));
}
}
public void setMsg(String msg) {
this.msg = msg;
}
public void setSize(int size) {
this.size = size;
}
public void setTypes(String[] types) {
this.types = types;
}
public void setWords(List<String> words) {
this.words = words;
}
public void setMySet(Set<String> mySet) {
this.mySet = mySet;
}
public void setMyMap(Map<String, Object> myMap) {
this.myMap = myMap;
}
public void setProps(Properties props) {
this.props = props;
}
}
2ガイド
3 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:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:jee="http://www.springframework.org/schema/jee"
xsi:schemaLocation="
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-2.5.xsd">
<bean id="type" class="action.Type">
<property name="msg" value=" "></property>
<property name="size" value="10"></property>
<property name="types" value="jpeg.gif,png,txt,docx,avi"></property>
<property name="words">
<list>
<value>abc</value>
<value>def</value>
<value>ghi</value>
</list>
</property>
<property name="mySet">
<set>
<value>Dexter</value>
<value>Aki</value>
</set>
</property>
<property name="myMap">
<map>
<entry key="1" value="a"></entry>
<entry key="2" value="b"></entry>
<entry key="3" value="c"></entry>
</map>
</property>
<property name="props">
<props>
<prop key="username">Scott</prop>
<prop key="password">Tiger</prop>
<prop key="driver">oracle.jdbc.driver.OracleDriver</prop>
<prop key="url">jdbc:oracle:thin:@localhost:1521:orcl</prop>
</props>
</property>
</bean>
</beans>
4テスト
package test;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import action.Type;
public class Demo {
@Test
public void test() {
String conf = "applicationContext.xml";
ApplicationContext ac = new ClassPathXmlApplicationContext(conf);
Type t = (Type) ac.getBean("type");
t.show();
}
}