Spring Bean注入

Spring Bean注入

Spring Bean注入

首先创建Bean类-People。

public class People {
	private int id;
	private String name;
	public People() {
		super();
		System.out.println("people 构造方法");
		// TODO Auto-generated constructor stub
	}
	public People(int id, String name) {
		super();
		this.id = id;
		this.name = name;
		System.out.println("people 有参构造方法");
	}
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	@Override
	public String toString() {
		return "People [id=" + id + ", name=" + name + "]";
	}
}
  • Spring Bean注入方式有两种:

    • 通过构造方法设置值

    • 设置注入(通过set方法)

      如果属性是基本数据类型或String等。则设置如下:

      
      	
      	
      
      

      或者:

      <bean id="peo3" class="com.spring.People">
      		<property name="id">
      			<value>456value>
      		property>
      		<property name="name">
      			<value>youvalue>
      		property>
      bean>
      

      以上两种配置实现的效果都是一样的。

      测试代码如下:

      public class Test {
          public static void main(String [] args) {
              ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
    
              People people = ac.getBean("peo3",People.class);
    
              System.out.println(people);
          }
      }
    

    ​ 如果属性是set,则设置如下:

    <bean id="peo4" class="com.spring.People">
    		<property name="id">
    			<value>456value>
    		property>
    		<property name="name">
    			<value>youvalue>
    		property>
    		<property name="sets">
    			<set>
    				<value>1value>
    				<value>2value>
    				<value>3value>
    				<value>4value>
    			set>
    		property>
    bean>
    

    ​ 测试代码如下:

     public class Test {
          public static void main(String [] args) {
              ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
    
              People people = ac.getBean("peo4",People.class);
    
              System.out.println(people);
          }
      }
    

    如果属性是list的话。则设置如下:

    <bean id="peo4" class="com.spring.People">
    		<property name="id">
    			<value>456value>
    		property>
    		<property name="name">
    			<value>youvalue>
    		property>
    		<property name="sets">
    			<set>
    				<value>1value>
    				<value>2value>
    				<value>3value>
    				<value>4value>
    			set>
    		property>
    		<property name="lists">
    			<list>
    				<value>list1value>
    				<value>list2value>
    				<value>list3value>
    			list>
    		property>
    	bean>
    

    如果list只有一个值,可以用以下配置:

    <property name="lists" value="list123">property>
    

    测试代码如下:

     public class Test {
          public static void main(String [] args) {
              ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
    
              People people = ac.getBean("peo4",People.class);
    
              System.out.println(people);
          }
      }
    
    

    如果属性是数组的话。则设置如下:

    <bean id="peo4" class="com.spring.People">
    		<property name="id">
    			<value>456value>
    		property>
    		<property name="name">
    			<value>youvalue>
    		property>
    		<property name="sets">
    			<set>
    				<value>1value>
    				<value>2value>
    				<value>3value>
    				<value>4value>
    			set>
    		property>
    		<property name="lists">
    			<list>
    				<value>list1value>
    				<value>list2value>
    				<value>list3value>
    			list>
    		property>
    		
    		<property name="strs">
    			<array>
    				<value>array1value>
    				<value>array2value>
    				<value>array3value>
    			array>
    		property>
    bean>
    
    

    如果数组中只有一个值,可以用以下配置:

    <property name="strs" value="array1">property>
    
    

    如果属性是map的话。则设置如下:

    <bean id="peo4" class="com.spring.People">
    		<property name="id">
    			<value>456value>
    		property>
    		<property name="name">
    			<value>youvalue>
    		property>
    		<property name="map">
    			<map>
    				<entry key="1" value="map1">
    				entry>
    				<entry key="2" value="map2">
    				entry>
    			map>
    		property>
    bean>
    
    

    测试代码同上。

​ 如果属性是Properties的话。则设置如下:

<property name="demo">
			<props>
				<prop key="prop1">prop1prop>
				<prop key="prop2">prop2prop>
			props>
property>

你可能感兴趣的:(spring)