spring——Spring Bean属性注入——setter 注入 Dear 丶 2024-03-31 17:23 36阅读 0赞 ### setter 注入 ### 我们可以通过 Bean 的 setter 方法,将属性值注入到 Bean 的属性中。 在 Spring 实例化 Bean 的过程中,IoC 容器首先会调用默认的构造方法(无参构造方法)实例化 Bean(Java 对象),然后通过 Java 的反射机制调用这个 Bean 的 setXxx() 方法,将属性值注入到 Bean 中。 使用 setter 注入的方式进行属性注入,大致步骤如下: 1. 1. **在 Bean 中提供一个默认的无参构造函数(在没有其他带参构造函数的情况下,可省略),并为所有需要注入的属性提供一个 setXxx() 方法;** 2. 在 Spring 的 XML 配置文件中,使用 <beans> 及其子元素 <bean> 对 Bean 进行定义; 3. 在 <bean> 元素内使用 <property> 元素对各个属性进行赋值。 项目依赖: <?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>org.example</groupId> <artifactId>ssw</artifactId> <version>1.0-SNAPSHOT</version> <properties> <maven.compiler.source>8</maven.compiler.source> <maven.compiler.target>8</maven.compiler.target> </properties> <dependencies> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>5.2.5.RELEASE</version> </dependency> </dependencies> </project> ![9c5520241685532fd73f3973e87d6fa6.png][] studeng类: package org.example; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; public class Student { private static final Log LOGGER = LogFactory.getLog(Student.class); private int id; private String name; private Grade grade; //无参构造方法,在没有其他带参构造方法的情况下,可以省略 public Student() { } //id 属性的 setter 方法 public void setId(int id) { LOGGER.info("正在执行 Student 类的 setId() 方法…… "); this.id = id; } //name 属性的 setter 方法 public void setName(String name) { LOGGER.info("正在执行 Student 类的 setName() 方法…… "); this.name = name; } public void setGrade(Grade grade) { LOGGER.info("正在执行 Student 类的 setGrade() 方法…… "); this.grade = grade; } @Override public String toString() { return "Student{" + "id=" + id + ", name='" + name + '\'' + ", grade=" + grade + '}'; } } ![47b8952dfe01bd7df5ab55de1fa8ef79.png][] grade类: package org.example; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; public class Grade { private static final Log LOGGER = LogFactory.getLog(Grade.class); private Integer gradeId; private String gradeName; /** * 无参构造函数 * 若该类中不存在其他的带参构造函数,则这个默认的无参构造函数可以省略 */ public Grade() { } public void setGradeId(Integer gradeId) { LOGGER.info("正在执行 Grade 类的 setGradeId() 方法…… "); this.gradeId = gradeId; } public void setGradeName(String gradeName) { LOGGER.info("正在执行 Grade 类的 setGradeName() 方法…… "); this.gradeName = gradeName; } @Override public String toString() { return "Grade{" + "gradeId=" + gradeId + ", gradeName='" + gradeName + '\'' + '}'; } } ![0f9784e1e9c1c829478d8b878eaf97e4.png][] bean.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 https://www.springframework.org/schema/beans/spring-beans.xsd"> <!-- <bean id="helloWorld" class="org.example.HelloWorld">--> <!-- <property name="message" value="Hello World!——china"/>--> <!-- </bean>--> <bean id="student" class="org.example.Student"> <!--使用 property 元素完成属性注入 name: 类中的属性名称,例如 id,name value: 向属性注入的值 例如 学生的 id 为 1,name 为张三 --> <property name="id" value="1"></property> <property name="name" value="张三"></property> <property name="grade" ref="grade"></property> </bean> <bean id="grade" class="org.example.Grade"> <property name="gradeId" value="3"></property> <property name="gradeName" value="三年级"></property> </bean> </beans> ![04c049a1acee2e3ad8b74432295cbd86.png][] 执行: package org.example; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class Main { private static final Log LOGGER = LogFactory.getLog(Main.class); public static void main(String[] args) { //获取 ApplicationContext 容器 ApplicationContext context = new ClassPathXmlApplicationContext("bean.xml"); //获取名为 student 的 Bean Student student = context.getBean("student", Student.class); //通过日志打印学生信息 LOGGER.info(student.toString()); System.out.println(student.toString()); } } ![5b570c473dac67be57876313a9581502.png][] 执行结果: ![f36d3e0df458998e77024d109fc91161.png][] [9c5520241685532fd73f3973e87d6fa6.png]: https://image.dandelioncloud.cn/pgy_files/images/2024/03/31/a67872eb336b4bb2a50b8b43fb90bd7c.png [47b8952dfe01bd7df5ab55de1fa8ef79.png]: https://image.dandelioncloud.cn/pgy_files/images/2024/03/31/1088d8ab9fae489aa9fd8407365b10dc.png [0f9784e1e9c1c829478d8b878eaf97e4.png]: https://image.dandelioncloud.cn/pgy_files/images/2024/03/31/cb9d62a6dce8475bb2ee45dd767f5a1c.png [04c049a1acee2e3ad8b74432295cbd86.png]: https://image.dandelioncloud.cn/pgy_files/images/2024/03/31/e4fb7aa2e86146d0982c0384bc8f805c.png [5b570c473dac67be57876313a9581502.png]: https://image.dandelioncloud.cn/pgy_files/images/2024/03/31/b1a6ea1dcca34e0eb449765a58267dee.png [f36d3e0df458998e77024d109fc91161.png]: https://image.dandelioncloud.cn/pgy_files/images/2024/03/31/285b816b6d9445fdbd20b34244d5f284.png
还没有评论,来说两句吧...