zoukankan      html  css  js  c++  java
  • Spring-模拟InitializingBean

    代码结构:

    package com.java.spring;
    
    /**
     * 自定义InitializingBean
     * 作用:做初始化操作
     */
    public interface IInitializingBean {
        void afterPropertiesSet() throws Exception;
    }
    package com.java.service;
    
            import com.java.spring.CustomizeAutowired;
            import com.java.spring.CustomizeComponent;
            import com.java.spring.CustomizeScope;
            import com.java.spring.IInitializingBean;
    
    @CustomizeComponent("userService")
    @CustomizeScope("prototype")
    public class UserService implements IInitializingBean {
    
        @CustomizeAutowired
        public OrderService orderService;
    
        public void doSomething(){
            orderService.doPBeanName();
        }
    
        @Override
        public void afterPropertiesSet() throws Exception {
            System.out.println("初始化操作.........");
        }
    }
    CustomizeApplicationContext
    /**
         * 创基bean
         * @param beanName
         * @return
         */
        public Object createBean(String beanName,CustomizeBeanDefinition beanDefinition){
            Class clazz = beanDefinition.getClazz();
            Object instance = null;
    
            try {
                instance = clazz.getDeclaredConstructor().newInstance();
    
                //依赖注入,给属性赋值
                //获取类中所有属性
                Field[] filelds = clazz.getDeclaredFields();
                for(Field f : filelds){
                    //如果有定义的注入注解
                    if(f.isAnnotationPresent(CustomizeAutowired.class)){
                        //根据属性名去找
                        String fBeanName = f.getName();
    
                        Object fBean = getBean(fBeanName);
                        CustomizeAutowired customizeAutowired = f.getDeclaredAnnotation(CustomizeAutowired.class);
                        if(customizeAutowired.required() && null == fBean){
                            //如果是必须
                            throw new NullPointerException();
                        }
    
                        //由于属性为私有属性,需要通过反射方式赋值,故设置true
                        f.setAccessible(true);
                        //将对象赋值给属性
                        f.set(instance,fBean);
                    }
                }
    
    
                //Aware 回调
                if(instance instanceof IBeanNameAware){
                    ((IBeanNameAware) instance).setBeanName(beanName);
                }
    
                //初始化操作
                if(instance instanceof IInitializingBean){
                    ((IInitializingBean) instance).afterPropertiesSet();
                }
    
            } catch (InstantiationException e) {
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            } catch (InvocationTargetException e) {
                e.printStackTrace();
            } catch (NoSuchMethodException e) {
                e.printStackTrace();
            } catch (Exception e) {
                e.printStackTrace();
            }
            return instance;
        }

    测试:

     public static void main(String[] args) {
            SpringLoader loaderTest = new SpringLoader();
            loaderTest.test2();
        }
    
        /**
         * 依赖注入测试
         */
        private void test2(){
            CustomizeApplicationContext context = new CustomizeApplicationContext(CustomizeConfig.class);
            UserService userService1 = (UserService) context.getBean("userService");
            userService1.doSomething();
        }

    结果:

  • 相关阅读:
    面试问题总结
    2016后半年读书系统
    java基础知识
    自动化测试的误区
    软件测试的艺术
    QTP
    软件测试的艺术读书笔记
    Jquery中$(document).ready()
    Python爬取糗事百科示例代码
    【转载】气象数据相关资源
  • 原文地址:https://www.cnblogs.com/xiaozhuanfeng/p/14724306.html
Copyright © 2011-2022 走看看