File tree Expand file tree Collapse file tree
src/main/java/it/aboutbits/springboot/testing/spring Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ package it .aboutbits .springboot .testing .spring ;
2+
3+ import lombok .NonNull ;
4+ import org .springframework .context .ApplicationContext ;
5+ import org .springframework .context .ApplicationContextAware ;
6+ import org .springframework .stereotype .Component ;
7+
8+ @ Component
9+ public class BeanAccessor implements ApplicationContextAware {
10+ private static ApplicationContext applicationContext ;
11+
12+ public static <T > T getBean (@ NonNull Class <T > clazz ) {
13+ if (applicationContext == null ) {
14+ throw new IllegalStateException ("ApplicationContext is not set. Did you @EnableBeanAccessor?" );
15+ }
16+ return applicationContext .getBean (clazz );
17+ }
18+
19+ @ Override
20+ public void setApplicationContext (@ NonNull ApplicationContext applicationContext ) {
21+ BeanAccessor .applicationContext = applicationContext ;
22+ }
23+ }
Original file line number Diff line number Diff line change 1+ package it .aboutbits .springboot .testing .spring ;
2+
3+ import org .springframework .context .annotation .Import ;
4+
5+ import java .lang .annotation .ElementType ;
6+ import java .lang .annotation .Retention ;
7+ import java .lang .annotation .RetentionPolicy ;
8+ import java .lang .annotation .Target ;
9+
10+ @ Target ({ElementType .TYPE })
11+ @ Retention (RetentionPolicy .RUNTIME )
12+ @ Import (BeanAccessor .class )
13+ public @interface EnableBeanAccessor {
14+ }
You can’t perform that action at this time.
0 commit comments