[Spring学習ノート2]各タイプの属性map,list,array,null,propertiesを組み立てます.
37229 ワード
.spring Ioc (1)
Spring Ioc DI( ):
: setter( )/ ( )/ field( )/ out
: <property>,<constructor-arg>,@Autowired,@Resource/
1. :
A、 value value
<bean id="person" class="com.lspring.step2.Person">
<property name="name" value="liyy"></property>
<!-- property , value -->
<property name="age" >
<value>30</value>
</property>
<property name="tel"><value>ABCD</value></property>
<!-- value Spring -->
<property name="homePage"><value>http://google.com</value></property>
</bean>
B、 bean, ref ref
<!--ref bean,local bean,parent bean,bean , -->
<property name="parent">
<ref bean="person_c"></ref>
</property>
C、 bean
<property name="parent">
<bean class="com.lspring.step2.Person">
<constructor-arg value="Jerry"></constructor-arg>
<constructor-arg>
<value>33</value>
</constructor-arg>
</bean>
</property>
D、 /
1).
:
private String[] favs;
public String[] getFavs() {
return favs;
}
public void setFavs(String[] favs) {
this.favs = favs;
}
xml :
<!-- -->
<property name="favs">
<array>
<value> </value>
<value> </value>
<value> </value>
</array>
</property>
2). list
private List<String> school;
public List<String> getSchool() {
return school;
}
public void setSchool(List<String> school) {
this.school = school;
}
<!-- list -->
<property name="school">
<list>
<value> </value>
<value> </value>
<value> </value>
</list>
</property>
3). set ,
private Set<String> cities;
public Set<String> getCities() {
return cities;
}
public void setCities(Set<String> cities) {
this.cities = cities;
}
<property name="cities">
<set>
<value>shanghai</value>
<value>shanghai</value>
<value>shanghai</value>
</set>
</property>
E. Map
private Map<String, Double> scores;
public Map<String, Double> getScores() {
return scores;
}
public void setScores(Map<String, Double> scores) {
this.scores = scores;
}
<!-- Map -->
<property name="scores">
<map>
<entry key=" " value="50"></entry>
<entry key=" " value="30"></entry>
<!-- key-ref, value-ref bean -->
</map>
</property>
F. properties
private Properties properties;
public Properties getProperties() {
return properties;
}
public void setProperties(Properties properties) {
this.properties = properties;
}
<!-- -->
<!-- <property name="properties"> <props> <prop key="qq">1213343</prop>
<prop key="msn">[email protected]</prop> </props> </property> -->
<!-- value -->
<property name="properties">
<value>
qq=133234
msn=1312@dfjk.com
</value>
</property>
G.
private Integer age=25;
age
<bean id="person_null" class="com.lspring.step2.Person">
<property name="name" value=" "></property>
<property name="age">
<!-- nul 。 -->
<null></null>
</property>
</bean>
コードは以下の通りです.Person.java PersonDao.java
package com.lspring.step2;
import java.net.URL;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
import java.util.Vector;
public class Person {
private String name;
private Integer age=25;
private String tel;
private Person parent;
private String[] favs;
private List<String> school = new Vector();
private Set<String> cities;
private Properties properties;
public Properties getProperties() {
return properties;
}
public void setProperties(Properties properties) {
this.properties = properties;
}
private Map<String, Double> scores;
public Map<String, Double> getScores() {
return scores;
}
public void setScores(Map<String, Double> scores) {
this.scores = scores;
}
public Set<String> getCities() {
return cities;
}
public void setCities(Set<String> cities) {
this.cities = cities;
}
public List<String> getSchool() {
return school;
}
public void setSchool(List<String> school) {
this.school = school;
}
public String[] getFavs() {
return favs;
}
public void setFavs(String[] favs) {
this.favs = favs;
}
private URL homePage;
public URL getHomePage() {
return homePage;
}
public void setHomePage(URL homePage) {
this.homePage = homePage;
}
public String getName() {
return name;
}
public Person(String name, Integer age) {
super();
this.name = name;
this.age = age;
}
public Person() {
super();
System.out.println(" person!");
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public String getTel() {
return tel;
}
public void setTel(String tel) {
this.tel = tel;
}
public Person getParent() {
return parent;
}
public void setParent(Person parent) {
this.parent = parent;
}
@Override
public String toString() {
return "Person [name=" + name + ", age=" + age + ", tel=" + tel + ", parent=" + parent + ", favs=" + Arrays.toString(favs) + ", school=" + school + ", cities=" + cities + ", properties=" + properties + ", scores=" + scores + ", homePage=" + homePage + "]";
}
}
package com.lspring.step2;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class PersonDao {
@Test
public void injectTest(){
@SuppressWarnings("resource")
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("beans_step2.xml");
System.out.println(applicationContext.getBean("person"));
System.out.println(applicationContext.getBean("person_c"));
Person person = (Person) applicationContext.getBean("person");
//Spring ,
System.out.println(person.getSchool().getClass());
System.out.println("person_null:"+applicationContext.getBean("person_null"));
}
}
xmlファイルは以下の通りです.beans_step 2.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-3.2.xsd">
<bean id="person_c" class="com.lspring.step2.Person">
<constructor-arg>
<value>Tom</value>
</constructor-arg>
<constructor-arg value="22"></constructor-arg>
<property name="parent">
<bean class="com.lspring.step2.Person">
<constructor-arg value="Jerry"></constructor-arg>
<constructor-arg>
<value>33</value>
</constructor-arg>
</bean>
</property>
</bean>
<bean id="person" class="com.lspring.step2.Person">
<property name="name" value="liyy"></property>
<!-- property , value -->
<property name="age">
<value>30</value>
</property>
<property name="tel">
<value>ABCD</value>
</property>
<!-- value Spring -->
<property name="homePage">
<value>http://google.com</value>
</property>
<!-- 1. <property name="parent" ref="person_c"></property>=2. <property
name="parent"><ref bean="person_c"/></property> -->
<!--ref bean,local bean,parent bean,bean , -->
<property name="parent">
<ref bean="person_c"></ref>
</property>
<!-- -->
<property name="favs">
<array>
<value> </value>
<value> </value>
<value> </value>
</array>
</property>
<!-- list -->
<property name="school">
<list>
<value> </value>
<value> </value>
<value> </value>
</list>
</property>
<!-- set -->
<property name="cities">
<set>
<value>shanghai</value>
<value>shanghai</value>
<value>shanghai</value>
</set>
</property>
<!-- Map -->
<property name="scores">
<map>
<entry key=" " value="50"></entry>
<entry key=" " value="30"></entry>
<!-- key-ref, value-ref bean -->
</map>
</property>
<!-- -->
<!-- <property name="properties"> <props> <prop key="qq">1213343</prop>
<prop key="msn">[email protected]</prop> </props> </property> -->
<!-- value -->
<property name="properties">
<value>
qq=133234
[email protected]
</value>
</property>
</bean>
<bean id="person_null" class="com.lspring.step2.Person">
<property name="name" value=" "></property>
<property name="age">
<!-- nul 。 -->
<null></null>
</property>
</bean>
</beans>