应用启动时就执行特定处理

14 篇文章 1 订阅

一、ApplicationContextAware接口

1

2

3

4

5

6

7

8

9

package org.springframework.context;

 

import org.springframework.beans.BeansException;

import org.springframework.beans.factory.Aware;

import org.springframework.context.ApplicationContext;

 

public interface ApplicationContextAware extends Aware {

    void setApplicationContext(ApplicationContext var1) throws BeansException;

}

二、ServletContextAware 接口

1

2

3

4

5

6

7

8

package org.springframework.web.context;

 

import javax.servlet.ServletContext;

import org.springframework.beans.factory.Aware;

 

public interface ServletContextAware extends Aware {

    void setServletContext(ServletContext var1);

}

三、InitializingBean 接口

1

2

3

4

5

package org.springframework.beans.factory;

 

public interface InitializingBean {

    void afterPropertiesSet() throws Exception;

}

四、ApplicationListener<ApplicationEvent> 接口

1

2

3

4

5

6

7

8

package org.springframework.context;

 

import java.util.EventListener;

import org.springframework.context.ApplicationEvent;

 

public interface ApplicationListener<E extends ApplicationEvent> extends EventListener {

    void onApplicationEvent(E var1);

}

示例程序:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

package test.web.listener;

 

import org.apache.logging.log4j.*;

import org.springframework.beans.*;

import org.springframework.beans.factory.InitializingBean;

import org.springframework.context.*;

import org.springframework.context.event.ContextRefreshedEvent;

import org.springframework.stereotype.Component;

import org.springframework.web.context.ServletContextAware;

import javax.servlet.ServletContext;

 

@Component

public class StartupListener implements ApplicationContextAware, ServletContextAware,

        InitializingBean, ApplicationListener<ContextRefreshedEvent> {

 

    protected Logger logger = LogManager.getLogger();

 

    @Override

    public void setApplicationContext(ApplicationContext ctx) throws BeansException {

        logger.info("1 => StartupListener.setApplicationContext");

    }

 

    @Override

    public void setServletContext(ServletContext context) {

        logger.info("2 => StartupListener.setServletContext");

    }

 

    @Override

    public void afterPropertiesSet() throws Exception {

        logger.info("3 => StartupListener.afterPropertiesSet");

    }

 

    @Override

    public void onApplicationEvent(ContextRefreshedEvent evt) {

        logger.info("4.1 => MyApplicationListener.onApplicationEvent");

        if (evt.getApplicationContext().getParent() == null) {

            logger.info("4.2 => MyApplicationListener.onApplicationEvent");

        }

    }

 

}

运行时,输出的顺序如下:

1 => StartupListener.setApplicationContext
2 => StartupListener.setServletContext
3 => StartupListener.afterPropertiesSet
4.1 => MyApplicationListener.onApplicationEvent
4.2 => MyApplicationListener.onApplicationEvent
4.1 => MyApplicationListener.onApplicationEvent

注意:onApplicationEvent方法会触发多次,初始化这种事情,越早越好,建议在setApplicationContext方法中处理。

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值