网站建设资讯

NEWS

网站建设资讯

SpringBoot监听机制-创新互联

1 简介

SpringBoot的监听机制,其实是对java提供的事件监听机制的封装。
java中的事件监听机制定义了一下几个角色:

为十堰郧阳等地区用户提供了全套网页设计制作服务,及十堰郧阳网站建设行业解决方案。主营业务为网站设计、网站制作、十堰郧阳网站设计,以传统方式定制建设网站,并提供域名空间备案等一条龙服务,秉承以专业、用心的态度为用户提供真诚的服务。我们深信只要达到每一位用户的要求,就会得到认可,从而选择与我们长期合作。这样,我们也可以走得更远!
  • 事件:Event,继承java.util.EventObject类的对象
  • 事件源:Source,任意对象Object
  • 监听器:Listener,实现java.util.EventListener接口的对象
2 实现ApplicationContextInitializer接口
import org.springframework.context.ApplicationContextInitializer;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.stereotype.Component;

@Component
public class MyApplicationContextInitializer implements ApplicationContextInitializer {@Override
    public void initialize(ConfigurableApplicationContext applicationContext) {System.out.println("ApplicationContextInitializer..................initialize");
    }
}
  • 在resource/META-INF/spring.factories下写
org.springframework.context.ApplicationContextInitializer=com.yunfeng.springbootlistener.listener.MyApplicationContextInitializer
3 实现ApplicationRunner接口
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;

import java.util.Arrays;

@Component
public class MyApplicationRunner implements ApplicationRunner {@Override
    public void run(ApplicationArguments args) throws Exception {System.out.println("ApplicationRunner.......run");
        System.out.println(Arrays.asList(args.getSourceArgs()));
    }
}
4 实现CommandLineRunner接口
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;
import java.util.Arrays;

@Component
public class MyCommandLineRunner implements CommandLineRunner {@Override
    public void run(String... args) throws Exception {System.out.println("CommandLineRunner......run");
        System.out.println(Arrays.asList(args));
    }
}
5 实现SpringApplicationRunListener接口观察者模式
public class MySpringApplicationRunListener implements SpringApplicationRunListener {}
6 效果
F:\java\jdk8\bin\java.exe -XX:TieredStopAtLevel=1 -noverify -Dspring.output.ansi.enabled=always "-javaagent:F:\intellij IDEA\IntelliJ IDEA 2021.1.1\lib\idea_rt.jar=49829:F:\intellij IDEA\IntelliJ IDEA 2021.1.1\bin" -Dcom.sun.management.jmxremote -Dspring.jmx.enabled=true -Dspring.liveBeansView.mbeanDomain -Dspring.application.admin.enabled=true -Dfile.encoding=UTF-8 -classpath "F:\java\jdk8\jre\lib\charsets.jar;F:\java\jdk8\jre\lib\deploy.jar;F:\java\jdk8\jre\lib\ext\access-bridge-64.jar;F:\java\jdk8\jre\lib\ext\cldrdata.jar;F:\java\jdk8\jre\lib\ext\dnsns.jar;F:\java\jdk8\jre\lib\ext\jaccess.jar;F:\java\jdk8\jre\lib\ext\jfxrt.jar;F:\java\jdk8\jre\lib\ext\localedata.jar;F:\java\jdk8\jre\lib\ext\nashorn.jar;F:\java\jdk8\jre\lib\ext\sunec.jar;F:\java\jdk8\jre\lib\ext\sunjce_provider.jar;F:\java\jdk8\jre\lib\ext\sunmscapi.jar;F:\java\jdk8\jre\lib\ext\sunpkcs11.jar;F:\java\jdk8\jre\lib\ext\zipfs.jar;F:\java\jdk8\jre\lib\javaws.jar;F:\java\jdk8\jre\lib\jce.jar;F:\java\jdk8\jre\lib\jfr.jar;F:\java\jdk8\jre\lib\jfxswt.jar;F:\java\jdk8\jre\lib\jsse.jar;F:\java\jdk8\jre\lib\management-agent.jar;F:\java\jdk8\jre\lib\plugin.jar;F:\java\jdk8\jre\lib\resources.jar;F:\java\jdk8\jre\lib\rt.jar;F:\intellij IDEA\workspace\springboot-listener\target\classes;F:\java\maven\maven-repository\org\springframework\boot\spring-boot-starter\2.7.7\spring-boot-starter-2.7.7.jar;F:\java\maven\maven-repository\org\springframework\boot\spring-boot\2.7.7\spring-boot-2.7.7.jar;F:\java\maven\maven-repository\org\springframework\spring-context\5.3.24\spring-context-5.3.24.jar;F:\java\maven\maven-repository\org\springframework\spring-aop\5.3.24\spring-aop-5.3.24.jar;F:\java\maven\maven-repository\org\springframework\spring-beans\5.3.24\spring-beans-5.3.24.jar;F:\java\maven\maven-repository\org\springframework\spring-expression\5.3.24\spring-expression-5.3.24.jar;F:\java\maven\maven-repository\org\springframework\boot\spring-boot-autoconfigure\2.7.7\spring-boot-autoconfigure-2.7.7.jar;F:\java\maven\maven-repository\org\springframework\boot\spring-boot-starter-logging\2.7.7\spring-boot-starter-logging-2.7.7.jar;F:\java\maven\maven-repository\ch\qos\logback\logback-classic\1.2.11\logback-classic-1.2.11.jar;F:\java\maven\maven-repository\ch\qos\logback\logback-core\1.2.11\logback-core-1.2.11.jar;F:\java\maven\maven-repository\org\apache\logging\log4j\log4j-to-slf4j\2.17.2\log4j-to-slf4j-2.17.2.jar;F:\java\maven\maven-repository\org\apache\logging\log4j\log4j-api\2.17.2\log4j-api-2.17.2.jar;F:\java\maven\maven-repository\org\slf4j\jul-to-slf4j\1.7.36\jul-to-slf4j-1.7.36.jar;F:\java\maven\maven-repository\jakarta\annotation\jakarta.annotation-api\1.3.5\jakarta.annotation-api-1.3.5.jar;F:\java\maven\maven-repository\org\springframework\spring-core\5.3.24\spring-core-5.3.24.jar;F:\java\maven\maven-repository\org\springframework\spring-jcl\5.3.24\spring-jcl-5.3.24.jar;F:\java\maven\maven-repository\org\yaml\snakeyaml\1.30\snakeyaml-1.30.jar;F:\java\maven\maven-repository\org\slf4j\slf4j-api\1.7.36\slf4j-api-1.7.36.jar" com.yunfeng.springbootlistener.SpringbootListenerApplication

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::                (v2.7.7)

ApplicationContextInitializer..................initialize
2022-12-25 18:47:40.548  INFO 6708 --- [           main] c.y.s.SpringbootListenerApplication      : Starting SpringbootListenerApplication using Java 1.8.0_301 on DESKTOP-Q3OS6FP with PID 6708 (F:\intellij IDEA\workspace\springboot-listener\target\classes started by 86157 in F:\intellij IDEA\workspace\springboot-condition)
2022-12-25 18:47:40.548  INFO 6708 --- [           main] c.y.s.SpringbootListenerApplication      : No active profile set, falling back to 1 default profile: "default"
2022-12-25 18:47:41.126  INFO 6708 --- [           main] c.y.s.SpringbootListenerApplication      : Started SpringbootListenerApplication in 1.004 seconds (JVM running for 1.993)
ApplicationRunner.......run
[]
CommandLineRunner......run
[]

你是否还在寻找稳定的海外服务器提供商?创新互联www.cdcxhl.cn海外机房具备T级流量清洗系统配攻击溯源,准确流量调度确保服务器高可用性,企业级服务器适合批量采购,新人活动首月15元起,快前往官网查看详情吧


分享标题:SpringBoot监听机制-创新互联
网站路径:http://cdweb.net/article/pggcs.html