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>

 

  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

进朱者赤

多多支持

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值