listener - Can't use Spring ApplicationListener -


my project spring-boot(1.3.5 release),web project(spring-web 4.3.0 realese). want use "applicationlistener",when spring loaded,to something.but code doesn't work.there's code:

package hello;

import org.springframework.context.applicationlistener; import org.springframework.context.event.contextrefreshedevent;

public class bootlistener implements applicationlistener{

@override public void onapplicationevent(contextrefreshedevent event) {     system.out.println("spring启动监听器");     system.out.println(event.gettimestamp());  } 

}

package hello;

import org.springframework.boot.springapplication; import org.springframework.boot.autoconfigure.springbootapplication;

@springbootapplication public class application {

public static void main(string[] args) {     springapplication.run(application.class, args); } 

}

http://maven.apache.org/xsd/maven-4.0.0.xsd"> 4.0.0

<groupid>org.springframework</groupid> <artifactid>demo</artifactid> <version>0.1.0</version>  <parent>     <groupid>org.springframework.boot</groupid>     <artifactid>spring-boot-starter-parent</artifactid>     <version>1.3.3.release</version> </parent>  <dependencies>     <dependency>         <groupid>org.springframework.boot</groupid>         <artifactid>spring-boot-starter-web</artifactid>     </dependency> </dependencies>  <properties>     <java.version>1.8</java.version> </properties>   <build>     <plugins>         <plugin>             <groupid>org.springframework.boot</groupid>             <artifactid>spring-boot-maven-plugin</artifactid>         </plugin>     </plugins> </build>  <repositories>     <repository>         <id>spring-releases</id>         <url>https://repo.spring.io/libs-release</url>     </repository> </repositories> <pluginrepositories>     <pluginrepository>         <id>spring-releases</id>         <url>https://repo.spring.io/libs-release</url>     </pluginrepository> </pluginrepositories> 

you need make event listener singleton spring can pick up. annotate bootlistener @component.

to publish events yourself, inject applicationeventpublisher.


Comments

Popular posts from this blog

wordpress - (T_ENDFOREACH) php error -

Export Excel workseet into txt file using vba - (text and numbers with formulas) -

Using django-mptt to get only the categories that have items -