有时根据项目要求需要在项目启动时去执行一些操作,比如资源的加载、系统IP的获取、 读取配置文件,数据库连接等,实现方法也有多种。
一、java自身加载方式实现
static静态代码块:
static关键字所修饰的内容在整个类执行前优先加载
构造方法:
在对象初始化时执行,执行顺序在静态代码块之后
@Component
public class Test {
static{
InetAddress ip = null;
try {
ip = InetAddress.getLocalHost();
} catch (UnknownHostException e) {
e.printStackTrace();
}
System.out.println("IP Address1 : " + ip.getHostAddress());
}
public Test(){
InetAddress ip = null;
try {
ip = InetAddress.getLocalHost();
} catch (UnknownHostException e) {
e.printStackTrace();
}
System.out.println("IP Address2 : " + ip.getHostAddress());
}
}
执行结果:
二、@PostConstruct注解
@PostConstruct是Java自带的注解,在方法上加该注解会在项目启动的时候执行该方法,在spring容器初始化的时候执行该方法,执行顺序在静态代码块和构造方法之后。
@Component
public class PostConstructTest {
static{
InetAddress ip = null;
try {
ip = InetAddress.getLocalHost();
} catch (UnknownHostException e) {
e.printStackTrace();
}
System.out.println("IP Address1 : " + ip.getHostAddress());
}
public PostConstructTest(){
InetAddress ip = null;
try {
ip = InetAddress.getLocalHost();
} catch (UnknownHostException e) {
e.printStackTrace();
}
System.out.println("IP Address2 : " + ip.getHostAddress());
}
@PostConstruct
public void init(){
InetAddress ip = null;
try {
ip = InetAddress.getLocalHost();
} catch (UnknownHostException e) {
e.printStackTrace();
}
System.out.println("IP Address3 : " + ip.getHostAddress());
}
}
执行结果:
三、CommandLineRunner、ApplicationRunner接口
SpringBoot提供了两个启动加载接口分别是:CommandLineRunner和ApplicationRunner。
在applicationContext容器加载完成之后,会调用SpringApplication类的callRunners方法:该方法中会获取所有实现了ApplicationRunner和CommandLineRunner的接口bean,然后依次执行对应的run方法,并且是在同一个线程中执行。
CommandLineRunner
@Component
@Order(value = 1)
public class CommandLineRunnerTest1 implements CommandLineRunner{
@Override
public void run(String... args) throws Exception {
System.out.println("CommandLineRunnerTest1");
}
}
@Component
@Order(value = 2)
public class CommandLineRunnerTest2 implements CommandLineRunner{
@Override
public void run(String... args) throws Exception {
System.out.println("CommandLineRunnerTest2");
}
}
@Component
@Order(value = 1)
public class ApplicationRunnerTest1 implements ApplicationRunner {
@Override
public void run(ApplicationArguments args) throws Exception {
System.out.println("ApplicationRunnerTest1");
}
}
@Component
@Order(value = 2)
public class ApplicationRunnerTest2 implements ApplicationRunner {
@Override
public void run(ApplicationArguments args) throws Exception {
System.out.println("ApplicationRunnerTest2");
}
}
有多个类实现了CommandLineRunner、ApplicationRunner接口时,可使用@Order(1)注解来控制执行顺序,value值越小、越先执行@Order(1)>@Order(2),ApplicationRunner执行顺序在CommandLineRunner之前
四、InitializingBean接口
InitializingBean接口为bean提供了初始化方法的方式,它只包括afterPropertiesSet方法,凡是继承该接口的类,在初始化bean的时候都会执行该方法 。
public class InitializingBeanTest implements InitializingBean {
@Override
public void afterPropertiesSet() throws Exception {
System.out.println("InitializingBeanTest");
}
以上四种方式都可实现在项目启动后执行一次方法。执行顺序如下:
s
t
a
t
i
c
静态方法
>
构造方法
>
@
P
o
s
t
C
o
n
s
t
r
u
c
t
注解
>
I
n
i
t
i
a
l
i
z
i
n
g
B
e
a
n
>
A
p
p
l
i
c
a
t
i
o
n
R
u
n
n
e
r
>
C
o
m
m
a
n
d
L
i
n
e
R
u
n
n
e
r
static静态方法>构造方法>@PostConstruct注解>InitializingBean>ApplicationRunner>CommandLineRunner
static静态方法>构造方法>@PostConstruct注解>InitializingBean>ApplicationRunner>CommandLineRunner
其中ApplicationRunner:@Order(1)>CommandLineRunner:@Order(1)>ApplicationRunner:@Order(2)>CommandLineRunner:@Order(2)
@Component
public class PostConstructTest {
static {
System.out.println("static静态方法");
}
public PostConstructTest() {
System.out.println("构造方法");
}
@PostConstruct
public void init() {
System.out.println("@PostConstruct注解");
}
@Component
@Order(value = 1)
public class CommandLineRunnerTest1 implements CommandLineRunner {
@Override
public void run(String... args) throws Exception {
System.out.println("CommandLineRunner:@Order(1)");
}
}
@Component
@Order(value = 2)
public class CommandLineRunnerTest2 implements CommandLineRunner {
@Override
public void run(String... args) throws Exception {
System.out.println("CommandLineRunner:@Order(2)");
}
}
@Component
@Order(value = 1)
public class ApplicationRunnerTest1 implements ApplicationRunner {
@Override
public void run(ApplicationArguments args) throws Exception {
System.out.println("ApplicationRunner:@Order(1)");
}
}
@Component
@Order(value = 2)
public class ApplicationRunnerTest2 implements ApplicationRunner {
@Override
public void run(ApplicationArguments args) throws Exception {
System.out.println("ApplicationRunner:@Order(2)");
}
}
@Component
public class InitializingBeanTest implements InitializingBean {
@Override
public void afterPropertiesSet() throws Exception {
System.out.println("InitializingBean");
}
}
}