forked from xuchengsheng/spring-reading
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
xuchengsheng
committed
Oct 25, 2023
1 parent
e7f066c
commit 84ef94c
Showing
28 changed files
with
549 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
...pring-annotation-componentScan/src/main/java/com/xcs/spring/special/SpecialComponent.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
102 changes: 102 additions & 0 deletions
102
...ing-core-resolveDependency/src/main/java/com/xcs/spring/ResolveDependencyApplication.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} | ||
} |
15 changes: 15 additions & 0 deletions
15
...re/spring-core-resolveDependency/src/main/java/com/xcs/spring/config/MyConfiguration.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 { | ||
} |
12 changes: 12 additions & 0 deletions
12
...g-core/spring-core-resolveDependency/src/main/java/com/xcs/spring/service/MyServiceA.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 { | ||
|
||
} |
39 changes: 39 additions & 0 deletions
39
...g-core/spring-core-resolveDependency/src/main/java/com/xcs/spring/service/MyServiceB.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 + | ||
'}'; | ||
} | ||
} |
1 change: 1 addition & 0 deletions
1
spring-core/spring-core-resolveDependency/src/main/resources/application.properties
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
my.property.value = Hello from Environment! |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
spring-jsr/spring-jsr330-named/src/main/java/com/xcs/spring/service/MyService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,7 +4,7 @@ | |
|
||
|
||
/** | ||
* @author 林雷 | ||
* @author xcs | ||
* @date 2023年10月19日 16时44分 | ||
**/ | ||
@Service | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,7 +8,7 @@ | |
import javax.inject.Inject; | ||
|
||
/** | ||
* @author 林雷 | ||
* @author xcs | ||
* @date 2023年10月20日 14时59分 | ||
**/ | ||
@Controller | ||
|
2 changes: 1 addition & 1 deletion
2
spring-jsr/spring-jsr330-qualifier/src/main/java/com/xcs/spring/service/MessageService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,7 +6,7 @@ | |
import javax.inject.Named; | ||
|
||
/** | ||
* @author 林雷 | ||
* @author xcs | ||
* @date 2023年10月20日 14时57分 | ||
**/ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,7 +6,7 @@ | |
import javax.inject.Named; | ||
|
||
/** | ||
* @author 林雷 | ||
* @author xcs | ||
* @date 2023年10月20日 14时57分 | ||
**/ | ||
@SMS | ||
|