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种实现方式的更多相关文章

  1. 🙈羞,Spring Bean 初始化/销毁竟然有这么多姿势

    文章来源:http://1t.click/bfHN 一.前言 日常开发过程有时需要在应用启动之后加载某些资源,或者在应用关闭之前释放资源.Spring 框架提供相关功能,围绕 Spring Bean ...

  2. Spring中bean的初始化和销毁几种实现方式

    Bean的生命周期 : 创建bean对象 – 属性赋值 – 初始化方法调用前的操作 – 初始化方法 – 初始化方法调用后的操作 – --- 销毁前操作 – 销毁方法的调用. [1]init-metho ...

  3. bean初始化和销毁的几种方式

    Bean生命周期 Bean创建 -->初始化 -->销毁 1.自定义Bean初始化 和销毁的方法 init-method和destroy-method 创建Bike类 public cla ...

  4. Spring随笔 —— IOC配置的三种不同方式简介

    在spring framework中,IOC的配置是最基础的部分,常见的配置方式有基于xml文件和基于注解的配置方式.除了这两种配置方式之外,今天这里再介绍另一种配置方式,先用小demo重温下我们熟悉 ...

  5. spring ioc三种注入方式

    spring ioc三种注入方式 IOC ,全称 (Inverse Of Control) ,中文意思为:控制反转 什么是控制反转? 控制反转是一种将组件依赖关系的创建和管理置于程序外部的技术. 由容 ...

  6. Spring IOC 依赖注入的两种方式XML和注解

    依赖注入的原理 依赖注入的方式---XML配置 依赖注入的方式---注解的方式 Spring 它的核心就是IOC和AOP.而IOC中实现Bean注入的实现方式之一就是DI(依赖注入). 一 DI的原理 ...

  7. Spring IOC 三种注入方式

    1.    接口注入 2.    setter注入 3.    构造器注入 对象与对象之间的关系可以简单的理解为对象之间的依赖关系:A类需要B类的一个实例来进行某些操作,比如在A类的方法中需要调用B类 ...

  8. Spring IOC以及三种注入方式

    IOC是spring的最基础部分,也是核心模块,Spring的其他组件模块和应用开发都是以它为基础的.IOC把spring的面向接口编程和松耦合的思想体现的淋漓尽致. IOC概念 IOC(Invers ...

  9. JAVA中初始化ArrayList的三种方式

    下面讲一下ArrayList初始化的几种不同方式. 一.最常用的初始化方式. List<String> list1 = new ArrayList<String>(); lis ...

随机推荐

  1. 基因调控网络 (Gene Regulatory Network) 01

    本文为入门级的基因调控网络文章,主要介绍一些基本概念及常见的GRN模型. 概念:基因调控网络 (Gene Regulatory Network, GRN),简称调控网络,指细胞内或一个基因组内基因和基 ...

  2. 项目常用JS方法封装--奋斗的IT青年(微信公众号)

                                                                                                        ...

  3. dotnet core 链接mongodb

    导入命名空间 using MongoDB.Bson; using MongoDB.Driver; 测试示例: var client = new MongoClient("mongodb:// ...

  4. Gym102361E Escape

    Link 首先我们可以推出一些有用的结论: 1.任意两个机器人之间的路线不能重合,但是可以垂直交叉. 2.如果一个格子没有转向器,那么最多允许两个机器人以相互垂直的方向通过. 3.如果一个格子有转向器 ...

  5. PAT Advanced 1074 Reversing Linked List (25) [链表]

    题目 Given a constant K and a singly linked list L, you are supposed to reverse the links of every K e ...

  6. Tkinter控件Canvas

    网上关于tkinter的canvas组件系统的中文教程很少,英文教程未知.要么是专业的参考文档,没有丰富的实例,要么在不同的论坛,博客平台零零散散存在一些canvas的例子,这给学习canvas带来了 ...

  7. 关于PIL库Image模块的一些测试代码

    为了加深理解,写了一些代码测试,在这里记录一下吧: 关于图片的模式问题,之前做过笔记,有“1”,“L”,"P","RGB","RGBA",& ...

  8. python_pycharm控制台输出带颜色

    一.书写格式: 设置颜色开始:\033[显示方式;前景色;背景色m] 结束:\033[0m 二.颜色参数: 前景色 背景色 颜色 ----------------------------------- ...

  9. 【转】 java类的加载和执行顺序

    1.先执行Test类的静态代码块后执行Test类的main方法,说明要执行类的方法需要先加载这个类. 2.在创建ClassB的对象时,先去加载了父类ClassA.说明加载子类时如果没有加载父类,会先加 ...

  10. python通过wakeonlan唤醒内网电脑开机

    首先需要pip3 install wakeonlan 然后在电脑需要你的网卡支持网络唤醒计算机. 然后在主板BIOS开启支持唤醒. 在系统网卡属性里选上“允许计算机关闭此设备以节约电源”,“允许此设备 ...