• 微信公众号:美女很有趣。 工作之余,放松一下,关注即送10G+美女照片!

Spring-模拟InitializingBean

开发技术 开发技术 2周前 (05-01) 4次浏览

代码结构:

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();
    }

结果:

Spring-模拟InitializingBean

 


程序员灯塔
转载请注明原文链接:Spring-模拟InitializingBean
喜欢 (0)