创建对象的方式:

  • 无参构造函数创建对象
  • 带参数的构造函数创建对象
    • 一般参数
    • 参数是其他类的对象
    • 参数是集合类型
  • 工厂创建对象
    • 静态方法创建对象
    • 非静态方法创建对象


示例工程:

图片.png

代码:

applicationContext.xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xmlns:p="http://www.springframework.org/schema/p"
  5. xmlns:context="http://www.springframework.org/schema/context"
  6. xsi:schemaLocation="
  7. http://www.springframework.org/schema/beans
  8. http://www.springframework.org/schema/beans/spring-beans.xsd
  9. http://www.springframework.org/schema/context
  10. http://www.springframework.org/schema/context/spring-context.xsd">
  11. <!--
  12. 使用bean节点来创建对象
  13. id属性标识着对象
  14. name属性代表着要创建对象的类全名
  15. -->
  16. <bean id="user" class="com.bean.User"/>
  17. <bean id="person" class="com.bean.Person"/>
  18. <bean id="userWithParams" class="com.bean.User">
  19. <!-- 通过constructor这个节点来指定构造函数的参数类型、名称、第几个 -->
  20. <constructor-arg index="0" name="id" type="java.lang.String" value="1"></constructor-arg>
  21. <constructor-arg index="1" name="username" type="java.lang.String" value="yue"></constructor-arg>
  22. <!-- 如果参数是对象,需要用ref引用 -->
  23. <constructor-arg index="2" name="person" type="com.bean.Person" ref="person"></constructor-arg>
  24. </bean>
  25. <!-- 工厂静态方法创建对象 -->
  26. <bean id="userByFactory" class="com.factory.Factory" factory-method="getBeanByStatic" >
  27. </bean>
  28. <!-- 工厂非静态方法创建对象 -->
  29. <bean id="factory" class="com.factory.Factory"/>
  30. <bean id="userByFactory2" class="com.bean.User" factory-bean="factory" factory-method="getBean" >
  31. </bean>
  32. <!-- 参数为集合 -->
  33. <bean id="personWithParams" class="com.bean.Person">
  34. <property name="lists">
  35. <list>
  36. <value>list1</value>
  37. <value>list2</value>
  38. <value>list3</value>
  39. </list>
  40. </property>
  41. <property name="sets">
  42. <set>
  43. <value>set1</value>
  44. <value>set2</value>
  45. <value>set3</value>
  46. </set>
  47. </property>
  48. <property name="maps">
  49. <map>
  50. <entry key="1" value="map1" />
  51. <entry key="2" value="map2" />
  52. </map>
  53. </property>
  54. </bean>
  55. </beans>

User.java

  1. package com.bean;
  2. public class User {
  3. private String id;
  4. private String username;
  5. private Person person;
  6. public User() {}
  7. public User(String id, String username, Person person) {
  8. this.id = id;
  9. this.username = username;
  10. this.person = person;
  11. }
  12. public String getId() {
  13. return id;
  14. }
  15. public void setId(String id) {
  16. this.id = id;
  17. }
  18. public String getUsername() {
  19. return username;
  20. }
  21. public void setUsername(String username) {
  22. this.username = username;
  23. }
  24. public Person getPerson() {
  25. return person;
  26. }
  27. public void setPerson(Person person) {
  28. this.person = person;
  29. }
  30. }

Person.java

  1. package com.bean;
  2. import java.util.List;
  3. import java.util.Map;
  4. import java.util.Set;
  5. public class Person {
  6. public String personID = "1";
  7. private List<String> lists;
  8. private Set<String> sets;
  9. private Map<String, String> maps;
  10. public Person() {}
  11. public Person(String personID, List<String> lists) {
  12. this.personID = personID;
  13. this.setLists(lists);
  14. }
  15. public Map<String, String> getMaps() {
  16. return maps;
  17. }
  18. public void setMaps(Map<String, String> maps) {
  19. this.maps = maps;
  20. }
  21. public Set<String> getSets() {
  22. return sets;
  23. }
  24. public void setSets(Set<String> sets) {
  25. this.sets = sets;
  26. }
  27. public List<String> getLists() {
  28. return lists;
  29. }
  30. public void setLists(List<String> lists) {
  31. this.lists = lists;
  32. }
  33. }

Factory.java

  1. package com.factory;
  2. import com.bean.User;
  3. public class Factory {
  4. public static User getBeanByStatic() {
  5. return new User();
  6. }
  7. public User getBean() {
  8. return new User();
  9. }
  10. }

TestMain.java

  1. package com.main;
  2. import org.springframework.context.ApplicationContext;
  3. import org.springframework.context.support.ClassPathXmlApplicationContext;
  4. import com.bean.User;
  5. public class TestMain {
  6. public static void main(String[] args) {
  7. // TODO Auto-generated method stub
  8. ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
  9. User user = (User)ac.getBean("user");
  10. User user2 = (User)ac.getBean("userWithParams");
  11. System.out.println(user);
  12. System.out.println(user2);
  13. System.out.println(user2.getUsername());
  14. System.out.println(user2.getPerson().personID);
  15. User user3 = (User)ac.getBean("userByFactory");
  16. System.out.println(user3);
  17. User user4 = (User)ac.getBean("userByFactory2");
  18. System.out.println(user4);
  19. Person person = (Person)ac.getBean("personWithParams");
  20. for (String list: person.getLists()) {
  21. System.out.println(list);
  22. }
  23. for (String set: person.getSets()) {
  24. System.out.println(set);
  25. }
  26. for (Entry<String, String> entry: person.getMaps().entrySet()) {
  27. System.out.println(entry);
  28. }
  29. }
  30. }

运行结果:

图片.png