IOC初始化销毁的2种实现方式
IOC初始化销毁的2种实现方式
1、bean内调用init-method 和destroy-method
2、通过注解实现@PostConstruct 和@PreDestroy
-----------------------------bean内调用init-method 和destroy-method------------------------
1、创建IOrderDao
package com.longteng.lesson2.dao;
public interface IOrderDao {
public String bookOrder(String name);
}
2、创建IOrderDao的实体 OrderDao
package com.longteng.lesson2.dao.impl;
import com.longteng.lesson2.dao.IOrderDao;
public class OrderDao implements IOrderDao {
@Override
public String bookOrder(String name) {
return name+"预订了订单";
}
}
3、创建OrderService
package com.longteng.lesson2.service;
import com.longteng.lesson2.dao.IOrderDao; public class OrderService {
private String orderSkuName;
public OrderService(String orderSkuName){
this.orderSkuName = orderSkuName;
} public void setOrderSkuName(String orderSkuName) {
this.orderSkuName = orderSkuName;
} public String getOrderSkuName() {
return orderSkuName;
} public void setiOrderDao(IOrderDao iOrderDao) {
this.iOrderDao = iOrderDao;
} IOrderDao iOrderDao; public String getOrderInfo(String name){
return iOrderDao.bookOrder(name);
}
public void initTest(){
System.out.println("初始化调用方法");
}
public void tearDownTest(){
System.out.println("回收调用方法");
}
}
4、 创建order.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="orderDao" class="com.longteng.lesson2.dao.impl.OrderDao"></bean>
<bean id="orderService" class="com.longteng.lesson2.service.OrderService" init-method="initTest" destroy-method="tearDownTest" >
<constructor-arg name="orderSkuName" value="123456789"></constructor-arg>
<property name="iOrderDao" ref="orderDao"></property>
</bean>
</beans>
5、创建测试类
package com.longteng.lesson2.dao; import com.longteng.lesson2.service.OrderService;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; public class OrderDaoTest {
ApplicationContext context;
@Before
public void initApplication(){
context = new ClassPathXmlApplicationContext("order.xml");
}
@Test
public void test(){
OrderService orderService = context.getBean("orderService", OrderService.class);
System.out.println(orderService.getOrderInfo("zhou"));
System.out.println(orderService.getOrderSkuName());
Assert.assertEquals("成功了","zhou预订了订单",orderService.getOrderInfo("zhou"));
}
}
6、测试结果
::43.096 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Invoking init method 'initTest' on bean with name 'orderService'
初始化调用方法
::43.097 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Finished creating instance of bean 'orderService'
::43.098 [main] DEBUG org.springframework.context.support.ClassPathXmlApplicationContext - Unable to locate LifecycleProcessor with name 'lifecycleProcessor': using default [org.springframework.context.support.DefaultLifecycleProcessor@d7f7e0]
::43.099 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Returning cached instance of singleton bean 'lifecycleProcessor'
::43.101 [main] DEBUG org.springframework.core.env.PropertySourcesPropertyResolver - Could not find key 'spring.liveBeansView.mbeanDomain' in any property source
::43.103 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Returning cached instance of singleton bean 'orderService'
zhou预订了订单
--------------------------------------方式2通过注解方式实现----------------------------------
1、创建IOrderDao
package com.longteng.lesson2.dao;
public interface IOrderDao {
public String bookOrder(String name);
}
2、创建IOrderDao的实体 OrderDao
package com.longteng.lesson2.dao.impl; import com.longteng.lesson2.dao.IOrderDao;
import org.springframework.stereotype.Repository; @Repository
public class OrderDao implements IOrderDao {
@Override
public String bookOrder(String name) {
return name+"预订了订单";
}
}
3、创建OrderService
package com.longteng.lesson2.service;
import com.longteng.lesson2.dao.IOrderDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy; @Service
public class OrderService { @Autowired
IOrderDao iOrderDao; public String getOrderInfo(String name){
return iOrderDao.bookOrder(name);
}
@PostConstruct
public void initTest(){
System.out.println("初始化调用方法");
}
@PreDestroy
public void tearDownTest(){
System.out.println("回收调用方法");
}
}
4、创建Order配置类
package com.longteng.lesson2.config; import com.longteng.lesson2.service.OrderService;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration; @Configuration
@ComponentScan(basePackages = {"com.longteng.lesson2.dao","com.longteng.lesson2.service"})
public class OrderCfg { @Bean
public OrderService getOrderService(){
return new OrderService();
}
}
5、创建测试类
package com.longteng.lesson2.dao; import com.longteng.lesson2.config.OrderCfg;
import com.longteng.lesson2.service.OrderService;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext; public class OrderDaoTest {
ApplicationContext context;
@Before
public void initApplication(){
context = new AnnotationConfigApplicationContext(OrderCfg.class);
}
@Test
public void test(){
OrderService orderService = context.getBean("orderService", OrderService.class);
System.out.println(orderService.getOrderInfo("zhou"));
Assert.assertEquals("成功了","zhou预订了订单",orderService.getOrderInfo("zhou"));
}
}
6、测试结果
::10.423 [main] DEBUG org.springframework.context.annotation.CommonAnnotationBeanPostProcessor - Invoking init method on bean 'getOrderService': public void com.longteng.lesson2.service.OrderService.initTest()
初始化调用方法
::10.423 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Finished creating instance of bean 'getOrderService'
::10.423 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Returning cached instance of singleton bean 'org.springframework.context.event.internalEventListenerFactory'
::10.444 [main] DEBUG org.springframework.context.annotation.AnnotationConfigApplicationContext - Unable to locate LifecycleProcessor with name 'lifecycleProcessor': using default [org.springframework.context.support.DefaultLifecycleProcessor@e31a9a]
::10.444 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Returning cached instance of singleton bean 'lifecycleProcessor'
::10.447 [main] DEBUG org.springframework.core.env.PropertySourcesPropertyResolver - Could not find key 'spring.liveBeansView.mbeanDomain' in any property source
::10.449 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Returning cached instance of singleton bean 'orderService'
zhou预订了订单 Process finished with exit code
IOC初始化销毁的2种实现方式的更多相关文章
- 🙈羞,Spring Bean 初始化/销毁竟然有这么多姿势
文章来源:http://1t.click/bfHN 一.前言 日常开发过程有时需要在应用启动之后加载某些资源,或者在应用关闭之前释放资源.Spring 框架提供相关功能,围绕 Spring Bean ...
- Spring中bean的初始化和销毁几种实现方式
Bean的生命周期 : 创建bean对象 – 属性赋值 – 初始化方法调用前的操作 – 初始化方法 – 初始化方法调用后的操作 – --- 销毁前操作 – 销毁方法的调用. [1]init-metho ...
- bean初始化和销毁的几种方式
Bean生命周期 Bean创建 -->初始化 -->销毁 1.自定义Bean初始化 和销毁的方法 init-method和destroy-method 创建Bike类 public cla ...
- Spring随笔 —— IOC配置的三种不同方式简介
在spring framework中,IOC的配置是最基础的部分,常见的配置方式有基于xml文件和基于注解的配置方式.除了这两种配置方式之外,今天这里再介绍另一种配置方式,先用小demo重温下我们熟悉 ...
- spring ioc三种注入方式
spring ioc三种注入方式 IOC ,全称 (Inverse Of Control) ,中文意思为:控制反转 什么是控制反转? 控制反转是一种将组件依赖关系的创建和管理置于程序外部的技术. 由容 ...
- Spring IOC 依赖注入的两种方式XML和注解
依赖注入的原理 依赖注入的方式---XML配置 依赖注入的方式---注解的方式 Spring 它的核心就是IOC和AOP.而IOC中实现Bean注入的实现方式之一就是DI(依赖注入). 一 DI的原理 ...
- Spring IOC 三种注入方式
1. 接口注入 2. setter注入 3. 构造器注入 对象与对象之间的关系可以简单的理解为对象之间的依赖关系:A类需要B类的一个实例来进行某些操作,比如在A类的方法中需要调用B类 ...
- Spring IOC以及三种注入方式
IOC是spring的最基础部分,也是核心模块,Spring的其他组件模块和应用开发都是以它为基础的.IOC把spring的面向接口编程和松耦合的思想体现的淋漓尽致. IOC概念 IOC(Invers ...
- JAVA中初始化ArrayList的三种方式
下面讲一下ArrayList初始化的几种不同方式. 一.最常用的初始化方式. List<String> list1 = new ArrayList<String>(); lis ...
随机推荐
- not in 优化 NOT EXISTS替代
优化前 SELECT id, loan_id, NAME FROM xw_baoxian_interchange WHERE , , ) , ) AND loan_id NOT IN ( SELECT ...
- 用IMX6开发板创建Android模拟器
基于迅为IMX6开发板 在 AndroidStudio 中,单击“Tools”->“Android”->“AVD Manager”选项.弹出 如下对话框,点击红色方框中的按钮. 弹出如下所 ...
- IaaS SaaS PaaS区别
- java使用forEach填充字典值
// 填充字典值 Vector vector = vectorMapper.selectByPrimaryKey(id); VectorModel vectorModel = new VectorMo ...
- Fractal Dimension|Relative Complexity|CG含量|重复序列|
生物信息学-序列拼接方法 物理学方法 Fractal Dimension of Exon and Intron Sequences --------------CGCGGCGTGTGTTATA --- ...
- 关于Ueditor富文本编辑器的配置和使用心得
一.环境和项目架构 本文章只是为了便于我个人记录日常笔记,如有错误或缺陷,请指出,文章仅供参考,如有转载请附上本文章链接. 介绍:将Ueditor富文本提交的内容直接生成Html文件,传到后台直接保存 ...
- 了解SSL证书从HTTPS开始 开发者绕不开的“劫”
微信小程序上线已经有很长一段时间了,而开发者在接入小程序的过程中,会遇到一些问题,例如小程序要求必须通过HTTPS完成服务端通信,开发者需搭建HTTPS服务,进行 SSL 证书申请.部署,完成HTTP ...
- Python笔记_第二篇_面向过程_第二部分_1.函数
函数:这个词属于一个数学概念,在编程语言借鉴了这个概念,表现形式是一段程序代码的组合,也叫“程序集”.有过编程基础的人很容易理解这个概念,当我们编写程序越来越多的时候,程序设计大师们会把散乱的程序进行 ...
- 微信H5支付demo
首先我们必须得在微信公众平台和微信商业平台那边配置好相关配置 1.注册微信服务号,开通微信支付权限绑定微信商业平台(这个具体怎么操作我就不说了) 2.获取应用(公众号)appid.应用(公众号)秘钥. ...
- 论文翻译——Deep contextualized word representations
Abstract We introduce a new type of deep contextualized word representation that models both (1) com ...