如何解决spring中同一个类里面方法之间调用的时候注解失效的问题

参考博客:https://blog.csdn.net/z55887/article/details/81073450

@RestController
public class Test {

    @Autowired
    ApplicationContext context;

    private static final Logger log = LoggerFactory.getLogger(Test.class);

    @GetMapping("/1")
    public String myProperties1() {
      //为了让异步注解生效,重新获取bean
        Test test = context.getBean(Test.class);
        test.printOut();
        System.out.println(222222222);
       return "Hello World!!!!!!!!!!!!!!";
    }
    //在同一个类中,一个方法调用另外一个有注解(比如@Async,@Transational)的方法,注解失效
    @Async
    void printOut() {
        log.info("------------------------");
        try {
            Thread.sleep(5000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println(11111111);
    }
}

猜你喜欢

转载自blog.csdn.net/Jsir_jsaf/article/details/85013254