Bean管理指的是2个操作,即创建对象 & 注入属性。
// 1、实体类
public class Book implements Serializable {
/**
* 书名
*/
private String name;
/**
* 作者
*/
private String author;
public String getName() {
return name;
}
public void setName(String name) {
System.out.println("Book setName invoked...");
this.name = name;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
System.out.println("Book setAuthor invoked...");
this.author = author;
}
}
// 2、bean配置
// 3、测试
public class BookTests {
/**
* Bean管理:使用xml方式创建对象 + set方法注入属性
*/
@Test
public void beanManagementTest1() {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext2.xml");
Book book = context.getBean("book", Book.class);
System.out.println(book);
}
}
// 控制台打印结果
Book setName invoked...
Book setAuthor invoked...
org.star.entity.Book@3891771e
// 1、实体类
@NoArgsConstructor
@Data
public class Order implements Serializable {
/**
* 订单名称
*/
private String name;
/**
* 收货地址
*/
private String address;
public Order(String name, String address) {
System.out.println("Order 有参构造方法被调用了......");
this.name = name;
this.address = address;
}
}
// 2、bean配置
// 3、测试
/**
* Bean管理:使用xml方式创建对象 + 有参构造方法注入属性
*/
@Test
public void beanManagementTest2() {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext3.xml");
Order order = context.getBean("order", Order.class);
System.out.println(order);
}
// 控制台打印结果
Order 有参构造方法被调用了......
Order(name=电脑, address=中国)
// 实体类
@AllArgsConstructor
@NoArgsConstructor
@Data
public class School implements Serializable {
/**
* 学校名称
*/
private String name;
/**
* 建校时间
*/
private String createDate;
/**
* 所在城市
*/
private String city;
/**
* 最少录取分数线
*/
private Integer minScore;
}
// bean配置
>]]>
// 测试
/**
* Bean管理:使用xml方式创建对象 + set方法注入属性(注入特殊属性)
*/
@Test
public void beanManagementTest3() {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext4.xml");
School school = context.getBean("school", School.class);
System.out.println(school);
}
// 控制台打印结果
School(name=北京大学, createDate=1898, city=<<北京>>, minScore=null)
// Dao
public interface UserDao {
void add();
}
public class UserDaoImpl implements UserDao {
@Override
public void add() {
System.out.println("UserDaoImpl add......");
}
}
// Service
public interface UserService {
void add();
}
@Data
@AllArgsConstructor
@NoArgsConstructor
public class UserServiceImpl implements UserService {
private UserDao userDao;
@Override
public void add() {
System.out.println("UserServiceImpl add......");
userDao.add();
}
}
// bean配置
// 测试
/**
* Bean管理:使用xml方式创建对象 + set方法注入属性(注入外部Bean)
*/
@Test
public void beanManagementTest4() {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext5.xml");
UserService userService = context.getBean("userService", UserService.class);
userService.add();
}
// 控制台打印结果
UserServiceImpl add......
UserDaoImpl add......
// 实体类
@AllArgsConstructor
@NoArgsConstructor
@Data
public class Department implements Serializable {
/**
* 部门名称
*/
private String name;
}
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Employee implements Serializable {
/**
* 员工名称
*/
private String name;
/**
* 性别
*/
private String gender;
/**
* 部门
*/
private Department department;
}
// bean配置
// 测试
/**
* Bean管理:使用xml方式创建对象 + set方法注入属性(注入内部Bean)
*/
@Test
public void beanManagementTest5() {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext6.xml");
Employee employee = context.getBean("employee", Employee.class);
System.out.println(employee);
}
// 控制台打印结果
Employee(name=Lucy, gender=女, department=Department(name=人力资源部))
// 实体类
同2.1.5
// bean配置
// 测试
/**
* Bean管理:使用xml方式创建对象 + set方法注入属性(注入属性-级联赋值)
*/
@Test
public void beanManagementTest6() {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext7.xml");
Employee employee = context.getBean("employee", Employee.class);
System.out.println(employee);
}
// 控制台打印结果
Employee(name=Lucy, gender=女, department=Department(name=人力资源部))
// 实体类
@AllArgsConstructor
@NoArgsConstructor
@Data
public class Course implements Serializable {
/**
* 课程名称
*/
private String name;
}
@AllArgsConstructor
@NoArgsConstructor
@Data
public class Student implements Serializable {
private String[] interests;
private List list;
private List courses;
private Map maps;
private Set sets;
}
// bean配置
篮球
足球
张三丰
武当张三丰
MySQL
Redis
// 测试
/**
* Bean管理:使用xml方式创建对象 + set方法注入属性(注入数组类型属性、注入List集合类型属性、注入Map集合类型属性、注入Set集合类型属性)
*/
@Test
public void beanManagementTest7() {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext8.xml");
Student student = context.getBean("student", Student.class);
System.out.println(student);
}
// 控制台打印结果
Student(interests=[篮球, 足球], list=[张三丰, 武当张三丰], courses=[Course(name=RocketMQ课程), Course(name=RabbitMQ课程)], maps={java=JAVA, php=PHP}, sets=[MySQL, Redis])