声明:代码是JavaEE开发的颠覆者 Spring Boot实战代码中的,我买了这书,并练习
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
@Configuration
@ComponentScan("com.wisely.highlight_spring4.ch3.aware")
public class AwareConfig {
}
----------------------
@Service
public class AwareService implements BeanNameAware{//1获取容器的bean名称
private String beanName;
@Override
public void setBeanName(String name) {//3
this.beanName = name;
}
public void outputResult(){
System.out.println("Bean的名称为:" + beanName);
}
------------------------------
public class Main {
public static void main(String[] args) {
AnnotationConfigApplicationContext context =
new AnnotationConfigApplicationContext(AwareConfig.class);
AwareService awareService = context.getBean(AwareService.class);
awareService.outputResult();
context.close();
}
}