package org.springframework.beans.factory.config; import org.springframework.beans.BeansException; public interface BeanFactoryPostProcessor { void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException; }
應用
package com.blueStarWei.processor; import org.springframework.beans.BeansException; import org.springframework.beans.factory.config.BeanDefinition; import org.springframework.beans.factory.config.BeanFactoryPostProcessor; import org.springframework.beans.factory.config.ConfigurableListableBeanFactory; import org.springframework.beans.factory.support.DefaultListableBeanFactory; import org.springframework.beans.factory.support.RootBeanDefinition; import org.springframework.stereotype.Component; @Component public class MyBeanFactoryPostProcessor implements BeanFactoryPostProcessor { public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException { // 註冊 BeanDefinition definition = new RootBeanDefinition(); definition.setBeanClassName("com.blueStarWei.ObjectBeanFactory"); //必須強制轉換為DefaultListableBeanFactory才能註冊\刪除 DefaultListableBeanFactory defaultListableBeanFactory = (DefaultListableBeanFactory) beanFactory; defaultListableBeanFactory.registerBeanDefinition("myBeanFactory",definition); //刪除 defaultListableBeanFactory.removeBeanDefinition("user"); //修改 BeanDefinition person = beanFactory.getBeanDefinition("person"); person.setScope(BeanDefinition.SCOPE_PROTOTYPE); } }
package com.blueStarWei.processor; import org.springframework.beans.BeansException; import org.springframework.beans.factory.config.BeanDefinition; import org.springframework.beans.factory.config.ConfigurableListableBeanFactory; import org.springframework.beans.factory.support.BeanDefinitionRegistry; import org.springframework.beans.factory.support.BeanDefinitionRegistryPostProcessor; import org.springframework.beans.factory.support.RootBeanDefinition; import org.springframework.stereotype.Component; @Component public class MyBeanDefinitionRegistryPostProcessor implements BeanDefinitionRegistryPostProcessor { public void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry registry) throws BeansException { System.out.println(">>>>>>>1. postProcessBeanDefinitionRegistry"); BeanDefinition definition = new RootBeanDefinition(); definition.setBeanClassName("com.blueStarWei.ObjectBeanFactory"); registry.registerBeanDefinition("myBeanFactory",definition); } public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException { System.out.println(">>>>>>>>>>2. postProcessBeanFactory"); } }
BeanPostProcessor
package org.springframework.beans.factory.config; import org.springframework.beans.BeansException; public interface BeanPostProcessor { /** * 在Bean的初始化之前執行 */ Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException; /** * 在Bean的初始化之後執行 */ Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException; }
應用
package com.blueStarWei.processor; import org.springframework.beans.BeansException; import org.springframework.beans.factory.config.BeanPostProcessor; import java.lang.reflect.Proxy; public class MyBeanPostProcessor implements BeanPostProcessor { @Override public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException { return bean; } /** * 列印方法呼叫時間紀錄檔 * 注意:使用jdk自帶的Proxy只能對有介面的類進行動態代理,直接代理POJO會報型別ClassCastException * */ @Override public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException { Object beanProxy = Proxy.newProxyInstance( bean.getClass().getClassLoader(), bean.getClass().getInterfaces(), (proxy, method, args) -> { long startTime = System.currentTimeMillis(); Object result = method.invoke(bean, args); long endTime = System.currentTimeMillis(); System.out.println(beanName + "." + method.getName() + "執行時間:" + (endTime - startTime) + "ms."); return result; } ); return beanProxy; } }