spring中給屬性賦值的三種方式


Main.java
 

package com.vow.spring;

import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Main {
 public static void main(String[] args) {
	        //1. 創建 Spring 的 IOC 容器
			ClassPathXmlApplicationContext ctx =
					new ClassPathXmlApplicationContext("applicationContext.xml");
			//2. 從 IOC 容器中獲取 bean 的實例
			HelloWorld helloWorld = (HelloWorld) ctx.getBean("helloworld");
			//3. 使用 bean

			helloWorld.printf();
	}
}


方式一:通過構造函數給屬性賦值

      1) 通過構造函數 

               value:屬性值,在沒有設置index和type時,設置value值的類型和value的順序要和構造函數參數類型的順序一致

               index:設置參數在構造函數參數中的索引,從0開始

               type  :設置參數的類型,寫全限定名
ps:  可混合使用
HelloWorld。java

package com.vow.spring;

public class HelloWorld {

	private String name;
	private int age;
	private String sex;
	public String getName() {
		return name;
	}

	public HelloWorld(String name, int age, String sex) {
		super();
		this.name = name;
		this.age = age;
		this.sex = sex;
	}
	public void printf()
	{
		System.out.println(name);
		System.out.println(age);
		System.out.println(sex);
	}

}

      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"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  <!-- 配置一個 bean --> 

   <bean id="helloworld" class="com.vow.spring.HelloWorld">
   <constructor-arg value="vow2"></constructor-arg>
   <constructor-arg value="19"></constructor-arg>
   <constructor-arg value="男"></constructor-arg>
   </bean>
</beans>


結果:

方式二:通過set方法給屬性賦值(在spring的bean中使用<property>標籤)


HelloWorld.java

package com.vow.spring;

public class HelloWorld {

	private String name;
	private int age;
	private String sex;
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}
	public String getSex() {
		return sex;
	}
	public void setSex(String sex) {
		this.sex = sex;
	}
	
	public void printf()
	{
		System.out.println(name);
		System.out.println(age);
		System.out.println(sex);
	}

}



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"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  <!-- 配置一個 bean --> 
  <bean id="helloWorld" class="com.vow.spring.HelloWorld" > 
    <爲屬性賦值 > 
    <property name="name" value="vow"></property>
    <property name="age" value="18"> </property> 
    <property name="sex" value="男"> </property> 
   </bean>  
</beans>


結果:

方式三:通過p命名空間給屬性賦值

<!-- 給對象屬性注入值: # p 名稱空間給對象的屬性注入值 (spring3.0以上版本才支持) -->
    <!-- p名稱空間優化後 -->
    <bean id="user" class="cn.itcast.c_property.User" p:name="Jack" p:id="123"></bean>

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章