
- <bean id="userService" class="com.example.Service.Impl.UserServiceImpl">
- <property name="userDAO" ref="userDAO">property>
- <property name="name" value="hhhh">property>
- bean>
- <bean name="userDAO" class="com.example.DAO.Impl.UserDAOImpl">bean>

- package com.example.Service.Impl;
-
- import com.example.DAO.UserDAO;
- import com.example.Service.UserService;
-
-
- public class UserServiceImpl implements UserService {
- public UserServiceImpl() {
- System.out.println("UserService对象创建");
- }
-
- private UserDAO userDAO;
- private String name;
-
-
- public void setName(String name) {
- this.name = name;
- }
-
- public void setUserDAO(UserDAO userDAO) {
- System.out.println("UserService执行注入UserDAO的操作:setDAO方法");
- this.userDAO = userDAO;
- }
-
- @Override
- public void show() {
- System.out.println("show~~~");
- }
- }
UserDAOImpl类
- package com.example.DAO.Impl;
-
- import com.example.DAO.UserDAO;
-
- public class UserDAOImpl implements UserDAO {
-
- public UserDAOImpl() {
- System.out.println("UserDAO对象创建");
- }
- }
测试类
- package com.example.Test;
-
-
- import com.example.Service.UserService;
- import org.springframework.context.support.ClassPathXmlApplicationContext;
-
- public class TestApplicationContext {
- public static void main(String[] args) {
- ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("application.xml");
- UserService UserServiceBean = (UserService) context.getBean(UserService.class);
- }
- }
-
若配置文件中先创建UserADO的bean对象
- "1.0" encoding="UTF-8"?>
- <beans xmlns="http://www.springframework.org/schema/beans"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
- <bean class="com.example.PostProcessor.TimeLogBeanPostProcessor">bean>
-
-
-
-
-
-
- <bean name="userDAO" class="com.example.DAO.Impl.UserDAOImpl">bean>
- <bean id="userService" class="com.example.Service.Impl.UserServiceImpl">
- <property name="userDAO" ref="userDAO">property>
- <property name="name" value="hhhh">property>
- bean>
-
- beans>
测试类运行结果为

若配置文件中先创建UserService的bean对象
- "1.0" encoding="UTF-8"?>
- <beans xmlns="http://www.springframework.org/schema/beans"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
- <bean class="com.example.PostProcessor.TimeLogBeanPostProcessor">bean>
-
-
-
-
-
-
-
- <bean id="userService" class="com.example.Service.Impl.UserServiceImpl">
- <property name="userDAO" ref="userDAO">property>
- <property name="name" value="hhhh">property>
- bean>
- <bean name="userDAO" class="com.example.DAO.Impl.UserDAOImpl">bean>
-
- beans>
运行结果如下


- public class DefaultSingletonBeanRegistry .......{
- .....
- // todo 最终存储单例Bean成品的容器,即实例化和初始化都完成的Bean,称之为’一级缓存‘
- private final Map
singletonObjects=new ConcurrentHashMap(256); - // todo 早期Bean单例池,缓存半成品对象,且当前对象已经被其它对象引用了,称之为‘二级缓存’
- private final Map
earlySingletonObjects=new ConcurrentHashMap(16); - // todo 单例Bean的工厂池,缓存半成品对象,对象未被引用,使用时再通过工厂创建Bean,称之为‘三级缓存’
- private final Map
>singletonFactories=new HashMap(16); - .....
- }
上述流程图中的实例化Service/DAO存放在三级缓存中,将其包装为对应的ObjectFactory,在ObjectFactory的getObject()方法中返回我们创建好的UserService/DAO,即上述流程图中的实例化Service/DAO,ObjectFactory是用来创建和管理Service/DAO对象的工厂类,而不是用来实例化Service/DAO对象的。被引用后,就会交给二级缓存,从三级换中移除。






| Aware接口 | 回调方法 | 作用 |
|---|---|---|
| ApplicationContextAware | setApplicationContext(ApplicationContext applicationContext) | 允许Bean获取ApplicationContext对象,从而可以访问Spring容器的各种功能。 |
| BeanFactoryAware | setBeanFactory(BeanFactory beanFactory) | 允许Bean获取BeanFactory对象,从而可以访问Spring容器的Bean工厂。 |
| BeanNameAware | setBeanName(String name) | 允许Bean获取自己在Spring容器中的名称。 |
| ServletContextAware | setServletContext(ServletContext servletContext) | 允许Bean获取ServletContext对象,从而可以访问Servlet容器的功能。 |
在Spring中,Aware接口的作用是让Bean能够感知和获取一些特定的功能或资源。通过实现Aware接口,并在对应的回调方法中接收相关的对象,Bean可以在运行时获取到Spring容器提供的一些重要信息或功能。
举个例子来说,如果一个Bean实现了ApplicationContextAware接口并实现了对应的回调方法,那么在Bean初始化的过程中,Spring容器会将ApplicationContext对象传递给该Bean。这样,该Bean就能够直接访问ApplicationContext,从而获取到Spring容器的各种功能,比如获取其他Bean、读取配置文件、发布事件等。这样的设计使得Bean能够更加灵活地与Spring容器进行交互,而不仅仅局限于自身的业务逻辑。
类似地,其他的Aware接口也提供了类似的功能,让Bean能够获取到BeanFactory、Bean的名称、国际化消息、资源加载器、应用程序事件发布器、运行时环境等等。通过实现这些Aware接口,Bean可以获取到所需的功能或资源,从而实现更加灵活和可扩展的功能。