Skip to content

Commit

Permalink
resolveDependency方法源码分析
Browse files Browse the repository at this point in the history
  • Loading branch information
xuchengsheng committed Oct 25, 2023
1 parent e7f066c commit 84ef94c
Show file tree
Hide file tree
Showing 28 changed files with 549 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import org.springframework.stereotype.Repository;

/**
* @author 林雷
* @author xcs
* @date 2023年10月07日 11时51分
**/
@Repository
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import org.springframework.stereotype.Service;

/**
* @author 林雷
* @author xcs
* @date 2023年10月07日 11时51分
**/
@Service
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import org.springframework.stereotype.Service;

/**
* @author 林雷
* @author xcs
* @date 2023年10月07日 11时50分
**/
@Service
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.xcs.spring.special;

/**
* @author 林雷
* @author xcs
* @date 2023年10月07日 11时52分
**/
public class SpecialComponent {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import org.springframework.beans.factory.DisposableBean;

/**
* @author 林雷
* @author xcs
* @date 2023年10月09日 16时45分
**/
public class BeanA implements DisposableBean {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import org.springframework.beans.factory.DisposableBean;

/**
* @author 林雷
* @author xcs
* @date 2023年10月09日 16时46分
**/
public class BeanB implements DisposableBean {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import org.springframework.beans.factory.DisposableBean;

/**
* @author 林雷
* @author xcs
* @date 2023年10月09日 16时46分
**/
public class BeanC implements DisposableBean {
Expand Down
359 changes: 358 additions & 1 deletion spring-core/spring-core-resolveDependency/README.md

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
package com.xcs.spring;

import com.xcs.spring.config.MyConfiguration;
import com.xcs.spring.service.MyServiceA;
import com.xcs.spring.service.MyServiceB;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.config.DependencyDescriptor;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.core.MethodParameter;
import org.springframework.util.ReflectionUtils;

import java.lang.reflect.Field;
import java.lang.reflect.Method;

/**
* @author xcs
* @date 2023年10月25日 10时13分
**/
public class ResolveDependencyApplication {

public static void main(String[] args) {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(MyConfiguration.class);
// 获得Bean工厂
ConfigurableListableBeanFactory beanFactory = context.getBeanFactory();
// 被注入对象
MyServiceB injectTarget = new MyServiceB();

System.out.println("Before MyServiceB = " + injectTarget + "\n");

methodResolveDependency(beanFactory, injectTarget, "setMethodMyServiceA");
fieldResolveDependency(beanFactory, injectTarget, "fieldMyServiceA");
fieldResolveDependency(beanFactory, injectTarget, "myPropertyValue");

System.out.println("After MyServiceB = " + injectTarget + "\n");
}

/**
* 解析方法依赖
*
* @param beanFactory
* @param injectTarget
*/
public static void methodResolveDependency(ConfigurableListableBeanFactory beanFactory, Object injectTarget, String name) {
try {
// 1. 获取MyServiceB类中名为setMyServiceA的方法的引用
Method method = injectTarget.getClass().getMethod(name, MyServiceA.class);

// 2. 创建一个描述此方法参数的DependencyDescriptor
DependencyDescriptor desc = new DependencyDescriptor(new MethodParameter(method, 0), true);

// 3. 使用BeanFactory来解析这个方法参数的依赖
Object value = beanFactory.resolveDependency(desc, null);

System.out.println("解析方法依赖结果:");
System.out.println("---------------------------------------------");
System.out.println(String.format("Name: %s.%s",method.getDeclaringClass().getName(),method.getName()));
System.out.println(String.format("Value: %s", value));
System.out.println("---------------------------------------------\n");

// 4. 使方法可访问(特别是如果它是private的)
ReflectionUtils.makeAccessible(method);

// 5. 使用反射调用这个方法,将解析得到的依赖注入到目标对象中
method.invoke(injectTarget, value);
} catch (Exception e) {
e.printStackTrace();
}
}

/**
* 解析字段依赖
*
* @param beanFactory
* @param injectTarget
*/
public static void fieldResolveDependency(ConfigurableListableBeanFactory beanFactory, Object injectTarget, String name) {
try {
// 1. 获取MyServiceB类中名为fieldMyServiceA的字段的引用
Field field = injectTarget.getClass().getDeclaredField(name);

// 2. 创建一个描述此字段的DependencyDescriptor
DependencyDescriptor desc = new DependencyDescriptor(field, true);

// 3. 使用BeanFactory来解析这个字段的依赖
Object value = beanFactory.resolveDependency(desc, null);

System.out.println("解析字段依赖结果:");
System.out.println("---------------------------------------------");
System.out.println(String.format("Name: %s.%s", field.getDeclaringClass().getName(), field.getName()));
System.out.println(String.format("Value: %s", value));
System.out.println("---------------------------------------------\n");

// 4. 使字段可访问(特别是如果它是private的)
ReflectionUtils.makeAccessible(field);

// 5. 使用反射设置这个字段的值,将解析得到的依赖注入到目标对象中
field.set(injectTarget, value);
} catch (Exception e) {
e.printStackTrace();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.xcs.spring.config;

import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;

/**
* @author xcs
* @date 2023年09月19日 16时35分
**/
@Configuration
@ComponentScan("com.xcs.spring")
@PropertySource("classpath:application.properties")
public class MyConfiguration {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.xcs.spring.service;

import org.springframework.stereotype.Service;

/**
* @author xcs
* @date 2023年10月25日 10时36分
**/
@Service
public class MyServiceA {

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package com.xcs.spring.service;

import org.springframework.beans.factory.annotation.Value;

/**
* @author xcs
* @date 2023年10月25日 10时37分
**/
public class MyServiceB {

/**
* 方法注入
*/
private MyServiceA methodMyServiceA;

/**
* 字段注入
*/
private MyServiceA fieldMyServiceA;

/**
* 字段注入 (环境变量)
*/
@Value("${my.property.value}")
private String myPropertyValue;

public void setMethodMyServiceA(MyServiceA methodMyServiceA){
this.methodMyServiceA = methodMyServiceA;
}

@Override
public String toString() {
return "MyServiceB{" +
"myPropertyValue='" + myPropertyValue + '\'' +
", methodMyServiceA=" + methodMyServiceA +
", fieldMyServiceA=" + fieldMyServiceA +
'}';
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
my.property.value = Hello from Environment!
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import com.xcs.spring.annotation.MyValue;

/**
* @author 林雷
* @author xcs
* @date 2023年09月20日 10时52分
**/
public class MyBean {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import java.time.format.DateTimeFormatter;

/**
* @author 林雷
* @author xcs
* @date 2023年09月27日 10时36分
**/
@Service
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import org.springframework.stereotype.Component;

/**
* @author 林雷
* @author xcs
* @date 2023年09月21日 10时31分
**/
@Component
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

/**
* @author 林雷
* @author xcs
* @date 2023年10月18日 14时17分
**/
public class PostConstructApplication {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

/**
* @author 林雷
* @author xcs
* @date 2023年10月18日 14时17分
**/
public class PreDestroyApplication {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.xcs.spring.service;

/**
* @author 林雷
* @author xcs
* @date 2023年10月19日 16时44分
**/
public class MyService {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import javax.inject.Named;

/**
* @author 林雷
* @author xcs
* @date 2023年10月19日 16时43分
**/
@Named("myServiceA")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import javax.inject.Named;

/**
* @author 林雷
* @author xcs
* @date 2023年10月19日 16时43分
**/
@Named("myServiceB")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@


/**
* @author 林雷
* @author xcs
* @date 2023年10月19日 16时44分
**/
@Service
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

/**
* @author 林雷
* @author xcs
* @date 2023年10月20日 14时56分
**/
public class QualifierApplication {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import org.springframework.context.annotation.Configuration;

/**
* @author 林雷
* @author xcs
* @date 2023年10月20日 14时56分
**/
@Configuration
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import javax.inject.Inject;

/**
* @author 林雷
* @author xcs
* @date 2023年10月20日 14时59分
**/
@Controller
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.xcs.spring.service;

/**
* @author 林雷
* @author xcs
* @date 2023年10月20日 14时56分
**/
public interface MessageService {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import javax.inject.Named;

/**
* @author 林雷
* @author xcs
* @date 2023年10月20日 14时57分
**/
@Email
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import javax.inject.Named;

/**
* @author 林雷
* @author xcs
* @date 2023年10月20日 14时57分
**/
@SMS
Expand Down

0 comments on commit 84ef94c

Please sign in to comment.