美文网首页
bean的生命周期

bean的生命周期

作者: AAnna珠 | 来源:发表于2019-04-25 18:42 被阅读0次

    配置文件

    <?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的生命周期 -->
        <bean id="customerService" class="com.neuedu.demo6.CustomerServiceImpl"
            init-method="setup" destroy-method="teardown">
            <property name="name" value="neuedu"></property>
        </bean>
        <bean class="com.neuedu.demo6.MyBeanPostProcessor"></bean>
    </beans>
    

    接口

    package com.neuedu.demo6;
    
    public interface CustomerService {
        public void add();
        public void find();
    }
    
    

    实现类

    package com.neuedu.demo6;
    
    import org.springframework.beans.BeansException;
    import org.springframework.beans.factory.BeanNameAware;
    import org.springframework.beans.factory.DisposableBean;
    import org.springframework.beans.factory.InitializingBean;
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.ApplicationContextAware;
    
    public class CustomerServiceImpl implements CustomerService,BeanNameAware,ApplicationContextAware,InitializingBean,DisposableBean     {
        
        private String name;
        
        public CustomerServiceImpl(){
            System.out.println("第一步:实例化类");
        }
        
        public void setName(String name) {
            this.name = name;
            System.out.println("第二步:属性的注入");
        }
    
        
    
        public void add() {
            System.out.println("添加客户。。。");
        }
    
        public void find() {
            System.out.println("查询客户。。。");
        }
    
        @Override
        public void setBeanName(String bean) {
            System.out.println("第三步:注入配之类的名称"+bean);
        }
    
        @Override
        public void setApplicationContext(ApplicationContext arg0) throws BeansException {
            System.out.println("第四步:注入applicationContext"+arg0);
        }
    
        @Override
        public void afterPropertiesSet() throws Exception {
            System.out.println("第六步:属性设置后执行");
        }
    
        public void setup(){
            System.out.println("第七步:调用手动设置的初始化方法");
        }
    
        @Override
        public void destroy() throws Exception {
            System.out.println("第十步:调用销毁方法");
        }
        
        public void teardown(){
            System.out.println("第十一步:调用手动销毁方法!!");
        }
    }
    
    

    初始化类

    package com.neuedu.demo6;
    
    import org.springframework.beans.BeansException;
    import org.springframework.beans.factory.config.BeanPostProcessor;
    
    public class MyBeanPostProcessor implements BeanPostProcessor {
    
        @Override
        public Object postProcessBeforeInitialization(Object bean, String arg1) throws BeansException {
            System.out.println("第五步:初始化之前执行");
            return bean;
        }
        
        @Override
        public Object postProcessAfterInitialization(Object bean, String arg1) throws BeansException {
            System.out.println("第八步:初始化之后执行");
            return bean;
        }
    
        
    
    }
    
    

    测试类

    package com.neuedu.demo6;
    
    import org.junit.Test;
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    
    
    public class SpringTest6 {
    
        @Test
        public void demo1(){
            ClassPathXmlApplicationContext applicationContext = 
                    new ClassPathXmlApplicationContext("applicationContext3.xml");
            CustomerService customerService = 
                    (CustomerServiceImpl) applicationContext.getBean("customerService");
            customerService.add();
            customerService.find();
            applicationContext.close();
        }
        
        
    }
    
    

    相关文章

      网友评论

          本文标题:bean的生命周期

          本文链接:https://www.haomeiwen.com/subject/zlqtnqtx.html