XML 配置
Person.java
package com.hy.spring.beans.collections;
import java.util.List;
import com.hy.spring.beans.Car;
public class Person {
private String name;
private int age;
private List cars;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public List getCars() {
return cars;
}
public void setCars(List cars) {
this.cars = cars;
}
@Override
public String toString() {
return "Person [name=" + name + ", age=" + age + ", cars=" + cars + "]";
}
}
XML 配置
NewPerson.java
package com.hy.spring.beans.collections;
import java.util.Map;
import com.hy.spring.beans.Car;
public class NewPerson {
private String name;
private int age;
private Map cars;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public Map getCars() {
return cars;
}
public void setCars(Map cars) {
this.cars = cars;
}
@Override
public String toString() {
return "Person1 [name=" + name + ", age=" + age + ", cars=" + cars
+ "]";
}
}
XML 配置
root
1234
jdbc:mysql://test
com.mysql.jdbc.Driver
DataSource.java
package com.hy.spring.beans.collections;
import java.util.Properties;
public class DataSource {
private Properties properties;
public Properties getProperties() {
return properties;
}
public void setProperties(Properties properties) {
this.properties = properties;
}
@Override
public String toString() {
return "DataSource [properties=" + properties + "]";
}
}
Main.java
package com.hy.spring.beans.collections;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Main {
public static void main(String[] args) {
ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
Person person = (Person) ctx.getBean("person3");
System.out.println(person);
NewPerson newPerson = (NewPerson) ctx.getBean("person4");
System.out.println(newPerson);
DataSource dataSource = (DataSource) ctx.getBean("datasource");
System.out.println(dataSource);
}
}
XML 配置
Main.java
package com.hy.spring.beans.collections;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Main {
public static void main(String[] args) {
ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
Person person = (Person) ctx.getBean("person3");
System.out.println(person);
NewPerson newPerson = (NewPerson) ctx.getBean("person4");
System.out.println(newPerson);
DataSource dataSource = (DataSource) ctx.getBean("datasource");
System.out.println(dataSource);
Person newPerson1 = (Person) ctx.getBean("person5");
System.out.println(newPerson1);
}
}
XML 配置
p:name="TING" p:age="29" p:cars-ref="cars">