public class UserController {
@Autowird
private UserService userService;
public UserService getUserService() {
return userService;
}
}
public class UserService {
}
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
public @interface Autowird {
}
public class MyTest {
@Test
public void test2() throws Exception {
UserController userController =new UserController();
Class<? extends UserController> clazz = userController.getClass();
Stream.of(clazz.getDeclaredFields()).forEach(field -> {
String name = field.getName();
Autowird annotation = field.getAnnotation(Autowird.class);
if(annotation!=null){
field.setAccessible(true);
Class<?> type = field.getType();
System.out.println("type: "+type);
try {
Object o = type.newInstance();
field.set(userController,o);
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}
});
System.out.println(userController.getUserService());
}
}