Spring, Java Reflection Characteristics Record

Dynamic generation of classes and spring bean management when the java reflection mechanism is running provides great flexibility to the code. It is simple to do whatever you want. It is not recommended to use it indiscriminately. Here is a record of what you usually use

One java assist dynamically generates classes, there is source code in dubbo, free to tidy up

Two dynamically replace the running classes in the spring container, and dynamically add bean properties.

1. The following code provides operating conditions and beans to be tested

 1

span> @Component
2 @Lazy(false)
3 public class SpringUtil implements ApplicationContextAware {
4
5 private static ApplicationContext applicationContext;
6 //Get applicationContext
7 public static ApplicationContext getApplicationContext() {
8 return applicationContext;
9 }
10
11 //Get Bean by name.
12 public static Object getBean(String name) {
13 return getApplicationContext().getBean(name);
14 }
15
16 //Get Bean through class.
17 public static T getBean(Class clazz) {
18 return getApplicationContext().getBean(clazz);
19 }
20
21 //Return to the specified Bean through name and Clazz
22 public static T getBean(String name, Class clazz) {
23 return getApplicationContext().getBean(name, clazz);
24 }
25
26 @Override
27 public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
28 if (SpringUtil.applicationContext == null) {
29 SpringUtil.applicationContext = applicationContext;
30 }
31 }
32
33 private SpringUtil() {
34 }
35 }
 1 public interface BeanTestMe {

2 String getIdentity();
3 }
4
5 @Service("beanTestMe1")
6 public class BeanTestMe1 implements BeanTestMe{
7 public String getIdentity(){
8 return "1";
9 }
10 }
11
12 @Service("beanTestMe2")
13 public class BeanTestMe2 implements BeanTestMe{
14 public String getIdentity(){
15 return "2";
16 }
17 }

p>

2. The specific operation bean of the following class

 1 @Component

2 @Lazy(false)
3 public class InitBeanPropertyConfig implements ApplicationListener {
4 @Override
5 public void onApplicationEvent(ContextRefreshedEvent event){
6 if (event.getApplicationContext().getParent () == null) {
7         /**
8           Get beanfactroy and other categories
9         */
10 ApplicationContext ac = SpringUtil.getApplicationContext();
11 DefaultListableBeanFactory fty = (DefaultListableBeanFactory) ac.getAutowireCapableBeanFactory();
12
13         /**
14           Get mybFormalClientService, add dependsOn attribute
15         */
16 BeanDefinition bd = fty.getBeanDefinition("mybFormalClientService");
17 //fty.getBeanDefinitionNames()
18 String[] dependsOn = bd.getDependsOn();
19 String[] dependsOnNew;
20 if (dependsOn == null || dependsOn.length == 0){
21 dependsOnNew = new String[1];
22 dependsOnNew[0] = "mwMaskConvertHelper";
23 }else{
24 dependsOnNew = new String[dependsOn.length + 1];
25 System.arraycopy(dependsOn,0,dependsOnNew,0,dependsOn .length);
26 dependsOnNew[dependsOnNew.length-1] = "mwMaskConvertHelper";
27 }
28 bd.setDependsOn(dependsOnNew);
29 fty.registerBeanDefinition("mybFormalClientService", bd);
30
31        /**
32          dynamically get beanTestMe1, change the execution class to BeanTestMe2, register, and execute again The result is the result of BeanTestMe2
33         */
34 bd = fty.getBeanDefinition("beanTestMe1");
35 ((BeanTestMe)fty.getBean("beanTestMe1")) .getIdentity();
36 bd.setBeanClassName("com.ymm.crm.biz.BeanTestMe2");
37 fty.registerBeanDefinition("beanTestMe1", bd);
38 ((BeanTestMe)fty.getBean("beanTestMe1")) .getIdentity();
39 }
40 }
41 }

Three inheritance of spring’s FactoryBean dynamics Generate beans and use them in the Pegion source code for service registration.

 1 @Component

2 @Lazy(false)
3 public class SpringUtil implements ApplicationContextAware {
4
5 private static ApplicationContext applicationContext;
6 //Get applicationContext
7 public static ApplicationContext getApplicationContext() {
8 return applicationContext;
9 }
10
11 //Get Bean by name.
12 public static Object getBean(String name) {
13 return getApplicationContext().getBean(name);
14 }
15
16 //Get Bean through class.
17 public static T getBean(Class clazz) {
18 return getApplicationContext().getBean(clazz);
19 }
20
21 //Return to the specified Bean through name and Clazz
22 public static T getBean(String name, Class clazz) {
23 return getApplicationContext().getBean(name, clazz);
24 }
25
26 @Override
27 public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
28 if (SpringUtil.applicationContext == null) {
29 SpringUtil.applicationContext = applicationContext;
30 }
31 }
32
33 private SpringUtil() {
34 }
35 }
 1 public interface BeanTestMe {

2 String getIdentity();
3 }
4
5 @Service("beanTestMe1")
6 public class BeanTestMe1 implements BeanTestMe{
7 public String getIdentity(){
8 return "1";
9 }
10 }
11
12 @Service("beanTestMe2")
13 public class BeanTestMe2 implements BeanTestMe{
14 public String getIdentity(){
15 return "2";
16 }
17 }

 1 public interface span> BeanTestMe {

2 String getIdentity();
3 }
4
5 @Service("beanTestMe1")
6 public class BeanTestMe1 implements BeanTestMe{
7 public String getIdentity(){
8 return "1";
9 }
10 }
11
12 @Service("beanTestMe2")
13 public class BeanTestMe2 implements BeanTestMe{
14 public String getIdentity(){
15 return "2";
16 }
17 }

 1 @Component

2 @Lazy(false)
3 public class InitBeanPropertyConfig implements ApplicationListener {
4 @Override
5 public void onApplicationEvent(ContextRefreshedEvent event){
6 if (event.getApplicationContext().getParent () == null) {
7         /**
8           Get beanfactroy and other categories
9         */
10 ApplicationContext ac = SpringUtil.getApplicationContext();
11 DefaultListableBeanFactory fty = (DefaultListableBeanFactory) ac.getAutowireCapableBeanFactory();
12
13         /**
14           Get mybFormalClientService, add dependsOn attribute
15         */
16 BeanDefinition bd = fty.getBeanDefinition("mybFormalClientService");
17 //fty.getBeanDefinitionNames()
18 String[] dependsOn = bd.getDependsOn();
19 String[] dependsOnNew;
20 if (dependsOn == null || dependsOn.length == 0){
21 dependsOnNew = new String[1];
22 dependsOnNew[0] = "mwMaskConvertHelper";
23 }else{
24 dependsOnNew = new String[dependsOn.length + 1];
25 System.arraycopy(dependsOn,0,dependsOnNew,0,dependsOn .length);
26 dependsOnNew[dependsOnNew.length-1] = "mwMaskConvertHelper";
27 }
28 bd.setDependsOn(dependsOnNew);
29 fty.registerBeanDefinition("mybFormalClientService", bd);
30
31        /**
32          dynamically get beanTestMe1, change the execution class to BeanTestMe2, register, and execute again The result is the result of BeanTestMe2
33         */
34 bd = fty.getBeanDefinition("beanTestMe1");
35 ((BeanTestMe)fty.getBean("beanTestMe1")) .getIdentity();
36 bd.setBeanClassName("com.ymm.crm.biz.BeanTestMe2");
37 fty.registerBeanDefinition("beanTestMe1", bd);
38 ((BeanTestMe)fty.getBean("beanTestMe1")) .getIdentity();
39 }
40 }
41 }

Leave a Comment

Your email address will not be published.