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、测试结果

13:53:43.096 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Invoking init method  'initTest' on bean with name 'orderService'
初始化调用方法
13:53:43.097 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Finished creating instance of bean 'orderService'
13:53:43.098 [main] DEBUG org.springframework.context.support.ClassPathXmlApplicationContext - Unable to locate LifecycleProcessor with name 'lifecycleProcessor': using default [org.springframework.context.support.DefaultLifecycleProcessor@d7f7e0]
13:53:43.099 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Returning cached instance of singleton bean 'lifecycleProcessor'
13:53:43.101 [main] DEBUG org.springframework.core.env.PropertySourcesPropertyResolver - Could not find key 'spring.liveBeansView.mbeanDomain' in any property source
13:53:43.103 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Returning cached instance of singleton bean 'orderService'
zhou预订了订单
123456789

--------------------------------------方式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、测试结果

14:03:10.423 [main] DEBUG org.springframework.context.annotation.CommonAnnotationBeanPostProcessor - Invoking init method on bean 'getOrderService': public void com.longteng.lesson2.service.OrderService.initTest()
初始化调用方法
14:03:10.423 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Finished creating instance of bean 'getOrderService'
14:03:10.423 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Returning cached instance of singleton bean 'org.springframework.context.event.internalEventListenerFactory'
14:03:10.444 [main] DEBUG org.springframework.context.annotation.AnnotationConfigApplicationContext - Unable to locate LifecycleProcessor with name 'lifecycleProcessor': using default [org.springframework.context.support.DefaultLifecycleProcessor@e31a9a]
14:03:10.444 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Returning cached instance of singleton bean 'lifecycleProcessor'
14:03:10.447 [main] DEBUG org.springframework.core.env.PropertySourcesPropertyResolver - Could not find key 'spring.liveBeansView.mbeanDomain' in any property source
14:03:10.449 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Returning cached instance of singleton bean 'orderService'
zhou预订了订单

Process finished with exit code 0
原文地址:https://www.cnblogs.com/zhou-test/p/ioc.html