├── .gitignore
├── README.md
├── pom.xml
└── src
├── main
├── java
│ ├── aop
│ │ ├── AfterReturningAdvice.java
│ │ ├── AopProxy.java
│ │ ├── CglibAopPorxy.java
│ │ ├── DelegatingIntroductionInterceptor.java
│ │ ├── JdkDynamicAopProxy.java
│ │ ├── MethodBeforeAdvice.java
│ │ ├── ProxyConfig.java
│ │ ├── ProxyFactory.java
│ │ ├── ProxyFactoryBean.java
│ │ ├── ReflectiveMethodInvocation.java
│ │ └── adapter
│ │ │ ├── AfterReturningAdviceInterceptor.java
│ │ │ └── MethodBeforeAdviceInterceptor.java
│ ├── bean
│ │ ├── BeanDefinition.java
│ │ ├── ConstructorValue.java
│ │ ├── ConstructorValues.java
│ │ ├── PropertyValue.java
│ │ └── PropertyValues.java
│ ├── context
│ │ ├── AbstractApplicationContext.java
│ │ ├── ApplicationContext.java
│ │ └── ClassPathXmlApplicationContext.java
│ ├── entity
│ │ ├── HelloWorldA.java
│ │ ├── HelloWorldB.java
│ │ └── HelloWorldService.java
│ ├── exception
│ │ └── BeanCurrentlyInCreationException.java
│ ├── factory
│ │ ├── AbstractBeanFactory.java
│ │ ├── AutowireCapableBeanFactory.java
│ │ ├── Aware.java
│ │ ├── BeanFactory.java
│ │ ├── BeanFactoryAware.java
│ │ ├── FactoryBean.java
│ │ └── XMLBeanFactory.java
│ ├── reader
│ │ └── XMLBeanDefinitionReader.java
│ └── resource
│ │ └── ClassPathResource.java
└── resources
│ └── beanFactoryTest.xml
└── test
├── java
├── SpringAopTest.java
├── SpringApplicationContextTest.java
├── SpringTest.java
├── aopAdvice
│ ├── PerformerAfterAdvice.java
│ ├── PerformerAroundAdvice.java
│ └── PerformerBeforeAdvice.java
├── aopEntity
│ ├── Apology.java
│ ├── GreetingIntroAdvice.java
│ ├── NiceJudge.java
│ ├── Performer.java
│ └── Singer.java
└── exception
│ └── PerformanceException.java
└── resources
├── DelegatingIntroductionInterceptor.xml
├── ProxyFactoryBean.xml
└── spring-idol.xml
/.gitignore:
--------------------------------------------------------------------------------
1 | target/
2 |
3 | # Compiled class file
4 | *.class
5 | .idea/
6 | *.iml
7 |
8 | # Log file
9 | *.log
10 |
11 | # BlueJ files
12 | *.ctxt
13 |
14 | # Mobile Tools for Java (J2ME)
15 | .mtj.tmp/
16 |
17 | # Package Files #
18 | *.jar
19 | *.war
20 | *.nar
21 | *.ear
22 | *.zip
23 | *.tar.gz
24 | *.rar
25 |
26 | # virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
27 | hs_err_pid*
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # tiny-spring
2 | 本项目是一个精简版的spring,通过自己实现一遍spring来理解spring框架的精华。
3 |
4 | ## IOC计划
5 | * [x] **最基本的容器:** 使用BeanFactory来获取bean,使用BeanDefinition来封装bean对象。
6 | * [x] **将bean创建放入工厂:** 设置beanClass的名称,通过反射来创建bean。
7 | * [x] **为bean注入属性:** 使用PropertyValue对象来存储bean的属性信息,使用ConstructorValue来存储构造器属性信息。
8 | * [x] **读取xml配置来初始化bean:** 引用XMLBeanDefinitionReader来读取xml中的信息,并将xml中的信息转化为BeanDefinition和PropertyValue
9 | * [x] **实现ApplicationContext:** 自动初始化所有bean
10 |
11 |
12 | ## AOP计划
13 | aop的实现遵守的[aop联盟](http://aopalliance.sourceforge.net/)的规定,所以需要先引入[aopalliance.jar](https://mvnrepository.com/artifact/aopalliance/aopalliance)。
14 | * [x] **实现简单版的jdk和cglib的动态代理:** [jdk动态代理](https://blog.csdn.net/wangdong5678999/article/details/72801623)通过InvocationHandler实现,缺点是必须需要代理的类必须实现了接口才能被jdk代理;[CGLIB](https://github.com/cglib/cglib)是一个基于ASM的字节码生成库,它允许我们在运行时对字节码进行修改和动态生成,CGLIB通过继承方式实现代理。
15 | * [x] **通过ProxyFactoryBean来实现aop和ioc的结合。**
16 | 1. 实现BeanFactoryAware接口,将beanFactory注入到ProxyFactoryBean中
17 | 2. 实现FactoryBean,获取代理对象
18 | * [x] **实现引入:** 对类的功能增强,实现原来类未实现的接口
19 | * [ ] **实现切面功能:** 即通知点和通知的结合
20 | * [ ] **实现aop自动代理,扫描 Bean 名称**
21 | * [ ] **接入aspectj,实现注解式配置切面**
22 |
23 | ## 收获
24 | 1. 对java中的一些概念有了更深入的理解,比如java内省、[javaBean](https://blog.csdn.net/zhuwenchao90/article/details/54893253) 等。
25 | 2. 阅读源码能力得到增强,现在读spring的源码,再也不会迷路了。
26 | 3. 当自己写好一个功能后,再去看看spring是怎么实现的,会有种恍然大悟的感觉!
27 |
--------------------------------------------------------------------------------
/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
5 | 4.0.0
6 |
7 | com.dong
8 | tiny-spring
9 | 1.0-SNAPSHOT
10 |
11 |
12 |
13 | org.apache.maven.plugins
14 | maven-compiler-plugin
15 |
16 | 8
17 | 8
18 |
19 |
20 |
21 |
22 |
23 |
24 | junit
25 | junit
26 | 4.12
27 | test
28 |
29 |
30 | org.dom4j
31 | dom4j
32 | 2.1.1
33 |
34 |
35 | org.projectlombok
36 | lombok
37 | 1.18.4
38 | provided
39 |
40 |
41 |
42 | aopalliance
43 | aopalliance
44 | 1.0
45 |
46 |
47 | cglib
48 | cglib
49 | 2.2.2
50 |
51 |
52 | commons-beanutils
53 | commons-beanutils
54 | 1.9.3
55 |
56 |
57 |
58 |
59 |
60 |
--------------------------------------------------------------------------------
/src/main/java/aop/AfterReturningAdvice.java:
--------------------------------------------------------------------------------
1 | package aop;
2 |
3 | import org.aopalliance.aop.Advice;
4 |
5 | import java.lang.reflect.Method;
6 |
7 | public interface AfterReturningAdvice extends Advice {
8 | void afterReturning(Object returnValue, Method method, Object[] args, Object target) throws Throwable;
9 | }
10 |
--------------------------------------------------------------------------------
/src/main/java/aop/AopProxy.java:
--------------------------------------------------------------------------------
1 | package aop;
2 |
3 | public interface AopProxy {
4 |
5 | /**
6 | * Create a new proxy object.
7 | *
Uses the AopProxy's default class loader (if necessary for proxy creation):
8 | * usually, the thread context class loader.
9 | * @return the new proxy object (never {@code null})
10 | * @see Thread#getContextClassLoader()
11 | */
12 | Object getProxy();
13 |
14 | /**
15 | * Create a new proxy object.
16 | *
Uses the given class loader (if necessary for proxy creation).
17 | * {@code null} will simply be passed down and thus lead to the low-level
18 | * proxy facility's default, which is usually different from the default chosen
19 | * by the AopProxy implementation's {@link #getProxy()} method.
20 | * @param classLoader the class loader to create the proxy with
21 | * (or {@code null} for the low-level proxy facility's default)
22 | * @return the new proxy object (never {@code null})
23 | */
24 | Object getProxy(ClassLoader classLoader);
25 |
26 | }
27 |
--------------------------------------------------------------------------------
/src/main/java/aop/CglibAopPorxy.java:
--------------------------------------------------------------------------------
1 | package aop;
2 |
3 | import lombok.Data;
4 | import net.sf.cglib.proxy.Enhancer;
5 | import net.sf.cglib.proxy.MethodInterceptor;
6 | import net.sf.cglib.proxy.MethodProxy;
7 |
8 | import java.lang.reflect.Method;
9 |
10 | @Data
11 | public class CglibAopPorxy implements MethodInterceptor, AopProxy {
12 | private ProxyFactory proxyFactory;
13 |
14 | public CglibAopPorxy(ProxyFactory proxyFactory) {
15 | this.proxyFactory = proxyFactory;
16 | }
17 |
18 | @Override
19 | public Object intercept(Object o, Method method, Object[] args, MethodProxy methodProxy) throws Throwable {
20 | return new ReflectiveMethodInvocation(proxyFactory.getProxy(),proxyFactory.getTarget(),method,args,proxyFactory.getTarget().getClass(),proxyFactory.getMethodInterceptors()).proceed();
21 | }
22 |
23 | @Override
24 | public Object getProxy() {
25 | return Enhancer.create(proxyFactory.getTarget().getClass(), proxyFactory.getInterfaces(),this);
26 | }
27 |
28 | @Override
29 | public Object getProxy(ClassLoader classLoader) {
30 | return Enhancer.create(proxyFactory.getTarget().getClass(), this);
31 | }
32 | }
33 |
--------------------------------------------------------------------------------
/src/main/java/aop/DelegatingIntroductionInterceptor.java:
--------------------------------------------------------------------------------
1 | package aop;
2 |
3 | import org.aopalliance.intercept.MethodInterceptor;
4 | import org.aopalliance.intercept.MethodInvocation;
5 |
6 | import java.lang.reflect.Method;
7 | import java.util.ArrayList;
8 | import java.util.Arrays;
9 | import java.util.List;
10 |
11 | /**
12 | * 引入的实现
13 | */
14 | public class DelegatingIntroductionInterceptor implements MethodInterceptor {
15 |
16 |
17 | private Object delegate;
18 | private List supportInterfaces = new ArrayList<>();
19 | private List supportMethods = new ArrayList<>();
20 |
21 |
22 | public DelegatingIntroductionInterceptor(Object delegate) {
23 | init(delegate);
24 | }
25 |
26 | public DelegatingIntroductionInterceptor() {
27 | init(this);
28 | }
29 |
30 | private void init(Object delegate) {
31 | this.delegate = delegate;
32 | supportInterfaces = Arrays.asList(delegate.getClass().getInterfaces());
33 | supportMethods = Arrays.asList(delegate.getClass().getMethods());
34 |
35 | }
36 |
37 | public Object getDelegate() {
38 | return delegate;
39 | }
40 |
41 | public void setDelegate(Object delegate) {
42 | this.delegate = delegate;
43 | }
44 |
45 | @Override
46 | public Object invoke(MethodInvocation invocation) throws Throwable {
47 | if (supportInterfaces.contains(invocation.getMethod().getDeclaringClass())){
48 | Method method = invocation.getMethod();
49 | method.setAccessible(true);
50 |
51 | return method.invoke(delegate, invocation.getArguments());
52 | }
53 | return invocation.proceed();
54 | }
55 | }
56 |
--------------------------------------------------------------------------------
/src/main/java/aop/JdkDynamicAopProxy.java:
--------------------------------------------------------------------------------
1 | package aop;
2 |
3 | import java.lang.reflect.InvocationHandler;
4 | import java.lang.reflect.Method;
5 | import java.lang.reflect.Proxy;
6 |
7 | public class JdkDynamicAopProxy implements InvocationHandler,AopProxy {
8 | private ProxyFactory proxyFactory;
9 |
10 | JdkDynamicAopProxy(ProxyFactory proxyFactory) {
11 | this.proxyFactory = proxyFactory;
12 | }
13 |
14 | @Override
15 | public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
16 | return new ReflectiveMethodInvocation(proxyFactory.getTarget(),proxyFactory.getTarget(),method,args,proxyFactory.getTarget().getClass(),proxyFactory.getMethodInterceptors()).proceed();
17 | }
18 |
19 | @Override
20 | public Object getProxy() {
21 | return Proxy.newProxyInstance(proxyFactory.getTarget().getClass().getClassLoader(),proxyFactory.getTarget().getClass().getInterfaces(),this);
22 |
23 | }
24 |
25 | @Override
26 | public Object getProxy(ClassLoader classLoader) {
27 | return Proxy.newProxyInstance(classLoader,proxyFactory.getTarget().getClass().getInterfaces(),this);
28 | }
29 | }
--------------------------------------------------------------------------------
/src/main/java/aop/MethodBeforeAdvice.java:
--------------------------------------------------------------------------------
1 | package aop;
2 |
3 | import org.aopalliance.aop.Advice;
4 |
5 | import java.lang.reflect.Method;
6 |
7 | /**
8 | * aop前置增强接口
9 | */
10 | public interface MethodBeforeAdvice extends Advice {
11 | void before(Method method, Object[] args, Object target) throws Throwable;
12 | }
13 |
--------------------------------------------------------------------------------
/src/main/java/aop/ProxyConfig.java:
--------------------------------------------------------------------------------
1 | package aop;
2 |
3 | import lombok.Data;
4 | import org.aopalliance.aop.Advice;
5 |
6 | import java.util.ArrayList;
7 | import java.util.List;
8 |
9 | @Data
10 | public class ProxyConfig {
11 | List beforeAdvices = new ArrayList<>();
12 | List afterReturningAdvices = new ArrayList<>();
13 | List aroundAdvices = new ArrayList<>();
14 |
15 | Object target;
16 | }
17 |
--------------------------------------------------------------------------------
/src/main/java/aop/ProxyFactory.java:
--------------------------------------------------------------------------------
1 | package aop;
2 |
3 | import aop.adapter.AfterReturningAdviceInterceptor;
4 | import aop.adapter.MethodBeforeAdviceInterceptor;
5 | import lombok.Data;
6 | import org.aopalliance.aop.Advice;
7 | import org.aopalliance.intercept.MethodInterceptor;
8 |
9 | import java.util.ArrayList;
10 | import java.util.List;
11 |
12 | @Data
13 | public class ProxyFactory{
14 | // List beforeAdvices = new ArrayList<>();
15 | // List afterReturningAdvices = new ArrayList<>();
16 | // List aroundAdvices = new ArrayList<>();
17 |
18 | List methodInterceptors = new ArrayList<>();
19 | Object target;
20 | Class[] interfaces;
21 | boolean proxyTargetClass = false;
22 |
23 | public Object getProxy(){
24 | return createAopProxy().getProxy();
25 | }
26 |
27 | /**
28 | * 若是代理接口,使用jdk代理,如果没有接口,通过cglib代理
29 | * @return
30 | */
31 | private AopProxy createAopProxy() {
32 | if (proxyTargetClass) {
33 | return new CglibAopPorxy(this);
34 | }
35 | if (target.getClass().isInterface() || target.getClass().getInterfaces().length != 0){
36 | return new JdkDynamicAopProxy(this);
37 | }
38 | return new CglibAopPorxy(this);
39 | }
40 |
41 | public void addAdvice(Advice advice){
42 | if (advice instanceof MethodBeforeAdvice){
43 | methodInterceptors.add(new MethodBeforeAdviceInterceptor((MethodBeforeAdvice)advice));
44 | }
45 | if (advice instanceof AfterReturningAdvice){
46 | methodInterceptors.add(new AfterReturningAdviceInterceptor((AfterReturningAdvice)advice));
47 | }
48 |
49 | if (advice instanceof MethodInterceptor){
50 | methodInterceptors.add((MethodInterceptor)advice);
51 | }
52 |
53 | }
54 | }
55 |
--------------------------------------------------------------------------------
/src/main/java/aop/ProxyFactoryBean.java:
--------------------------------------------------------------------------------
1 | package aop;
2 |
3 | import factory.BeanFactory;
4 | import factory.BeanFactoryAware;
5 | import factory.FactoryBean;
6 | import lombok.Data;
7 | import lombok.EqualsAndHashCode;
8 | import org.aopalliance.aop.Advice;
9 |
10 | @EqualsAndHashCode(callSuper = true)
11 | @Data
12 | public class ProxyFactoryBean extends ProxyFactory implements FactoryBean, BeanFactoryAware {
13 | private boolean singleton = true;
14 | private BeanFactory beanFactory;
15 | private String afterReturningAdvice;
16 | private String beforeAdvice;
17 | private String methodInterceptor;
18 |
19 | @Override
20 | public Object getObject() {
21 | initialInterceptorChain();
22 | return super.getProxy();
23 | }
24 |
25 | private void initialInterceptorChain() {
26 | addAdvice((Advice) beanFactory.getBean(afterReturningAdvice));
27 | addAdvice((Advice) beanFactory.getBean(beforeAdvice));
28 | addAdvice((Advice) beanFactory.getBean(methodInterceptor));
29 | // interceptorNames.forEach(interceptorNames -> addAdvice((Advice) beanFactory.getBean(interceptorNames)));
30 | }
31 |
32 | @Override
33 | public Class> getObjectType() {
34 | return super.getClass();
35 | }
36 |
37 | @Override
38 | public boolean isSingleton() {
39 | return singleton;
40 | }
41 |
42 |
43 | @Override
44 | public void setBeanFactory(BeanFactory beanFactory) {
45 | this.beanFactory = beanFactory;
46 | }
47 | }
48 |
--------------------------------------------------------------------------------
/src/main/java/aop/ReflectiveMethodInvocation.java:
--------------------------------------------------------------------------------
1 | package aop;
2 |
3 | import lombok.Data;
4 | import org.aopalliance.intercept.MethodInterceptor;
5 | import org.aopalliance.intercept.MethodInvocation;
6 |
7 | import java.lang.reflect.AccessibleObject;
8 | import java.lang.reflect.Method;
9 | import java.util.List;
10 | import java.util.Map;
11 |
12 | @Data
13 | public class ReflectiveMethodInvocation implements MethodInvocation {
14 |
15 | protected final Object proxy;
16 |
17 | protected final Object target;
18 |
19 | /**
20 | * 要调用的方法
21 | */
22 | protected final Method method;
23 |
24 | /**
25 | * 方法参数
26 | */
27 | protected Object[] arguments;
28 |
29 | /**
30 | *
31 | */
32 | private final Class targetClass;
33 |
34 | private Map userAttributes;
35 |
36 | /**
37 | * 拦截器的list
38 | */
39 | protected final List interceptorsAndDynamicMethodMatchers;
40 |
41 |
42 | private int currentInterceptorIndex = -1;
43 |
44 | public ReflectiveMethodInvocation(Object proxy, Object target, Method method, Object[] arguments, Class targetClass, List interceptorsAndDynamicMethodMatchers) {
45 | this.proxy = proxy;
46 | this.target = target;
47 | this.method = method;
48 | this.arguments = arguments;
49 | this.targetClass = targetClass;
50 | this.interceptorsAndDynamicMethodMatchers = interceptorsAndDynamicMethodMatchers;
51 | }
52 |
53 | @Override
54 | public Object proceed() throws Throwable {
55 | //没有拦截器,直接调用方法
56 | if (this.currentInterceptorIndex == this.interceptorsAndDynamicMethodMatchers.size() - 1) {
57 | method.setAccessible(true);
58 |
59 | return method.invoke(target, arguments);
60 | }
61 | Object interceptorOrInterceptionAdvice =
62 | this.interceptorsAndDynamicMethodMatchers.get(++this.currentInterceptorIndex);
63 | return ((MethodInterceptor) interceptorOrInterceptionAdvice).invoke(this);
64 | }
65 |
66 | @Override
67 | public Object getThis() {
68 | return this.target;
69 | }
70 |
71 | @Override
72 | public AccessibleObject getStaticPart() {
73 | return this.method;
74 | }
75 | }
76 |
--------------------------------------------------------------------------------
/src/main/java/aop/adapter/AfterReturningAdviceInterceptor.java:
--------------------------------------------------------------------------------
1 | package aop.adapter;
2 |
3 | import aop.AfterReturningAdvice;
4 | import org.aopalliance.aop.Advice;
5 | import org.aopalliance.intercept.MethodInterceptor;
6 | import org.aopalliance.intercept.MethodInvocation;
7 |
8 | import java.io.Serializable;
9 |
10 | public class AfterReturningAdviceInterceptor implements MethodInterceptor, Advice, Serializable {
11 |
12 | private final AfterReturningAdvice advice;
13 |
14 | /**
15 | * Create a new AfterReturningAdviceInterceptor for the given advice.
16 | * @param advice the AfterReturningAdvice to wrap
17 | */
18 | public AfterReturningAdviceInterceptor(AfterReturningAdvice advice) {
19 | this.advice = advice;
20 | }
21 |
22 | public Object invoke(MethodInvocation mi) throws Throwable {
23 | Object retVal = mi.proceed();
24 | this.advice.afterReturning(retVal, mi.getMethod(), mi.getArguments(), mi.getThis());
25 | return retVal;
26 | }
27 | }
--------------------------------------------------------------------------------
/src/main/java/aop/adapter/MethodBeforeAdviceInterceptor.java:
--------------------------------------------------------------------------------
1 | package aop.adapter;
2 |
3 | import aop.MethodBeforeAdvice;
4 | import org.aopalliance.aop.Advice;
5 | import org.aopalliance.intercept.MethodInterceptor;
6 | import org.aopalliance.intercept.MethodInvocation;
7 |
8 | import java.io.Serializable;
9 |
10 | public class MethodBeforeAdviceInterceptor implements MethodInterceptor, Advice, Serializable {
11 | private final MethodBeforeAdvice advice;
12 |
13 | public MethodBeforeAdviceInterceptor(MethodBeforeAdvice advice) {
14 | this.advice = advice;
15 | }
16 |
17 | @Override
18 | public Object invoke(MethodInvocation invocation) throws Throwable {
19 | advice.before(invocation.getMethod(),invocation.getArguments(),invocation.getThis());
20 | return invocation.proceed();
21 | }
22 | }
23 |
--------------------------------------------------------------------------------
/src/main/java/bean/BeanDefinition.java:
--------------------------------------------------------------------------------
1 | package bean;
2 |
3 | import factory.BeanFactory;
4 | import factory.BeanFactoryAware;
5 | import factory.FactoryBean;
6 | import lombok.Data;
7 | import org.apache.commons.beanutils.BeanUtils;
8 |
9 | import java.lang.reflect.Constructor;
10 | import java.lang.reflect.Field;
11 | import java.lang.reflect.InvocationTargetException;
12 | import java.util.ArrayList;
13 | import java.util.Arrays;
14 | import java.util.HashSet;
15 | import java.util.List;
16 |
17 | @Data
18 | public class BeanDefinition {
19 | private Object object;
20 | private String beanClassName;
21 | private String beanId;
22 | private PropertyValues propertyValues;
23 | private ConstructorValues constructorValues;
24 | private BeanFactory beanFactory;
25 |
26 | private List dependOnClassList = new ArrayList<>();
27 |
28 | public ConstructorValues getConstructorValues() {
29 | return constructorValues;
30 | }
31 |
32 | public void setConstructorValues(ConstructorValues constructorValues) {
33 | this.constructorValues = constructorValues;
34 | }
35 |
36 | public BeanDefinition(Object object) {
37 | this.object = object;
38 | }
39 |
40 | public BeanDefinition() {
41 | }
42 |
43 | public Object getBean(BeanFactory beanFactory) throws IllegalAccessException, InstantiationException, ClassNotFoundException, NoSuchMethodException, InvocationTargetException {
44 | this.beanFactory = beanFactory;
45 | Class clazz = Class.forName(beanClassName);
46 | Constructor constructor = clazz.getConstructor(getClassArray());
47 | object = constructor.newInstance(getObjectArray());
48 | propertyValues.getPropertyValueList().forEach(propertyValue -> {
49 | Object value;
50 | if (propertyValue.getRef() != null) {
51 | value = beanFactory.getBean(propertyValue.getRef());
52 | }else{
53 | value = propertyValue.getValue();
54 | }
55 | try {
56 | BeanUtils.setProperty(object,propertyValue.getField(),value);
57 | // PropertyUtils.setProperty(object, propertyValue.getField(), value);
58 | } catch (IllegalAccessException | InvocationTargetException e) {
59 | e.printStackTrace();
60 | }
61 | });
62 |
63 | try {
64 | postProcessBeanFactory(object);
65 | } catch (Exception ignored) {
66 |
67 | }
68 | if (object instanceof FactoryBean) {
69 | return this.beanFactory.getObjectFromFactoryBean((FactoryBean) object);
70 | }
71 |
72 | beanFactory.cacheBean(beanId, object);
73 |
74 | return object;
75 | }
76 | private static Object getPrivateStatic(Class clazz, String f) throws Exception {
77 | try {
78 | Field field = clazz.getDeclaredField(f);
79 | field.setAccessible(true);
80 | return field.get(null);
81 | }
82 | catch (NoSuchFieldException e) {
83 | // Throw a more helpful exception.
84 | throw new NoSuchFieldException(
85 | "Could not find field named '" + f + "' in class '" + clazz +
86 | "'. All fields: " + Arrays.asList(clazz.getDeclaredFields()));
87 | }
88 | }
89 | private void postProcessBeanFactory(Object o) throws Exception {
90 | if (o instanceof BeanFactoryAware){
91 | ((BeanFactoryAware) o).setBeanFactory(beanFactory);
92 | }
93 | }
94 |
95 | private Class[] getClassArray() {
96 | List classes = new ArrayList<>();
97 | constructorValues.getConstructorValueList().forEach(constructorValue -> {
98 | if (constructorValue.getRef() != null){
99 | String className = beanFactory.getBeanType(constructorValue.getRef());
100 | try {
101 | classes.add(Class.forName(className));
102 | } catch (ClassNotFoundException e) {
103 | e.printStackTrace();
104 | }
105 | }else if (constructorValue.getValue() != null){
106 | classes.add(String.class);
107 | }
108 | });
109 | return classes.toArray(new Class[constructorValues.getConstructorValueList().size()]);
110 | // return constructorValues.getConstructorValueList().stream().map(constructorValue -> {
111 | //
112 | // }).map(refName-> beanFactory.getBeanType(refName)).map(className -> {
113 | //
114 | // try {
115 | // return Class.forName(className);
116 | // } catch (ClassNotFoundException e) {
117 | // e.printStackTrace();
118 | // }
119 | // return Collections.EMPTY_LIST;
120 | // }).collect(Collectors.toList()).toArray(new Class[constructorValues.getConstructorValueList().size()]);
121 | }
122 |
123 | private boolean isHasDuplicateData(List list) {
124 | HashSet hashSet = new HashSet<>(list);
125 |
126 | return list.size() != hashSet.size();
127 | }
128 |
129 | private Object[] getObjectArray(){
130 | List