set方法注入
set注入发生再容器调用无参构造器构造一个对象后。下面这个类只能使用set方法注入参数。
| public class School { |
| private String name; |
| private String address; |
| |
| public String getName() { |
| return name; |
| } |
| |
| public void setName(String name) { |
| this.name = name; |
| } |
| |
| public String getAddress() { |
| return address; |
| } |
| |
| public void setAddress(String address) { |
| this.address = address; |
| } |
| |
| @Override |
| public String toString() { |
| return "School{" + |
| "name='" + name + '\'' + |
| ", address='" + address + '\'' + |
| '}'; |
| } |
| } |
| <bean id="school" class="com.sora.services.School"> |
| <property name="name" value="成都信息工程大学"/> |
| <property name="address" value="成都"/> |
| </bean> |
| package com.sora.services; |
| |
| import org.springframework.context.ApplicationContext; |
| import org.springframework.context.support.ClassPathXmlApplicationContext; |
| |
| |
| |
| |
| |
| |
| |
| public class SchoolTest { |
| public static void main(String[] args) { |
| ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml"); |
| School school = context.getBean("school", School.class); |
| System.out.println(school); |
| } |
| } |
| School{name='成都信息工程大学', address='成都'} |
构造方法注入
| public class Student { |
| private String name; |
| private int age; |
| |
| public Student() { |
| } |
| |
| public Student(String name, int age) { |
| this.name = name; |
| this.age = age; |
| } |
| |
| 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; |
| } |
| |
| @Override |
| public String toString() { |
| return "Student{" + |
| "name='" + name + '\'' + |
| ", age=" + age + |
| '}'; |
| } |
| } |
| <bean id="student" class="com.sora.services.Student"> |
| <constructor-arg name="age" value="20"/> |
| <constructor-arg name="name" value="xiao"/> |
| </bean> |
| public class StudentTest { |
| @Test |
| public void test(){ |
| ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml"); |
| Student bean = context.getBean(Student.class); |
| System.out.println(bean); |
| } |
| } |
| Student{name='xiao', age=20} |
对象中注入属性
| public class Student { |
| private String name; |
| private int age; |
| private School school; |
| |
| public Student() { |
| } |
| |
| public Student(String name, int age) { |
| this.name = name; |
| this.age = age; |
| this.school = school; |
| } |
| |
| 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 void setSchool(School school) { |
| this.school = school; |
| } |
| @Override |
| public String toString() { |
| return "Student{" + |
| "name='" + name + '\'' + |
| ", age=" + age + |
| ", school=" + school + |
| '}'; |
| } |
| } |
使用构造函数
| <bean id="student" class="com.sora.services.Student"> |
| <constructor-arg name="age" value="20"/> |
| <constructor-arg name="name" value="xiao"/> |
| <constructor-arg name="school" ref="school"/> |
| </bean> |
| <bean id="school" class="com.sora.services.School"> |
| <property name="name" value="成都信息工程大学"/> |
| <property name="address" value="成都"/> |
| </bean> |
使用set方法
| <bean id="student" class="com.sora.services.Student"> |
| <property name="age" value="22"/> |
| <property name="name" value="xiao"/> |
| <property name="school" ref="school"/> |
| </bean> |
| <bean id="school" class="com.sora.services.School"> |
| <property name="name" value="成都信息工程大学"/> |
| <property name="address" value="成都"/> |
| </bean> |
| Student{name='xiao', age=22, school=School{name='成都信息工程大学', address='成都'}} |
本文作者:xiaoovo
本文链接:https://www.cnblogs.com/xiaoovo/p/17270929.html
版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行许可。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步