Spring-常用操作

Spring

# 初始化bean时,普通成员变量获取不到@Value注解的变量值

问题:

public class Xxx {
    @Value("${value}")
    private String value;

    private Map<String, String> map = new HashMap<String, String>(){{
         put("key", value);// value取不到值
    }};
}
1
2
3
4
5
6
7
8

修改:

public class Xxx {
    @Value("${value}")
    private String value;
    private Map<String, String> map;

    @PostConstruct
    private void init(){
      map = new HashMap<String, String>(){{
           put("key", value);
      }};
    }
}
1
2
3
4
5
6
7
8
9
10
11
12

参考:
https://blog.csdn.net/zlp1992/article/details/78346420 (opens new window)

# springboot yaml文件数组表示

array: item1, item2, item3, item4, item5
1

参考:
https://stackoverflow.com/questions/26699385/spring-boot-yaml-configuration-for-a-list-of-strings (opens new window)

# springboot 获取上下文

@Configuration
public class ApplicationContextConfiguration {
  @Bean
  public InitApplicationContext initApplicationContext() {
    return new InitApplicationContext();
  }
}
1
2
3
4
5
6
7
public class InitApplicationContext implements ApplicationContextAware  {
  
  @Override
  public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
    AppEnvContext.setApplicationContext(applicationContext);
  }
  
}
1
2
3
4
5
6
7
8
public final class AppEnvContext {

  private static ApplicationContext applicationContext;

  private AppEnvContext() { }

  static void setApplicationContext(ApplicationContext applicationContext) {
    AppEnvContext.applicationContext = applicationContext;
  }

  public static ApplicationContext getApplicationContext() {
    return applicationContext;
  }
  
  public static Object getBean(String beanName) {
    return applicationContext.getBean(beanName);
  }

  public static <T> T getBean(Class<T> t) {
    return applicationContext.getBean(t);
  }

  public static <T> Map<String, T> getBeansOfType(Class<T> type) {
    return applicationContext.getBeansOfType(type);
  }

  public static String getActiveProfile() {
    return applicationContext.getEnvironment().getActiveProfiles()[0];
  }

}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
String env = AppEnvContext.getActiveProfile();
1

参考:
https://blog.csdn.net/qq_27818541/article/details/105719962 (opens new window)

# spring 获取对象的属性名&属性值

private void putAllField(Object obj, Map<String, Object> item) {
    PropertyDescriptor[] beanProperties = ReflectUtils.getBeanProperties(obj.getClass());
    try {
      for (PropertyDescriptor property : beanProperties) {
        Method readMethod = property.getReadMethod();
        if (readMethod == null)
          continue;
        if (property.getPropertyType().isAssignableFrom(Date.class)) {
          item.put(property.getName(), DateUtil.date2string((Date) readMethod.invoke(obj), DATA_FORMAT));
        } else if (property.getPropertyType().isAssignableFrom(Double.class)) {
          item.put(property.getName(), String.format("%.2f", (Double) readMethod.invoke(obj)));
        } else {
          item.put(property.getName(), property.getReadMethod().invoke(obj));
        }
      }
    } catch (Exception e) {
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18

# 禁用swagger-ui

springfox.documentation.enabled=false
1

参考:
https://stackoverflow.com/questions/66596813/springfox-boot-starter-how-to-disable-swagger-ui-for-production-profile (opens new window)
https://www.baeldung.com/swagger-ui-turn-off-in-production (opens new window)

# 删除引用jar包中的无用bean

@Component
public class RemoveRegistryBeanFactoryPostProcessor implements BeanDefinitionRegistryPostProcessor {

    @Override
    public void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry registry) throws BeansException {
        registry.removeBeanDefinition("XXX");
    }

    @Override
    public void postProcessBeanFactory(@NonNull ConfigurableListableBeanFactory configurableListableBeanFactory) throws BeansException {
        // execute postProcessBeanFactory
    }

}
1
2
3
4
5
6
7
8
9
10
11
12
13
14

参考:
https://www.jb51.net/article/252654.htm (opens new window)