这里今天看了下ApplicationContextAware,简单用下
首先随便建立一个springboot项目
然后上代码
package com.example.demo;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class CC implements ApplicationContextAware{
private ApplicationContext applicationContext;
@Bean("My")
public String getName() {
return "MyName";
}
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
this.applicationContext=applicationContext;
}
@Bean
public String getName2() {
String bean = (String)applicationContext.getBean("My")+"yourName";
return bean;
}
}
下面