FactoryBean
接口是Spring IoC容器实例化逻辑的可插拔性点。
用于配置复杂的Bean对象,可以将创建过程存储在FactoryBean
的getObject方法!
FactoryBean
接口提供三种方法:
T getObject()
: 返回此工厂创建的对象的实例。该返回值会被存储到IoC容器!boolean isSingleton()
: 如果此 FactoryBean
返回单例,则返回 true
,否则返回 false
。此方法的默认实现返回 true
(注意,lombok插件使用,可能影响效果)。Class getObjectType()
: 返回 getObject()
方法返回的对象类型,如果事先不知道类型,则返回 null
。// 实现FactoryBean接口时需要指定泛型
// 泛型类型就是当前工厂要生产的对象的类型
public class HappyFactoryBean implements FactoryBean<HappyMachine> {
private String machineName;
public String getMachineName() {
return machineName;
}
public void setMachineName(String machineName) {
this.machineName = machineName;
}
@Override
public HappyMachine getObject() throws Exception {
// 方法内部模拟创建、设置一个对象的复杂过程
HappyMachine happyMachine = new HappyMachine();
happyMachine.setMachineName(this.machineName);
return happyMachine;
}
@Override
public Class<?> getObjectType() {
// 返回要生产的对象的类型
return HappyMachine.class;
}
}
<bean id="happyMachine7" class="com.alex.ioc.HappyFactoryBean">
<property name="machineName" value="iceCreamMachine"/>
bean>
@Test
public void testExperiment07() {
ApplicationContext iocContainer = new ClassPathXmlApplicationContext("spring-bean-07.xml");
//注意: 直接根据声明FactoryBean的id,获取的是getObject方法返回的对象
HappyMachine happyMachine = iocContainer.getBean("happyMachine7",HappyMachine.class);
System.out.println("happyMachine = " + happyMachine);
//如果想要获取FactoryBean对象, 直接在id前添加&符号即可! &happyMachine7 这是一种固定的约束
Object bean = iocContainer.getBean("&happyMachine7");
System.out.println("bean = " + bean);
}
FactoryBean 是 Spring 中一种特殊的 bean,可以在 getObject() 工厂方法自定义的逻辑创建Bean!是一种能够生产其他 Bean 的 Bean。FactoryBean 在容器启动时被创建,而在实际使用时则是通过调用 getObject() 方法来得到其所生产的 Bean。因此,FactoryBean 可以自定义任何所需的初始化逻辑,生产出一些定制化的 bean。
一般情况下,整合第三方框架,都是通过定义FactoryBean实现!!!
BeanFactory 是 Spring 框架的基础,其作为一个顶级接口定义了容器的基本行为,例如管理 bean 的生命周期、配置文件的加载和解析、bean 的装配和依赖注入等。BeanFactory 接口提供了访问 bean 的方式,例如 getBean() 方法获取指定的 bean 实例。它可以从不同的来源(例如 Mysql 数据库、XML 文件、Java 配置类等)获取 bean 定义,并将其转换为 bean 实例。同时,BeanFactory 还包含很多子类(例如,ApplicationContext 接口)提供了额外的强大功能。
总的来说,FactoryBean 和 BeanFactory 的区别主要在于前者是用于创建 bean 的接口,它提供了更加灵活的初始化定制功能,而后者是用于管理 bean 的框架基础接口,提供了基本的容器功能和 bean 生命周期管理。
create database studb;
use studb;
CREATE TABLE students (
id INT PRIMARY KEY,
name VARCHAR(50) NOT NULL,
gender VARCHAR(10) NOT NULL,
age INT,
class VARCHAR(50)
);
INSERT INTO students (id, name, gender, age, class)
VALUES
(1, '张三', '男', 20, '高中一班'),
(2, '李四', '男', 19, '高中二班'),
(3, '王五', '女', 18, '高中一班'),
(4, '赵六', '女', 20, '高中三班'),
(5, '刘七', '男', 19, '高中二班'),
(6, '陈八', '女', 18, '高中一班'),
(7, '杨九', '男', 20, '高中三班'),
(8, '吴十', '男', 19, '高中二班');
<dependencies>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-contextartifactId>
<version>6.0.6version>
dependency>
<dependency>
<groupId>mysqlgroupId>
<artifactId>mysql-connector-javaartifactId>
<version>8.0.25version>
dependency>
<dependency>
<groupId>com.alibabagroupId>
<artifactId>druidartifactId>
<version>1.2.8version>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-jdbcartifactId>
<version>6.0.6version>
dependency>
dependencies>
public class Student {
private Integer id;
private String name;
private String gender;
private Integer age;
private String classes;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public String getClasses() {
return classes;
}
public void setClasses(String classes) {
this.classes = classes;
}
@Override
public String toString() {
return "Student{" +
"id=" + id +
", name='" + name + '\'' +
", gender='" + gender + '\'' +
", age=" + age +
", classes='" + classes + '\'' +
'}';
}
}
alex.url=jdbc:mysql://localhost:3306/studb
alex.driver=com.mysql.cj.jdbc.Driver
alex.username=root
alex.password=root
<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 http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">
<context:property-placeholder location="classpath:jdbc.properties" />
<bean id="druidDataSource" class="com.alibaba.druid.pool.DruidDataSource">
<property name="url" value="${alex.url}"/>
<property name="driverClassName" value="${alex.driver}"/>
<property name="username" value="${alex.username}"/>
<property name="password" value="${alex.password}"/>
bean>
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="druidDataSource"/>
bean>
beans>
public class JdbcTemplateTest {
/**
* 使用jdbcTemplate进行DML动作
*/
@Test
public void testDML(){
ApplicationContext applicationContext =
new ClassPathXmlApplicationContext("spring-ioc.xml");
JdbcTemplate jdbcTemplate = applicationContext.getBean(JdbcTemplate.class);
//TODO 执行插入一条学员数据
String sql = "insert into students (id,name,gender,age,class) values (?,?,?,?,?);";
/*
参数1: sql语句
参数2: 可变参数,占位符的值
*/
int rows = jdbcTemplate.update(sql, 9,"十一", "男", 18, "二年三班");
System.out.println("rows = " + rows);
}
/**
* 查询单条实体对象
* public class Student {
* private Integer id;
* private String name;
* private String gender;
* private Integer age;
* private String classes;
*/
@Test
public void testDQLForPojo(){
String sql = "select id , name , age , gender , class as classes from students where id = ? ;";
ApplicationContext applicationContext =
new ClassPathXmlApplicationContext("spring-ioc.xml");
JdbcTemplate jdbcTemplate = applicationContext.getBean(JdbcTemplate.class);
//根据id查询
Student student = jdbcTemplate.queryForObject(sql, (rs, rowNum) -> {
//自己处理结果映射
Student stu = new Student();
stu.setId(rs.getInt("id"));
stu.setName(rs.getString("name"));
stu.setAge(rs.getInt("age"));
stu.setGender(rs.getString("gender"));
stu.setClasses(rs.getString("classes"));
return stu;
}, 2);
System.out.println("student = " + student);
}
/**
* 查询实体类集合
*/
@Test
public void testDQLForListPojo(){
String sql = "select id , name , age , gender , class as classes from students ;";
ApplicationContext applicationContext =
new ClassPathXmlApplicationContext("spring-ioc.xml");
JdbcTemplate jdbcTemplate = applicationContext.getBean(JdbcTemplate.class);
/*
query可以返回集合!
BeanPropertyRowMapper就是封装好RowMapper的实现,要求属性名和列名相同即可
*/
List<Student> studentList = jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(Student.class));
System.out.println("studentList = " + studentList);
}
}
//接口
public interface StudentDao {
/**
* 查询全部学生数据
* @return
*/
List<Student> queryAll();
}
//实现类
public class StudentDaoImpl implements StudentDao {
private JdbcTemplate jdbcTemplate;
public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
this.jdbcTemplate = jdbcTemplate;
}
/**
* 查询全部学生数据
* @return
*/
@Override
public List<Student> queryAll() {
String sql = "select id , name , age , gender , class as classes from students ;";
/*
query可以返回集合!
BeanPropertyRowMapper就是封装好RowMapper的实现,要求属性名和列名相同即可
*/
List<Student> studentList = jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(Student.class));
return studentList;
}
}
//接口
public interface StudentService {
/**
* 查询全部学员业务
* @return
*/
List<Student> findAll();
}
//实现类
public class StudentServiceImpl implements StudentService {
private StudentDao studentDao;
public void setStudentDao(StudentDao studentDao) {
this.studentDao = studentDao;
}
/**
* 查询全部学员业务
* @return
*/
@Override
public List<Student> findAll() {
List<Student> studentList = studentDao.queryAll();
return studentList;
}
}
public class StudentController {
private StudentService studentService;
public void setStudentService(StudentService studentService) {
this.studentService = studentService;
}
public void findAll(){
List<Student> studentList = studentService.findAll();
System.out.println("studentList = " + studentList);
}
}
<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 http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">
<context:property-placeholder location="classpath:jdbc.properties" />
<bean id="druidDataSource" class="com.alibaba.druid.pool.DruidDataSource">
<property name="url" value="${alex.url}"/>
<property name="driverClassName" value="${alex.driver}"/>
<property name="username" value="${alex.username}"/>
<property name="password" value="${alex.password}"/>
bean>
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="druidDataSource"/>
bean>
<bean id="studentDao" class="com.alex.dao.impl.StudentDaoImpl">
<property name="jdbcTemplate" ref="jdbcTemplate" />
bean>
<bean id="studentService" class="com.alex.service.impl.StudentServiceImpl">
<property name="studentDao" ref="studentDao" />
bean>
<bean id="studentController" class="com.alex.controller.StudentController">
<property name="studentService" ref="studentService" />
bean>
beans>
public class ControllerTest {
@Test
public void testRun(){
ApplicationContext applicationContext =
new ClassPathXmlApplicationContext("spring-ioc.xml");
StudentController studentController = applicationContext.getBean(StudentController.class);
studentController.findAll();
}