/** * some useful static methods for spring. In fact, this class is not very "legal" in some way, and maybe warned by some * static code analysis tool like sonarqube for updating static field in constructor. * Created by shanghechen on 2017/8/1. */ @Service publicfinalclassSprings {
/** * accessed via reflection, see {@link Springs#Springs(ApplicationContext)} */ privatestatic ApplicationContext applicationContext;
/** * use reflection to violate sonar rule: "write to static field from instance method" * trivially, this method should be implemented as * {@code Springs.applicationContext = applicationContext; } * * @param applicationContext injected context */ @Autowired privateSprings(ApplicationContext applicationContext) { try { Fieldfield=this.getClass().getDeclaredField("applicationContext"); field.setAccessible(true); field.set(this, applicationContext); } catch (IllegalAccessException | NoSuchFieldException e) { thrownewIllegalStateException(e); } }