SpringBoot监听机制-创新互联
1 简介
新闻名称:SpringBoot监听机制-创新互联
文章分享:http://scjbc.cn/article/pggcs.html
SpringBoot的监听机制,其实是对java提供的事件监听机制的封装。
java中的事件监听机制定义了一下几个角色:
- 事件:Event,继承java.util.EventObject类的对象
- 事件源:Source,任意对象Object
- 监听器:Listener,实现java.util.EventListener接口的对象
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://scjbc.cn/article/pggcs.html