DependsOn注解可以让我们在某个bean依赖另外一个bean的时候先加载另外一个bean,如下在控制台可以看到b类先被注入
package com.testGetBean; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.context.annotation.DependsOn; import org.springframework.stereotype.Component; @Component @DependsOn("b") public class A { private Logger logger = LoggerFactory.getLogger(getClass()); public A() { System.out.println("A"); logger.info("我是A我被实例化啦"); } public void display() { System.out.println("A dispaly"); } }
package com.testGetBean; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.stereotype.Component; @Component public class B { private Logger logger = LoggerFactory.getLogger(getClass()); public B() { System.out.println("B"); logger.info("我是B我被实例化啦"); } public void display() { System.out.println("b dispaly"); } }