SpringBoot花式获取ApplicationContext

1、继承SpringBootServletInitializer

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
@SpringBootApplication
public class MongodbApplication extends SpringBootServletInitializer {

@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(MongodbApplication.class);
}

public static void main(String[] args) throws UnknownHostException {
ConfigurableApplicationContext application = SpringApplication.run(MongodbApplication.class, args);
Environment env = application.getEnvironment();
String ip = InetAddress.getLocalHost().getHostAddress();
String port = env.getProperty("server.port");
String path = env.getProperty("server.servlet.context-path");
log.info("\n----------------------------------------------------------\n\t" +
"Application MongoDB is running! Access URLs:\n\t" +
"Local: \t\thttp://localhost:" + port + path + "/\n\t" +
"Swagger-UI: \t\thttp://" + ip + ":" + port + path + "/swagger-ui/index.html\n" +
"----------------------------------------------------------");
}

}

2、实现ApplicationContextAware

1
2
3
4
5
6
7
8
9
10
11
12
13
14
@Component
public class Book implements ApplicationContextAware {

private ApplicationContext applicationContext;

public void show (){
System.out.println(applicationContext.getClass());
}

@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
this.applicationContext = applicationContext;
}
}

【推荐】非Spring管理获取ApplicationContext

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
public class ApplicationContextHolder implements ApplicationContextAware {
private static Logger log = LoggerFactory.getLogger(ApplicationContextHolder.class);
private static ApplicationContext applicationContext;

public ApplicationContextHolder() {
}

public void setApplicationContext(ApplicationContext context) throws BeansException {
if (applicationContext != null) {
throw new IllegalStateException("ApplicationContextHolder already holded 'applicationContext'.");
} else {
applicationContext = context;
log.info("holded applicationContext,显示名称:" + applicationContext.getDisplayName());
}
}

public static ApplicationContext getApplicationContext() {
if (applicationContext == null) {
throw new IllegalStateException("'applicationContext' property is null,ApplicationContextHolder not yet init.");
} else {
return applicationContext;
}
}
}

3、使用Autowired注入

1
2
3
4
5
6
7
8
9
10
@Component
public class Book {

@Autowired
private ApplicationContext applicationContext;

public void show (){
System.out.println(applicationContext.getClass());
}
}

4、使用ContextLoader

1
2
3
4
import org.springframework.context.ApplicationContext;
import org.springframework.web.context.ContextLoader;

ApplicationContext context = ContextLoader.getCurrentWebApplicationContext();

注:该方式不依赖于servlet,不需要注入的方式;但在服务器启动Spring容器初始化时,不能使用该方法获取Spring容器

5、加载xml配置

1
2
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("classpath:Knight.xml")
Knight knight = applicationContext.getBean(Knight.class);
打赏
  • 版权声明: 本网站所有文章除特别声明外,均采用 Apache License 2.0 许可协议。转载请注明出处!
  1. © 2020-2021 Lauy    湘ICP备20003709号-1

请我喝杯咖啡吧~

支付宝
微信