| 通过构造器创建 bean 实例(无参数构造) |
| 为 bean 的属性设置值和对其他 bean 引用(调用 set 方法) |
| 调用 bean 的初始化的方法(需要进行配置初始化的方法) |
| bean 可以使用了(对象获取到了) |
| 当容器关闭时候,调用 bean 的销毁的方法(需要进行配置销毁的方法) |
| # 编写实体 |
| public class Orders { |
| |
| |
| public Orders() { |
| System.out.println("第一步 执行无参数构造创建bean实例"); |
| } |
| |
| private String oname; |
| public void setOname(String oname) { |
| this.oname = oname; |
| System.out.println("第二步 调用set方法设置属性值"); |
| } |
| |
| |
| public void initMethod() { |
| System.out.println("第三步 执行初始化的方法"); |
| } |
| |
| |
| public void destroyMethod() { |
| System.out.println("第五步 执行销毁的方法"); |
| } |
| |
| } |
| |
| # 配置bean.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" |
| xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> |
| |
| <bean id="orders" class="com.ychen.spring.bean.Orders" init-method="initMethod" destroy-method="destroyMethod"> |
| <property name="oname" value="手机"></property> |
| </bean> |
| |
| </beans> |
| |
| # 测试方法 |
| @Test |
| public void testBean3() { |
| ClassPathXmlApplicationContext context = |
| new ClassPathXmlApplicationContext("bean4.xml"); |
| Orders orders = context.getBean("orders", Orders.class); |
| System.out.println("第四步 获取创建bean实例对象"); |
| System.out.println(orders); |
| |
| context.close(); |
| } |
| |
| # 控制台 |
| 第一步 执行无参数构造创建bean实例 |
| 第二步 调用set方法设置属性值 |
| 第三步 执行初始化的方法 |
| 第四步 获取创建bean实例对象 |
| com.ychen.spring.bean.Orders@68c9133c |
| 第五步 执行销毁的方法 |
| |
| Process finished with exit code 0 |
| # 测试方法写法2 |
| @Test |
| public void testBean3() { |
| ApplicationContext context = |
| new ClassPathXmlApplicationContext("bean4.xml"); |
| Orders orders = context.getBean("orders", Orders.class); |
| System.out.println("第四步 获取创建bean实例对象"); |
| System.out.println(orders); |
| |
| ((ClassPathXmlApplicationContext)context).close(); |
| } |
| |
| # 将context对象强转为ClassPathXmlApplicationContext类型,才能手动销毁bean |
| 通过构造器创建 bean 实例(无参数构造) |
| 为 bean 的属性设置值和对其他 bean 引用(调用 set 方法) |
| 把 bean 实例传递 bean 后置处理器的方法 postProcessBeforeInitialization (4) |
| 调用 bean 的初始化的方法(需要进行配置初始化的方法) |
| 把 bean 实例传递 bean 后置处理器的方法 postProcessAfterInitialization |
| bean 可以使用了(对象获取到了) |
| 当容器关闭时候,调用 bean 的销毁的方法(需要进行配置销毁的方法) |
| # 编写实体 |
| public class Orders { |
| |
| |
| public Orders() { |
| System.out.println("第一步 执行无参数构造创建bean实例"); |
| } |
| |
| private String oname; |
| public void setOname(String oname) { |
| this.oname = oname; |
| System.out.println("第二步 调用set方法设置属性值"); |
| } |
| |
| |
| public void initMethod() { |
| System.out.println("第三步 执行初始化的方法"); |
| } |
| |
| |
| public void destroyMethod() { |
| System.out.println("第五步 执行销毁的方法"); |
| } |
| |
| } |
| |
| # 编写后置处理器 |
| public class MyBeanPost implements BeanPostProcessor { |
| |
| @Override |
| public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException { |
| System.out.println("在初始化之前执行的方法"); |
| return bean; |
| } |
| |
| @Override |
| public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException { |
| System.out.println("在初始化之后执行的方法"); |
| return bean; |
| } |
| |
| } |
| |
| # 配置bean.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" |
| xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> |
| |
| <bean id="orders" class="com.ychen.spring.bean.Orders" init-method="initMethod" destroy-method="destroyMethod"> |
| <property name="oname" value="手机"></property> |
| </bean> |
| |
| <bean id="myBeanPost" class="com.ychen.spring.bean.MyBeanPost"></bean> |
| |
| </beans> |
| |
| # 测试方法 |
| @Test |
| public void testBean3() { |
| ApplicationContext context = |
| new ClassPathXmlApplicationContext("bean4.xml"); |
| Orders orders = context.getBean("orders", Orders.class); |
| System.out.println("第四步 获取创建bean实例对象"); |
| System.out.println(orders); |
| ((ClassPathXmlApplicationContext)context).close(); |
| } |
| |
| # 控制台 |
| 第一步 执行无参数构造创建bean实例 |
| 第二步 调用set方法设置属性值 |
| 在初始化之前执行的方法 |
| 第三步 执行初始化的方法 |
| 在初始化之后执行的方法 |
| 第四步 获取创建bean实例对象 |
| com.ychen.spring.bean.Orders@2cb4893b |
| 第五步 执行销毁的方法 |
| |
| Process finished with exit code 0 |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
· 字符编码:从基础到乱码解决
· 提示词工程——AI应用必不可少的技术