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>
分类:
# Spring
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· winform 绘制太阳,地球,月球 运作规律
· AI与.NET技术实操系列(五):向量存储与相似性搜索在 .NET 中的实现
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 上周热点回顾(3.3-3.9)