4.IOC建立對象方式

4.IOC建立對象方式

4.1 使用無參構造建立對象(默認)

  • 測試方式(經過給實體類設置有參構造)java

 public User(String name, Integer age) {
     this.name = name;
     this.age = age;
 }
  • 執行測試方法spring

 public class TestUser {
 
     public static void main(String[] args) {
         ApplicationContext applicationContext=new ClassPathXmlApplicationContext("applicationContext.xml");
 
         User user = applicationContext.getBean("user", User.class);
 
         System.out.println(user);
    }
 
 }
  • 測試結果app

  • 顯示初始化失敗函數

 Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'user' defined in class path resource [applicationContext.xml]: Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.xuan.pojo.User]: No default constructor found; nested exception is java.lang.NoSuchMethodException: com.xuan.pojo.User.<init>()
 Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.xuan.pojo.User]: No default constructor found; nested exception is java.lang.NoSuchMethodException: com.xuan.pojo.User.<init>()
  at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:83)
  at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateBean(AbstractAutowireCapableBeanFactory.java:1312)
  ... 13 more

 

 

 

4.2 使用有參構造建立對象

 

4.2.1 構造函數參數索引建立對象

  • 可使用該index屬性來明確指定構造函數參數的索引。測試

 public User(String name, Integer age) {
     this.name = name;
     this.age = age;
 }
 <bean id="user" class="com.xuan.pojo.User">
     <constructor-arg index="0" value="xuan"></constructor-arg>
     <constructor-arg index="1" value="22"></constructor-arg>
 </bean>

 

 

4.2.2 構造函數參數類型匹配建立對象(不建議使用,由於會存在都是String類型)

  • 經過使用type屬性顯式指定構造函數參數的類型,則容器可使用簡單類型的類型匹配。this

 private String name;
 private Integer age;
 
 public User(String name, Integer age) {
     this.name = name;
     this.age = age;
 }
 <bean id="user" class="com.xuan.pojo.User">
     <constructor-arg type="java.lang.String" value="xuan"></constructor-arg>
     <constructor-arg type="java.lang.Integer" value="22"></constructor-arg>
 </bean>

 

 

 

4.2.3 構造函數參數解析建立對象(經過引用或者直接參數進行賦值)下面以引用舉例

  • 構造函數參數解析匹配經過使用參數的類型進行。spa

  • 若是Bean定義的構造函數參數中不存在潛在的歧義(歧義也就是參數中的類不是繼承關聯的),則在實例化Bean時,在Bean定義中定義構造函數參數的順序就是將這些參數提供給適當的構造函數的順序。code

  • User類xml

 private Student student;
 private Teacher teacher;
 
 public User(Student student, Teacher teacher) {
     this.student = student;
     this.teacher = teacher;
 }
  • Student類對象

 private String name;
 private Integer age;
 
 public Student(String name, Integer age) {
     this.name = name;
     this.age = age;
 }
  • Teacher類

 private String name;
 private Integer age;
 
 public Student(String name, Integer age) {
     this.name = name;
     this.age = age;
 }
  • 主配置文件applicationContext.xml

  • 經過構造引用的方式將Student和Teacher類注入到User中 User又交給Spring建立,管理

 <bean id="user" class="com.xuan.pojo.User">
     <constructor-arg ref="student"></constructor-arg>
     <constructor-arg ref="teacher"></constructor-arg>
 </bean>
 
 
 <bean id="teacher" class="com.xuan.pojo.Teacher">
     <constructor-arg type="java.lang.String" value="jiaoshi"></constructor-arg>
     <constructor-arg type="java.lang.Integer" value="50"></constructor-arg>
 </bean>
 <bean id="student" class="com.xuan.pojo.Student">
     <constructor-arg type="java.lang.String" value="xuesheng"></constructor-arg>
     <constructor-arg type="java.lang.Integer" value="18"></constructor-arg>
 </bean>
  • 測試運行結果

 User{student=Student{name='xuesheng', age=18}, teacher=Student{name='jiaoshi', age=50}}

 

總結補充:直接參數進行賦值須要使用name屬性指定參數(其實掌握name纔是主要的)

相關文章
相關標籤/搜索