@Component
public class First {
private void invoker() {
System.out.println("First class invoker method call");
}
}
@Component
public class Second {
@MyAnnotation
private void invoker() {
System.out.println("second class invoker method call");
}
}
@Component
public class Third {
@MyAnnotation
private void invoker() {
System.out.println("Third class invoker method call");
}
}
//호출될 클래스 및 메소드
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
//annotation 정의
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface MyAnnotation {
}
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.boot.SpringBootVersion;
import org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext;
import org.springframework.core.SpringVersion;
import org.springframework.stereotype.Component;
import java.lang.annotation.Annotation;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
@Component
public class AppRunner implements ApplicationRunner {
// @Autowired
//private AnnotationConfigApplicationContext context; //일반 application 인 경우
@Autowired
private AnnotationConfigServletWebServerApplicationContext context; //web application 인 경우
//private final Logger logger = LoggerFactory.getLogger(this.getClass());
public void run(ApplicationArguments args) {
System.out.println(
SpringVersion.getVersion());
System.out.println(SpringBootVersion.getVersion());
String[] beanDefinitionNames = context.getBeanDefinitionNames();
for (String beanDefinitionName : beanDefinitionNames) {
Object bean = context.getBean(beanDefinitionName);
Method[] declaredMethods = bean.getClass().getDeclaredMethods();
for (Method declaredMethod : declaredMethods) {
Annotation[] annotations = declaredMethod.getAnnotations();
for (Annotation annotation : annotations) {
if (annotation instanceof MyAnnotation) {
System.out.println("beanName: " + beanDefinitionName);
declaredMethod.setAccessible(true);
try {
System.out.println(declaredMethod.getName());
System.out.println("class name: " + bean.getClass().getName());
declaredMethod.invoke(bean);
} catch (IllegalAccessException e) {
System.out.println("can not access method " + declaredMethod.getName());
System.out.println(e.getMessage());
} catch (InvocationTargetException e) {
System.out.println(e.getMessage());
}
}
}
}
}
}
}
//application 구동 후 run 메소드 실행
import org.springframework.boot.SpringApplication;
import org.springframework.boot.WebApplicationType;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class App {
public static void main(String[] args) {
SpringApplication app = new SpringApplication(App.class);
app.setWebApplicationType(WebApplicationType.SERVLET);
app.run(args);
}
}
//메인 메소드
@MyAnnotation가 붙은 Second, Third Class의 invoker 메소드만 호출됨을 확인