一、步骤
1.给实体类添加注解
2.在配置文件(applicationContext.xml)中扫描实体类所在包
3.测试
4.属性注入
目录结构
二、实操
1.给实体类添加注解
package com.zxs.pojo;
import org.springframework.stereotype.Component;
@Component
public class Book {
private int id;
private String name;
private String author;
public Book(){
System.out.println("调用了Book类的无参构造器");
}
public Book(int id2, String name, String author) {
this.id = id2;
this.name = name;
this.author = author;
System.out.println("调用了Book类的有参构造器");
}
public int getId() {
return id;
}
public void setId1(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
@Override
public String toString() {
return "Book{" +
"id=" + id +
", name='" + name + '\'' +
", author='" + author + '\'' +
'}';
}
}
2.在配置文件(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"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
https://www.springframework.org/schema/context/spring-context.xsd">
<!-- 扫描实体类包,通过注解创建实体类 -->
<context:component-scan base-package="com.zxs.pojo"></context:component-scan>
</beans>
3.测试
package test;
import com.zxs.pojo.Book;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Test01 {
public static void main(String[] args) {
//创建Spring容器
//当创建Spring容器的时候,会自动创建beans中的所有类
ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");
//获取对象
Book book= (Book) context.getBean("book");
//打印对象信息
System.out.println(book);
}
}
4.属性注入
Boy
package com.zxs.pojo;
public class Boy {
private int age;
private String name;
public Boy(){}
public Boy(int age, String name) {
this.age = age;
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "Boy{" +
"age=" + age +
", name='" + name + '\'' +
'}';
}
}
Girl
package com.zxs.pojo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
@Component
public class Girl {
@Value("21")
private int age;
@Value("丽丽")
private String name;
@Autowired
private Boy boyfriend;
public Girl(){}
public Girl(int age, String name, Boy boyfriend) {
this.age = age;
this.name = name;
this.boyfriend = boyfriend;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Boy getBoyfriend() {
return boyfriend;
}
public void setBoyfriend(Boy boyfriend) {
this.boyfriend = boyfriend;
}
@Override
public String toString() {
return "Girl{" +
"age=" + age +
", name='" + name + '\'' +
", boyfriend=" + boyfriend +
'}';
}
}
Test01
package test;
import com.zxs.pojo.Book;
import com.zxs.pojo.Girl;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Test01 {
public static void main(String[] args) {
//创建Spring容器
//当创建Spring容器的时候,会自动创建beans中的所有类
ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");
//获取对象
Girl girl=(Girl) context.getBean("girl");
//打印对象信息
System.out.println(girl);
}
}