天天看點

spring Bean初始化方法:無參構造、有參構造、set注入

Bean初始化

  • ​​1.Bean的初始化方式:​​
  • ​​2.例子​​
  • ​​2.1建立一個空的spring項目​​
  • ​​2.2類檔案建立​​
  • ​​2.3java檔案​​
  • ​​2.4xml檔案​​
  • ​​2.5運作結果​​
  • ​​3.總結​​

1.Bean的初始化方式:

1.無參構造(預設構造方法);

2.有參構造(自定義構造方法);

3.set方法

2.例子

2.1建立一個空的spring項目

spring Bean初始化方法:無參構造、有參構造、set注入

其中:

client包是主方法所在類的包;

domain包是實體Bean的包;

resource包是xml配置檔案所在的包;

readme是項目介紹。

2.2類檔案建立

spring Bean初始化方法:無參構造、有參構造、set注入

2.3java檔案

package client;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import domain.People;

public class Main {

  public static void main(String[] args) {
    
    ApplicationContext applicationContext = new ClassPathXmlApplicationContext(
        "resource/*With.xml");
    People people = (People) applicationContext.getBean("people");
    people.say();
    
    applicationContext = new ClassPathXmlApplicationContext(
        "resource/*No.xml");
    people = (People) applicationContext.getBean("people");
    people.say();
    
    applicationContext = new ClassPathXmlApplicationContext(
        "resource/*Pro.xml");
    people = (People) applicationContext.getBean("people");
    people.say();
  }

}
      
package domain;

import java.io.Serializable;

public class People implements Serializable{

  /**
   * 
   */
  private static final long serialVersionUID = -3290777865940655640L;

  private String name;
  
  private String age;

  public void say(){
    System.out.println("say");
  }
  
  public People(String name,String age){
    this.name = name;
    this.age = age;
    System.out.println(this.getClass()+"有參構造");
  }
  
  public People(){
    System.out.println(this.getClass()+"無參構造");
  }
  
  public String getName() {
    return name;
  }

  public void setName(String name) {
    this.name = name;
  }

  public String getAge() {
    return age;
  }

  public void setAge(String age) {
    this.age = age;
  }
  
}
      

2.4xml檔案

beanNo.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.xsd">
  
  <bean id="people" class="domain.People">
  </bean>
  
</beans>      

beanPro.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.xsd">
  
  <bean id="people" class="domain.People">
    <property name="name">
      <value>xiaojua</value>
    </property>
    <property name="age">
      <value>23</value>
    </property>
  </bean>
  
</beans>      

beanWith.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.xsd">
  
  <bean id="people" class="domain.People">
    <constructor-arg name="name" type="java.lang.String">
      <value>xiaoming</value>
    </constructor-arg>
    <constructor-arg name="age" type="java.lang.String">
      <value>12</value>
    </constructor-arg>
  </bean>
  
</beans>      

2.5運作結果

class domain.People有參構造
say
class domain.People無參構造
say
class domain.People無參構造
say
      

3.總結