使用注解的方式把类注入到容器
1.新建pojo 实体类,Student ,ClassRoom ,Adress,并加上注解
//student类
package pojo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
public class Student {
private int id;
private String name;
@Autowired
private Adress adress;
// 当有多个ClassRoom 类的时候,设定用id为classRoom2的bean
@Autowired
@Qualifier("classRoom2")
private ClassRoom classRoom;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Adress getAdress() {
return adress;
}
public void setAdress(Adress adress) {
this.adress = adress;
}
public ClassRoom getClassRoom() {
return classRoom;
}
public void setClassRoom(ClassRoom classRoom) {
this.classRoom = classRoom;
}
@Override
public String toString() {
return "Student{" +
"id=" + id +
", name=‘" + name + ‘\‘‘ +
", adress=" + adress +
", classRoom=" + classRoom +
‘}‘;
}
}
//ClassRoom类
package pojo;
public class ClassRoom {
private Integer id;
private String roomName;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getRoomName() {
return roomName;
}
public void setRoomName(String roomName) {
this.roomName = roomName;
}
@Override
public String toString() {
return "ClassRoom{" +
"id=" + id +
", roomName=‘" + roomName + ‘\‘‘ +
‘}‘;
}
}
//Adress类
package pojo;
public class Adress {
private Integer id;
private String aressMessage;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getAressMessage() {
return aressMessage;
}
public void setAressMessage(String aressMessage) {
this.aressMessage = aressMessage;
}
@Override
public String toString() {
return "Adress{" +
"id=" + id +
", aressMessage=‘" + aressMessage + ‘\‘‘ +
‘}‘;
}
}
2.配置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"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">
<bean id="adress" class="pojo.Adress" >
<property name="aressMessage" value="shanwei"></property>
</bean>
<bean id="classRoom1" class="pojo.ClassRoom">
<property name="roomName" value="一班"></property>
</bean>
<bean id="classRoom2" class="pojo.ClassRoom">
<property name="roomName" value="二班"></property>
</bean>
<bean id="student" class="pojo.Student"></bean>
<!-- 开启注解-->
<context:annotation-config/>
</beans>
3.编写测试类
package test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import pojo.Student;
public class Mytest {
public static void main(String[] args) {
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
Student student=applicationContext.getBean("student", Student.class);
System.out.println(student);
}
}
输出的结果是:
Student{id=0, name=‘null‘, adress=Adress{id=null, aressMessage=‘shanwei‘}, classRoom=ClassRoom{id=null, roomName=‘二班‘}}
//ClassRoom类
package pojo;
public class ClassRoom {
private Integer id;
private String roomName;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getRoomName() {
return roomName;
}
public void setRoomName(String roomName) {
this.roomName = roomName;
}
@Override
public String toString() {
return "ClassRoom{" +
"id=" + id +
", roomName=‘" + roomName + ‘\‘‘ +
‘}‘;
}
}
原文:https://www.cnblogs.com/codinggege/p/13599606.html