新建一个项目,选择Spring initializr
修改group和项目名
添加依赖包Web,MongoDB
设置保存位置和工程名
新建一个test的文件
输入代码:
1 package com.xxx.test.spider; 2 3 import org.springframework.stereotype.Service; 4 5 import javax.annotation.PostConstruct; 6 import java.util.Date; 7 8 import static java.lang.Thread.*; 9 10 //如果一个类带了@Service注解,将自动注册到Spring容器 11 @Service 12 public class test { 13 //启动spring时运行这个方法 14 @PostConstruct 15 public void fun(){ 16 while (true){ 17 try { 18 Thread.sleep(1000); //每隔1秒 19 } catch (InterruptedException e) { 20 e.printStackTrace(); 21 } 22 System.out.println(new Date()); //打印当前时间 23 } 24 } 25 }
运行输出:
. ____ _ __ _ _ /\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \ ( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \ \\/ ___)| |_)| | | | | || (_| | ) ) ) ) ' |____| .__|_| |_|_| |_\__, | / / / / =========|_|==============|___/=/_/_/_/ :: Spring Boot :: (v1.5.8.RELEASE) 2017-10-30 15:33:23.583 INFO 8108 --- [ main] com.xxx.test.TestApplication : Starting TestApplication on CXYF-100-049 with PID 8108 (D:\IdeaProjects\x\test\target\classes started by jacky.jiang in D:\IdeaProjects\x\test) 2017-10-30 15:33:23.585 INFO 8108 --- [ main] com.xxx.test.TestApplication : No active profile set, falling back to default profiles: default 2017-10-30 15:33:23.621 INFO 8108 --- [ main] s.c.a.AnnotationConfigApplicationContext : Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@6dde5c8c: startup date [Mon Oct 30 15:33:23 CST 2017]; root of context hierarchy Mon Oct 30 15:33:24 CST 2017 Mon Oct 30 15:33:25 CST 2017 Mon Oct 30 15:33:26 CST 2017