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;
/**
* @Classname: SchoolTest
* @Description:
* @Author: Stonffe
* @Date: 2023/3/29 22:51
*/
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='成都'}}
标签:school,依赖,name,age,Student,注入,public,String
From: https://www.cnblogs.com/xiaoovo/p/17270929.html