当前位置: 首页 > news >正文

免费b站在线观看人数统计必应搜索引擎网站

免费b站在线观看人数统计,必应搜索引擎网站,移动app设计网站建设,网站设计二级页面怎么做SpringBoot有哪些扩展点 aware 感知类接口 aware系列的扩展接口,允许spring应用感知/获取特定的上下文环境或对象。bean生命周期控制类接口 bean生命周期类的接口,可以控制spring容器对bean的处理。app生命周期控制类接口 app生命周期控制类接口&#xf…

SpringBoot有哪些扩展点

aware 感知类接口

aware系列的扩展接口,允许spring应用感知/获取特定的上下文环境或对象。

bean生命周期控制类接口

bean生命周期类的接口,可以控制spring容器对bean的处理。

app生命周期控制类接口

app生命周期控制类接口,可以控制spring应用启动/销毁时的逻辑

下面详细介绍各种扩展点的使用

aware 感知类接口

ApplicationContextAware

package cn.test.ext.aware;import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;import java.util.Arrays;/*** ApplicationContextAware* 通过实现该接口,组件可以获取ApplicationContext对象的引用,* 从而访问应用程序上下文中的bean以及其他特定的spring功能*/
@Component
@Slf4j
public class TestAppCtxAware implements ApplicationContextAware {@Overridepublic void setApplicationContext(ApplicationContext applicationContext) throws BeansException {String applicationName = applicationContext.getApplicationName();log.info("context-path:{}",applicationName);String displayName = applicationContext.getDisplayName();log.info("displayName:{}",displayName);String id = applicationContext.getId();log.info("spring-applicationName:{}",id);long startupDate = applicationContext.getStartupDate();log.info("startupDate:{}",startupDate);
//        String[] beanDefinitionNames = applicationContext.getBeanDefinitionNames();
//        log.info("beanDefinitionNames:{}", Arrays.asList(beanDefinitionNames).toString());}
}

BeanFactoryAware

package cn.test.ext.aware;import cn.test.web.TestController;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.BeanFactoryAware;
import org.springframework.stereotype.Component;/*** BeanFactoryAware* 实现该接口,可以使自定义类获取bean工厂对象的引用,* 允许你在运行时从bean工厂中获取其他bean*/
@Slf4j
@Component
public class TestBeanFactoryAware implements BeanFactoryAware {@Overridepublic void setBeanFactory(BeanFactory beanFactory) throws BeansException {boolean b = beanFactory.containsBean("testController");log.info("spring容器内是否含有 testController这个bean :{}",b);TestController testController =(TestController) beanFactory.getBean("testController");String tgwt43t = testController.t1("tgwt43t");log.info("get bean方式调用 TestController里的t1方法,返回值:{}",tgwt43t);}
}

EnvironmentAware

package cn.test.ext.aware;import lombok.extern.slf4j.Slf4j;
import org.springframework.context.EnvironmentAware;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Component;import java.util.Arrays;/*** EnvironmentAware* 实现该接口,你的组件可以获取 Environment 环境对象的引用,* 从而可以访问spring应用程序的配置属性和配置文件*/
@Component
@Slf4j
public class TestEnvironmentAware implements EnvironmentAware {@Overridepublic void setEnvironment(Environment environment) {//获取当前默认的profile环境String[] defaultProfiles = environment.getDefaultProfiles();log.info("defaultProfiles:{}",Arrays.asList(defaultProfiles));//获取某个属性配置String property = environment.getProperty("spring.application.name");log.info("property->spring.application.name:{}",property);}
}

MessageSourceAware

package cn.test.ext.aware;import lombok.extern.slf4j.Slf4j;
import org.springframework.context.MessageSource;
import org.springframework.context.MessageSourceAware;
import org.springframework.stereotype.Component;/*** MessageSourceAware* 实现该接口,你的组件可以获取 messageSource 消息源对象的引用,* 从而访问 国际化消息内容*/
@Component
@Slf4j
public class TestMessageSourceAware implements MessageSourceAware {@Overridepublic void setMessageSource(MessageSource messageSource) {
//        messageSource.getMessage()}
}

ResourceLoaderAware

package cn.test.ext.aware;import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.ResourceLoaderAware;
import org.springframework.core.io.Resource;
import org.springframework.core.io.ResourceLoader;
import org.springframework.stereotype.Component;import java.util.Properties;/*** ResourceLoaderAware* 实现该接口,你的组件可以获取 ResourceLoader 资源加载器 对象的引用。* 从而可以加载外部资源文件*/
@Slf4j
@Component
public class TestResourceLoaderAware implements ResourceLoaderAware {@SneakyThrows@Overridepublic void setResourceLoader(ResourceLoader resourceLoader) {//ResourceLoader可以加载自定义位置的自定义的properties配置文件//这里测试加载classpath下的test.properties文件Resource resource = resourceLoader.getResource("classpath:test.properties");if (resource.isFile()) {String filename = resource.getFilename();log.info("filename:{}",filename);Properties properties = new Properties();properties.load(resource.getInputStream());properties.list(System.out);String aaa = properties.getProperty("aaa");log.info("aaa-->:{}",aaa);}}
}

ServletContextAware

package cn.test.ext.aware;import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.web.context.ServletContextAware;import javax.servlet.ServletContext;/*** ServletContextAware* 实现该接口,你的组件可以获取 ServletContext Servlet上下文对象的引用,* 从而访问与web应用程序相关的功能*/
@Component
@Slf4j
public class TestServletContextAware implements ServletContextAware {@Overridepublic void setServletContext(ServletContext servletContext) {//添加java web 过滤器
//        servletContext.addFilter()//添加java web servlet
//        servletContext.addServlet()//添加java web 监听器
//        servletContext.addListener();}
}

bean生命周期控制类接口

InitializingBean

package cn.test.ext.beanlife;import cn.test.ext.ITestStrategy;
import cn.test.ext.MyStrategyFactory;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.stereotype.Component;/*** InitializingBean* 实现该接口,你的bean可以在 spring容器初始化当前bean时 执行自定义逻辑** 必须在afterPropertiesSet方法里写你自定义的执行逻辑*/
@Slf4j
@Component
public class TestInitializingBean implements ITestStrategy, InitializingBean {private static String str ;//实现InitializingBean接口,必须重写afterPropertiesSet方法//这样当springboot启动加载TestInitializingBean时,会自动执行里边的afterPropertiesSet方法@Overridepublic void afterPropertiesSet() throws Exception {//本方法里可以做一些初始化的操作,如设置类静态变量值 / 将类注册到策略工厂/ 执行一些其他方法或动作/...//设置类静态变量值str = "qwe123!";//将k1策略 (本类) 注册到 策略工厂里MyStrategyFactory.register("k1",this);log.info("注册了策略:{}到策略工厂里",this);}@Overridepublic void execTestMethod() {log.info("start----执行k1策略...");System.err.println(str);}
}

DisposableBean

package cn.test.ext.beanlife;import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.stereotype.Component;/*** DisposableBean* 实现该接口,你的bean可以在销毁阶段执行自定义逻辑。* 该接口包含一个destory(),你可以在此方法中定义销毁逻辑。*/
@Slf4j
@Component
public class TestDisposableBean implements DisposableBean {@Overridepublic void destroy() throws Exception {log.info("TestDisposableBean 这个bean销毁了...");}
}

BeanPostProcessor

package cn.test.ext.beanlife;import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.stereotype.Component;/*** BeanPostProcessor* 该接口定义了在spring容器实例化bean之后和初始化之前,* 对bean进行自定义处理的方法。* 通过实现该接口,你可以插入自定义逻辑来处理bean*/
@Component
@Slf4j
public  class TestBeanPostProcessor implements BeanPostProcessor {public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {if (beanName.equals("testController")){log.info("BeanPostProcessor Before 处理到了 beanName:{}",beanName);}return bean;}public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {if (beanName.equals("testController")){log.info("BeanPostProcessor After 处理到了 beanName:{}",beanName);}return bean;}
}

BeanFactoryPostProcessor

package cn.test.ext.beanlife;import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.stereotype.Component;/*** BeanFactoryPostProcessor接口* 允许在 所有bean定义加载到容器之后,但在bean实例化之前对他们进行自定义处理。* 通过实现该接口,你可以修改或添加新的bean定义***/
@Component
@Slf4j
public class TestBeanFctoryPostProcessor implements BeanFactoryPostProcessor {@Overridepublic void postProcessBeanFactory(ConfigurableListableBeanFactory configurableListableBeanFactory) throws BeansException {log.info("postProcessBeanFactory 触发了....");}
}

BeanDefinitionRegistryPostProcessor

package cn.test.ext.beanlife;import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.beans.factory.support.BeanDefinitionRegistryPostProcessor;
import org.springframework.stereotype.Component;/*** BeanDefinitionRegistryPostProcessor接口** 这个接口是BeanFactoryPostProcessor接口的扩展,* 允许在bean定义注册过程中对 bean定义进行自定义处理,* 它提供了对bean定义注册表的直接访问,可以添加,修改或删除bean定义*/
@Slf4j
@Component
public class TestBeanDefineRegistPostProcessor implements BeanDefinitionRegistryPostProcessor {@Overridepublic void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry beanDefinitionRegistry) throws BeansException {log.info("postProcessBeanDefinitionRegistry 触发了...");}@Overridepublic void postProcessBeanFactory(ConfigurableListableBeanFactory configurableListableBeanFactory) throws BeansException {log.info("postProcessBeanFactory 触发了...");}
}

app生命周期控制类接口

SmartLifecycle

package cn.test.ext.applife;import lombok.extern.slf4j.Slf4j;
import org.springframework.context.SmartLifecycle;
import org.springframework.stereotype.Component;/*** SmartLifecycle接口 可以感知springboot应用生命周期变化,是spring的一个扩展点,可以实现随springboot启动和销毁的操作。** 自定义类实现SmartLifecycle接口后,至少要重写3个方法start(),stop(),isRunning() ,其他方法可以按需实现** springboot中的内嵌tomcat启动/关闭,就是利用SmartLifecycle实现的,* 见 org.springframework.boot.web.servlet.context.WebServerStartStopLifecycle*/
@Component
@Slf4j
public class TestSmartLifeCycle implements SmartLifecycle {//模拟一个任务 (实际开发中,可以在这里引入netty,实现netty随springboot启动和销毁)private static boolean xxxStat;/*** bean初始化完毕后,该方法会被执行*/@Overridepublic void start() {xxxStat = true;log.info("启动了 xxxStat ");}/*** 容器关闭后,spring容器发现当前对象实现了SmartLifecycle,* 就调用stop(Runnable),如果只是实现了Lifecycle,就调用stop()*/@Overridepublic void stop() {log.info("stop---容器关闭了");xxxStat = false;}/*** 当前状态* @return*/@Overridepublic boolean isRunning() {log.info("isRunning 获取当前状态:{}",xxxStat);return xxxStat;}/*** start方法被执行前,先看此方法返回值,* 返回false就不执行start方法了* @return*/@Overridepublic boolean isAutoStartup() {log.info("isAutoStartup 是否自启动");return true;}/*** 容器关闭后,spring容器发现当前对象实现了SmartLifecycle,就调用stop(Runnable),* 如果只是实现了Lifecycle,就调用stop()* @param callback*/@Overridepublic void stop(Runnable callback) {//必须调用stop方法 ,确保SmartLifecycle这个bean里的任务停止了this.stop();//必须执行callback.run方法,否则app exit时无法销毁这个SmartLifecyclecallback.run();log.info("stop Runnable---容器关闭了");}/*** 返回值决定* start方法 在众多Lifecycle实现类中的执行顺序(stop也是)* @return*/@Overridepublic int getPhase() {log.info("SmartLifecycle 当前优先级 1");return 1;}
}

CommandLineRunner

CommandLineRunner可以让springboot-jar启动时接收一些jvm参数或自定义cmd参数,然后执行一些操作

package cn.test.ext.applife;import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;@Slf4j
@Component
public class TestCmdLineRunner implements CommandLineRunner {@Overridepublic void run(String... args) throws Exception {log.info("CommandLineRunner 执行了...");}
}

ApplicationRunner

ApplicationRunner可以让springboot-jar启动时接收一些应用配置参数如server.port,然后执行一些操作

package cn.test.ext.applife;import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;@Slf4j
@Component
public class TestApplictionRunner implements ApplicationRunner {@Overridepublic void run(ApplicationArguments args) throws Exception {log.info("ApplicationRunner 执行了...");}
}

ApplicationListener

要通过spring内置的ApplicationListener监听ContextRefreshedEvent
上下文刷新事件,springboot启动时会触发一次,
一般常用来做业务缓存预热,如在springboot启动时把系统字典数据/系统配置项数据加到缓存中,方便后续调用。

package cn.test.ext.applife;import lombok.extern.slf4j.Slf4j;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.stereotype.Component;
@Slf4j
@Component
public class AppCtxRefreshListener implements ApplicationListener<ContextRefreshedEvent> {/*** 监听到上下文刷新事件时,* 执行相应的操作* @param contextRefreshedEvent 上下文刷新事件*/@Overridepublic void onApplicationEvent(ContextRefreshedEvent contextRefreshedEvent) {log.info("监听到了spring应用 上下文刷新事件...");}
}
http://www.dinnco.com/news/61707.html

相关文章:

  • 各大网站代下单怎么做网上推广产品怎么做
  • 最新全国疫情中高风险地区名单江西seo推广
  • 东营政府网站建设好的seo平台
  • 长春免费网站制作百度网站收录查询
  • 点击图片跳转到网站怎么做链接网络运营推广是做什么的
  • 视频网站推广怎么做营销活动推广策划
  • react.js做的网站百度账号快速注册
  • 最新网站制作seo职位描述
  • 深圳设计网站招聘域名怎么注册
  • asp.net制作网站开发优化营商环境应当坚持什么原则
  • 个人团购网站 转入备案上海百度整站优化服务
  • 做情书直接点网站深圳做网站
  • 网站图片大小多少合适宣传推广计划怎么写
  • 怎么做网站页面代码搜索百度推广代理商
  • 美国外贸网站成都网站seo性价比高
  • 郑州微信网站开发app平台搭建需要多少钱
  • 手机建网站 教程磁力链 ciliba
  • 广东手机网站制作价格免费建站系统官网
  • php移动网站开发网页设计排版布局技巧
  • 有哪几个网站可以做贸易aso优化软件
  • php做网站的支付功能产品互联网推广
  • 哪里的郑州网站建设seo外链优化方法
  • 网站后台管理是做一些什么苏州网站关键字优化
  • 怎么做网络销售的网站百度用户服务中心
  • 类似直播平台网站的建设费用怎么把平台推广出去
  • seo全网推广营销软件新泰网站seo
  • 学做ps的软件的网站百度公司是国企还是私企
  • 网站开发人员没有按照设计开发百度推广培训机构
  • 汕头网站推广银徽seo
  • 建设免费网站seo优化专员编辑