创建对象的方式:
- 无参构造函数创建对象
- 带参数的构造函数创建对象
- 一般参数
- 参数是其他类的对象
- 参数是集合类型
- 工厂创建对象
- 静态方法创建对象
- 非静态方法创建对象
- 静态方法创建对象
示例工程:
代码:
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:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<!--
使用bean节点来创建对象
id属性标识着对象
name属性代表着要创建对象的类全名
-->
<bean id="user" class="com.bean.User"/>
<bean id="person" class="com.bean.Person"/>
<bean id="userWithParams" class="com.bean.User">
<!-- 通过constructor这个节点来指定构造函数的参数类型、名称、第几个 -->
<constructor-arg index="0" name="id" type="java.lang.String" value="1"></constructor-arg>
<constructor-arg index="1" name="username" type="java.lang.String" value="yue"></constructor-arg>
<!-- 如果参数是对象,需要用ref引用 -->
<constructor-arg index="2" name="person" type="com.bean.Person" ref="person"></constructor-arg>
</bean>
<!-- 工厂静态方法创建对象 -->
<bean id="userByFactory" class="com.factory.Factory" factory-method="getBeanByStatic" >
</bean>
<!-- 工厂非静态方法创建对象 -->
<bean id="factory" class="com.factory.Factory"/>
<bean id="userByFactory2" class="com.bean.User" factory-bean="factory" factory-method="getBean" >
</bean>
<!-- 参数为集合 -->
<bean id="personWithParams" class="com.bean.Person">
<property name="lists">
<list>
<value>list1</value>
<value>list2</value>
<value>list3</value>
</list>
</property>
<property name="sets">
<set>
<value>set1</value>
<value>set2</value>
<value>set3</value>
</set>
</property>
<property name="maps">
<map>
<entry key="1" value="map1" />
<entry key="2" value="map2" />
</map>
</property>
</bean>
</beans>
User.java
package com.bean;
public class User {
private String id;
private String username;
private Person person;
public User() {}
public User(String id, String username, Person person) {
this.id = id;
this.username = username;
this.person = person;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public Person getPerson() {
return person;
}
public void setPerson(Person person) {
this.person = person;
}
}
Person.java
package com.bean;
import java.util.List;
import java.util.Map;
import java.util.Set;
public class Person {
public String personID = "1";
private List<String> lists;
private Set<String> sets;
private Map<String, String> maps;
public Person() {}
public Person(String personID, List<String> lists) {
this.personID = personID;
this.setLists(lists);
}
public Map<String, String> getMaps() {
return maps;
}
public void setMaps(Map<String, String> maps) {
this.maps = maps;
}
public Set<String> getSets() {
return sets;
}
public void setSets(Set<String> sets) {
this.sets = sets;
}
public List<String> getLists() {
return lists;
}
public void setLists(List<String> lists) {
this.lists = lists;
}
}
Factory.java
package com.factory;
import com.bean.User;
public class Factory {
public static User getBeanByStatic() {
return new User();
}
public User getBean() {
return new User();
}
}
TestMain.java
package com.main;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.bean.User;
public class TestMain {
public static void main(String[] args) {
// TODO Auto-generated method stub
ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
User user = (User)ac.getBean("user");
User user2 = (User)ac.getBean("userWithParams");
System.out.println(user);
System.out.println(user2);
System.out.println(user2.getUsername());
System.out.println(user2.getPerson().personID);
User user3 = (User)ac.getBean("userByFactory");
System.out.println(user3);
User user4 = (User)ac.getBean("userByFactory2");
System.out.println(user4);
Person person = (Person)ac.getBean("personWithParams");
for (String list: person.getLists()) {
System.out.println(list);
}
for (String set: person.getSets()) {
System.out.println(set);
}
for (Entry<String, String> entry: person.getMaps().entrySet()) {
System.out.println(entry);
}
}
}