您的位置:首页 > 编程语言 > Java开发

spring中给属性赋值的三种方式

2019-06-04 14:22 302 查看


Main.java
 

[code]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

[code]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

[code]<?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

[code]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

[code]<?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>

 

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: