springboot获取applicationcontext
使用springboot之前,我们通过ClassPathXmlApplicationContext加载spring xml配置文件来获取applicationcontext,使用springboot后,由于不存在xml文件,故该种方式已经不能使用
在官方文档中介绍,可通过实现ApplicationRunner或者CommandLineRunner在springaplication启动后,立即执行其中的一些代码,做初始化操作,如果存在多个实现类,可使用@Order注解设置其启动顺序,也可使用实现ApplicationListener来做初始化操作,并且实现ApplicationListener可获取到applicationcontext
获取applicationcontext的方式有多种,以下列举出几种方式
一、实现ApplicationListener<ContextRefreshedEvent>
1、启动类
package com.demo.bootdemo; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import com.demo.bootdemo.listeners.MainBusiListeners; import com.demo.bootdemo.test.TestBean; import com.demo.bootdemo.uitils.ApplicationContextUtils; @SpringBootApplication public class MydemoApplication { public static void main(String[] args) { SpringApplication sa = new SpringApplication(MydemoApplication.class); sa.addListeners(new MainBusiListeners()); sa.run(args); // 测试 TestBean bean = ApplicationContextUtils.getBean(TestBean.class); bean.print("yes it is test."); } }
2、启动监听
package com.demo.bootdemo.listeners; import org.springframework.context.ApplicationListener; import org.springframework.context.event.ContextRefreshedEvent; import com.demo.bootdemo.uitils.ApplicationContextUtils; /** * 启动监听 */ public class MainBusiListeners implements ApplicationListener<ContextRefreshedEvent> { @Override public void onApplicationEvent(ContextRefreshedEvent event) { ApplicationContextUtils.setContext(event.getApplicationContext()); } }
3、ApplicationContext工具类
package com.demo.bootdemo.uitils; import org.springframework.context.ApplicationContext; /** * ApplicaitonContext工具类 * */ public class ApplicationContextUtils { private static ApplicationContext context; public static void setContext(ApplicationContext applicationContext) { context = applicationContext; } public static Object getBean(String beanName) { return context.getBean(beanName); } public static <T> T getBean(Class<T> t) { return context.getBean(t); } }
4、测试Bean
package com.demo.bootdemo.test; import org.springframework.stereotype.Component; /** * 测试Bean */ @Component public class TestBean { public void print(String str) { System.out.println("input: " + str); } }
5、启动启动类MydemoApplication.java
. ____ _ __ _ _ /\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \ ( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \ \\/ ___)| |_)| | | | | || (_| | ) ) ) ) ' |____| .__|_| |_|_| |_\__, | / / / / =========|_|==============|___/=/_/_/_/ :: Spring Boot :: (v2.1.3.RELEASE) 2019-04-04 09:51:28.722 INFO 93344 --- [ main] com.demo.bootdemo.MydemoApplication : Starting MydemoApplication on admin-PC with PID 93344 (D:\Programs\eclipseworkplace\springboot\mydemo\target\classes started by admin in D:\Programs\eclipseworkplace\springboot\mydemo) 2019-04-04 09:51:28.725 INFO 93344 --- [ main] com.demo.bootdemo.MydemoApplication : No active profile set, falling back to default profiles: default 2019-04-04 09:51:29.247 INFO 93344 --- [ main] com.demo.bootdemo.MydemoApplication : Started MydemoApplication in 0.838 seconds (JVM running for 1.204) input: yes it is test.
通过上述输出结果可知,applicationcontext正常获取,如果需要在run方法执行之前,使用applicationcontext做一些事情,可在MainBusiListeners的onApplicationEvent方法中进行。另外,使用@Component或者具有类似功能注解,启动类中去掉sa.addListeners(new MainBusiListeners());,一样可以正常获取applicationcontext,ApplicationListener中的Event有多种,父接口为ApplicationEvent,具体可参考源码查看
二、启动类run方法返回值即为applicationcontext
查看main方法中启动类代码
查看run方法,发现该方法返回ConfigurableApplicationContext
查看ConfigurableApplicationContext
接口实现了ApplicaitonContext接口,所以run方法返回的值就是我们需要的context了
启动类
package com.demo.Demo001; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.context.ConfigurableApplicationContext; @SpringBootApplication public class App { public static void main(String[] args) { ConfigurableApplicationContext context = SpringApplication.run(App.class, args); ContextUtils.setApplicationContext(context); MainBusiEntry.execute(); } }
context工具类
package com.demo.Demo001; import org.springframework.context.ApplicationContext; public class ContextUtils { public static ApplicationContext context; private ContextUtils() { } public static void setApplicationContext(ApplicationContext applicationContext) { context = applicationContext; } public static Object getBean(String beanName) { return context.getBean(beanName); } public static <T> T getBean(Class<T> t) { return context.getBean(t); } }
测试bean
package com.demo.Demo001; import org.springframework.stereotype.Component; @Component public class TestBean { public String getName() { return this.getClass().getCanonicalName(); } }
业务处理入口
package com.demo.Demo001; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; public class MainBusiEntry { private static Logger logger = LogManager.getLogger(MainBusiEntry.class); public static void execute() { TestBean bean = ContextUtils.getBean(TestBean.class); logger.info(bean.getName()); } }
启动springboot,输出结果如下
. ____ _ __ _ _ /\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \ ( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \ \\/ ___)| |_)| | | | | || (_| | ) ) ) ) ' |____| .__|_| |_|_| |_\__, | / / / / =========|_|==============|___/=/_/_/_/ :: Spring Boot :: (v2.1.0.RELEASE) 11:53:25.302 INFO org.springframework.boot.StartupInfoLogger.logStarting()/50 - Starting App on admin-PC with PID 195640 (D:\Programs\eclipseworkplace\springboot\Demo001\target\classes started by admin in D:\Programs\eclipseworkplace\springboot\Demo001) 11:53:25.338 INFO org.springframework.boot.SpringApplication.logStartupProfileInfo()/675 - No active profile set, falling back to default profiles: default 11:53:26.375 INFO com.mongodb.diagnostics.logging.SLF4JLogger.info()/71 - Cluster created with settings {hosts=[localhost:27017], mode=SINGLE, requiredClusterType=UNKNOWN, serverSelectionTimeout='30000 ms', maxWaitQueueSize=500} 11:53:26.456 INFO org.springframework.boot.StartupInfoLogger.logStarted()/59 - Started App in 1.393 seconds (JVM running for 2.078) 11:53:26.459 INFO com.demo.Demo001.MainBusiEntry.execute()/11 - com.demo.Demo001.TestBean
输出了测试类的路径。
三、 实现ApplicationContextAware
这种方式比较简单,applicaitoncontext初始化后,spring自动调用setApplicationContext方法,设置applicationcontext
package com.demo.nettyclientpool.tool; import org.springframework.beans.BeansException; import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContextAware; import org.springframework.stereotype.Component; @Component public class ApplicationConetxtHelper implements ApplicationContextAware{ private static ApplicationContext ctx; @Override public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { ctx=applicationContext; } public static Object getBean(String beanName) { return ctx.getBean(beanName); } public static <T> T getBean(Class<T> requiredType){ return ctx.getBean(requiredType); } }
我们在开发中,使用的为第一种获取applicationcontext的方式