赞
踩
- package net.cybclass.sp.domain;
-
- /**
- * @author: wangxiaobo
- * @create: 2020-10-04 19:15
- **/
- 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 id="orders" class="net.cybclass.sp.domain.Orders" init-method="initMethod" destroy-method="destroyMethod">
- <property name="oname" value="手机"></property>
- </bean>
- /**
- * @author: wangxiaobo
- * @create: 2020-10-03 15:11
- **/
- public class app {
- public static void main(String[] args) {
- // ApplicationContext applicationContext=new ClassPathXmlApplicationContext("applicationContext.xml");
- // Video video=(Video) applicationContext.getBean("video");
- // VideoOrder videoOrder= (VideoOrder) applicationContext.getBean("videoOrder");
- // System.out.println(video.getTitle());
- // System.out.println(videoOrder.getVideo());
- // System.out.println(video.getChapterList());
- // System.out.println(video.getVideoMap());
- // Video2 video=(Video2) applicationContext.getBean("video2");
- // System.out.println(video);
- ClassPathXmlApplicationContext context =
- new ClassPathXmlApplicationContext("applicationContext.xml");
- Orders orders = context.getBean("orders", Orders.class);
- System.out.println("第四步 获取创建 bean 实例对象");
- System.out.println(orders);
- //手动让 bean 实例销毁
- context.close();
- }
- }
运行:
- package net.cybclass.sp.domain;
-
- import org.springframework.beans.BeansException;
- import org.springframework.beans.factory.config.BeanPostProcessor;
-
- /**
- * @author: wangxiaobo
- * @create: 2020-10-04 19:26
- **/
- 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 id="myBeanPost" class="net.cybclass.sp.domain.MyBeanPost"></bean>
运行:
赞
踩
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。