Skip to content

Commit

Permalink
Merge branch 'main' of github.com:DerekYRC/mini-spring
Browse files Browse the repository at this point in the history
  • Loading branch information
DerekYRC committed Jul 5, 2022
2 parents df3bb7c + 8ed8a83 commit 0164b61
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 4 deletions.
4 changes: 2 additions & 2 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -280,10 +280,10 @@ public class XmlFileDefineBeanTest {
}
```

## [BeanFactoryPostProcess和BeanPostProcessor](#BeanFactoryPostProcess和BeanPostProcessor)
## [BeanFactoryPostProcessor和BeanPostProcessor](#BeanFactoryPostProcessor和BeanPostProcessor)
> 代码分支:bean-factory-post-processor-and-bean-post-processor
BeanFactoryPostProcess和BeanPostProcessor是spring框架中具有重量级地位的两个接口,理解了这两个接口的作用,基本就理解spring的核心原理了。为了降低理解难度分两个小节实现。
BeanFactoryPostProcessor和BeanPostProcessor是spring框架中具有重量级地位的两个接口,理解了这两个接口的作用,基本就理解spring的核心原理了。为了降低理解难度分两个小节实现。

BeanFactoryPostProcessor是spring提供的容器扩展机制,允许我们在bean实例化之前修改bean的定义信息即BeanDefinition的信息。其重要的实现类有PropertyPlaceholderConfigurer和CustomEditorConfigurer,PropertyPlaceholderConfigurer的作用是用properties文件的配置值替换xml文件中的占位符,CustomEditorConfigurer的作用是实现类型转换。BeanFactoryPostProcessor的实现比较简单,看单元测试BeanFactoryPostProcessorAndBeanPostProcessorTest#testBeanFactoryPostProcessor追下代码。

Expand Down
6 changes: 6 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -62,5 +62,11 @@
<scope>test</scope>
</dependency>

<dependency>
<groupId>cglib</groupId>
<artifactId>cglib</artifactId>
<version>3.3.0</version>
</dependency>

</dependencies>
</project>
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package org.springframework.beans.factory.support;

import net.sf.cglib.proxy.Enhancer;
import net.sf.cglib.proxy.MethodInterceptor;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanDefinition;

Expand All @@ -18,7 +20,9 @@ public class CglibSubclassingInstantiationStrategy implements InstantiationStrat
*/
@Override
public Object instantiate(BeanDefinition beanDefinition) throws BeansException {
//TODO 感兴趣的小伙伴可以实现下
throw new UnsupportedOperationException("CGLIB instantiation strategy is not supported");
Enhancer enhancer = new Enhancer();
enhancer.setSuperclass(beanDefinition.getBeanClass());
enhancer.setCallback((MethodInterceptor) (obj, method, argsTemp, proxy) -> proxy.invokeSuper(obj,argsTemp));
return enhancer.create();
}
}
13 changes: 13 additions & 0 deletions src/test/java/org/springframework/test/ioc/HelloService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package org.springframework.test.ioc;

/**
* @author derekyi
* @date 2020/11/22
*/
public class HelloService {

public String sayHello() {
System.out.println("hello");
return "hello";
}
}

0 comments on commit 0164b61

Please sign in to comment.