diff --git a/SpringFramework-container/src/main/java/com/ycjy/Main.java b/SpringFramework-container/src/main/java/com/ycjy/Main.java index 5b3a284..b9e6e5b 100644 --- a/SpringFramework-container/src/main/java/com/ycjy/Main.java +++ b/SpringFramework-container/src/main/java/com/ycjy/Main.java @@ -8,21 +8,33 @@ import org.springframework.context.support.ClassPathXmlApplicationContext; public class Main { public static void main(String[] args) { - // 实现了ApplicationContext接口的类型都是容器 - // 容器对象只创建一次就够了 + // ApplicationContext(应用上下文) 是 Spring 的容器接口, + // 所有实现了该接口的类型都可以当作容器使用. + // ClassPathXmlApplicationContext(类路径 XML 应用上下文) 会从 classpath 下 + // 读取 spring-config.xml 并启动容器. + // 容器对象通常只需要创建一个, 重复创建会重复加载配置, 浪费资源. ApplicationContext context = new ClassPathXmlApplicationContext("spring-config.xml"); - UserServiceImpl userService= context.getBean("myUser", UserServiceImpl.class); + + // 按名称 "myUser" 和类型 UserServiceImpl 获取 Bean + UserServiceImpl userService = context.getBean("myUser", UserServiceImpl.class); userService.doSth(); - /* System.out.println(userService.hashCode()); - UserServiceImpl userService2= context.getBean("myUser", UserServiceImpl.class); + /* 下面这段被注释的代码, 是用来验证单例(Singleton) 的: + 同一个 Bean 名称, 两次获取, 打印的 hashCode 应该相同, + 说明拿到的是同一个实例. + + System.out.println(userService.hashCode()); + UserServiceImpl userService2 = context.getBean("myUser", UserServiceImpl.class); System.out.println(userService2.hashCode()); + */ -*/ - + // "dept" 对应的是 DeptServiceImplFactoryBean, + // 但这里拿到的不是工厂本身, 而是工厂 getObject() 返回的产品 (DeptServiceImpl). DeptServiceImpl deptService = context.getBean("dept", DeptServiceImpl.class); deptService.doSth(); + // 在 Bean 名称前加 "&", 才能拿到 FactoryBean(工厂 Bean) 本身. + // 这是 FactoryBean 的一个经典用法: 普通名称拿产品, "&" 前缀拿工厂. DeptServiceImplFactoryBean o = context.getBean("&dept", DeptServiceImplFactoryBean.class); System.out.println(o); } diff --git a/SpringFramework-container/src/main/java/com/ycjy/factory/DeptServiceImplFactoryBean.java b/SpringFramework-container/src/main/java/com/ycjy/factory/DeptServiceImplFactoryBean.java index 396a11b..7d5f89b 100644 --- a/SpringFramework-container/src/main/java/com/ycjy/factory/DeptServiceImplFactoryBean.java +++ b/SpringFramework-container/src/main/java/com/ycjy/factory/DeptServiceImplFactoryBean.java @@ -4,27 +4,48 @@ import com.ycjy.service.DeptServiceImpl; import org.jspecify.annotations.Nullable; import org.springframework.beans.factory.FactoryBean; - +/** + * 部门服务实现工厂类 + * + * 实现 FactoryBean, 泛型指定该工厂生产的产品类型为 DeptServiceImpl. + * 关键规则: 一个类一旦实现 FactoryBean 接口, Spring 容器通过 getBean() 获取到的 + * 不是 FactoryBean 本身, 而是 getObject() 返回的对象 (即 "产品"). + */ public class DeptServiceImplFactoryBean implements FactoryBean { + /** + * 创建产品的核心方法 + * Spring 调用它来获取真正要交给容器管理的对象. + * + * @Nullable 返回值允许为 null + * @throws Exception 创建过程中允许抛出异常, 由 Spring 处理 + */ @Override public @Nullable DeptServiceImpl getObject() throws Exception { + // 此处可编写复杂的创建逻辑, 例如 MyBatis 的初始化: + // String resource = "spring-config.xml"; + // InputStream inputStream = Resources.getResourceAsStream(resource); + // SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream); + // return sqlSessionFactory; - /* String resource = "org/mybatis/example/mybatis-config.xml"; - InputStream inputStream = Resources.getResourceAsStream(resource); - SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream); - return sqlSessionFactory;*/ return new DeptServiceImpl(); - } + /** + * 声明产品的类型 + * Spring 用这个信息做类型检查和依赖注入时的类型匹配. + */ @Override public @Nullable Class getObjectType() { - //return SqlSessionFactory.class; return DeptServiceImpl.class; } + /** + * 声明产品的作用域 + * true 表示单例 (Singleton): 整个容器只创建一次, 之后复用同一实例 + * false 表示每次获取都调用 getObject() 创建新实例 + */ @Override public boolean isSingleton() { return true; } -} +} \ No newline at end of file diff --git a/SpringFramework-container/src/main/java/com/ycjy/service/DeptServiceImpl.java b/SpringFramework-container/src/main/java/com/ycjy/service/DeptServiceImpl.java index 5687efd..fc90863 100644 --- a/SpringFramework-container/src/main/java/com/ycjy/service/DeptServiceImpl.java +++ b/SpringFramework-container/src/main/java/com/ycjy/service/DeptServiceImpl.java @@ -1,7 +1,7 @@ package com.ycjy.service; public class DeptServiceImpl { - public void doSth(){ - System.out.println("===doSth ===in deptServiceImpl"); + public void doSth() { + System.out.println("--做某事, 在DeptServiceImpl中"); } -} +} \ No newline at end of file diff --git a/SpringFramework-container/src/main/java/com/ycjy/service/UserServiceImpl.java b/SpringFramework-container/src/main/java/com/ycjy/service/UserServiceImpl.java index fa24982..db4f1e1 100644 --- a/SpringFramework-container/src/main/java/com/ycjy/service/UserServiceImpl.java +++ b/SpringFramework-container/src/main/java/com/ycjy/service/UserServiceImpl.java @@ -5,26 +5,50 @@ import org.springframework.beans.factory.BeanNameAware; import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContextAware; -public class UserServiceImpl implements BeanNameAware , ApplicationContextAware { - private UserServiceImpl(){ - System.out.println("----user constructor"); - } - public void doSth(){ - System.out.println(this.name + "---doSth ---in userServiceImpl"); - +/** + * 用户服务实现类 + *

+ * 实现了两个 Aware(感知) 接口: + * - BeanNameAware(感知 Bean 名称): 让 Bean 能拿到自己在容器中的名字 + * - ApplicationContextAware(感知应用上下文): 让 Bean 能拿到 Spring 的 ApplicationContext + *

+ * 这两个接口都属于 Spring 的 Aware 回调机制: + * 容器在创建 Bean 的过程中, 会主动把某些资源"注入"给 Bean. + */ +public class UserServiceImpl implements BeanNameAware, ApplicationContextAware { + /** + * 私有构造函数 + * Spring 通过反射调用它来创建实例, 每创建一个都会打印一行提示. + */ + private UserServiceImpl() { + System.out.println("--用户, 构造函数"); } + public void doSth() { + System.out.println(this.name + "--做某事, 在UserServiceImpl中"); + } + + // 用于保存容器注入进来的 Bean 名称 private String name; + /** + * BeanNameAware 的回调方法 + * 容器在创建 Bean 时调用它, 把当前 Bean 在容器中的名字传进来. + */ @Override public void setBeanName(String name) { this.name = name; } + // 用于保存容器注入进来的 ApplicationContext private ApplicationContext context; + /** + * ApplicationContextAware 的回调方法 + * 容器在创建 Bean 时调用它, 把整个 ApplicationContext 传进来. + */ @Override public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { this.context = applicationContext; } -} +} \ No newline at end of file diff --git a/SpringFramework-container/src/main/resources/spring-config.xml b/SpringFramework-container/src/main/resources/spring-config.xml index 264e2a3..ba973b8 100644 --- a/SpringFramework-container/src/main/resources/spring-config.xml +++ b/SpringFramework-container/src/main/resources/spring-config.xml @@ -1,8 +1,9 @@ + xsi:schemaLocation="http://www.springframework.org/schema/beans + http://www.springframework.org/schema/beans/spring-beans.xsd"> - - + + \ No newline at end of file