spring容器启动如何实现初始化某个方法

发布时间:2021-08-10 09:06:53 作者:小新
来源:亿速云 阅读:130

小编给大家分享一下spring容器启动如何实现初始化某个方法,相信大部分人都还不怎么了解,因此分享这篇文章给大家参考一下,希望大家阅读完这篇文章后大有收获,下面让我们一起去了解一下吧!

1、前言

很多时候,我们需要在项目启动的时候,就要完成某些方法的执行。今天整理了一个简单的方法,使用spring容器中bean的属性:init-method

2、代码

/*
    初始化的类。这里不需要添加任何注解
*/
public class InitData {
    @Autowired
    private UserService userService;
    /*
        初始化方法
    */
    public void inits(){
        System.out.println("初始化方法执行.....");
        List<User> userList = userService.queryAllUser();
        System.out.println(userList.toString());
    }
}

3、配置

<?xml version="1.0" encoding="UTF-8"?>
<beans  xmlns="http://www.springframework.org/schema/beans"    
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"    
        xmlns:p="http://www.springframework.org/schema/p"    
        xmlns:context="http://www.springframework.org/schema/context" 
        xmlns:mvc="http://www.springframework.org/schema/mvc"
        xsi:schemaLocation="http://www.springframework.org/schema/beans    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd    
        http://www.springframework.org/schema/context    http://www.springframework.org/schema/context/spring-context-3.0.xsd 
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd"
    default-lazy-init="true"><!-- 默认懒加载(延迟加载):调用的时候才实例化   -->
    <!-- 启动注解扫描包,获取bean -->
    <context:component-scan base-package="ws.spring.mybatis.service" />
    <!-- 引入数据源 -->
    <context:property-placeholder location="classpath:jdbc.properties"/>
    <!-- 注入数据源 -->
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="driverClass" value="${jdbc.driverClass}"></property>
        <property name="jdbcUrl" value="${jdbc.url}"></property>
        <property name="user" value="${jdbc.username}"></property>
        <property name="password" value="${jdbc.password}"></property>
    </bean>
    <!-- 容器启动后执行:需要执行初始化方法,所以必须直接实例化,取消懒加载-->
    <bean id="initData" class="ws.spring.mybatis.init.InitData" init-method="inits"  lazy-init="false" />
</beans>

5、结果

spring容器启动如何实现初始化某个方法

6、注意事项

spring容器启动初始化的几种方法

方法一

实现InitializingBean接口

InitializingBean接口为bean提供了初始化方法的方式,它只包括afterPropertiesSet方法,凡是继承该接口的类,在初始化bean的时候都会执行该方法。

xml中添加bean

不在xml中添加可以在Initializing类头部添加注解@Service

<bean id="initializingBean" class="cn.base.core.init.Initializing" init-method="init"></bean>

Initializing.java

public class Initializing implements InitializingBean{ 
 private static final Logger logger = LoggerFactory.getLogger(Initializing.class); 
 // 方法一
 @Override
 public void afterPropertiesSet() throws Exception {
  logger.info(">>>>>>>> init start...");
 }
 
 public void init() {
  logger.info(">>>>>>>> 初始化...");
 }
}

启动项目日志如下:

Invoking afterPropertiesSet() on bean with name 'initializingBean'
>>>>>>>> init start...
Invoking init method 'init' on bean with name 'initializingBean'
>>>>>>>> 初始化...

方法二

使用@PostConstruct注解

@PostConstruct
public void initConstruct() {
 System.out.println("注解初始化...");
}

注意:此方法所在的类需要被扫描到,多种注解可以用,推荐使用@Service

执行顺序:方法二比方法一优先执行。

方法三

web.xml配置

<filter>  
     <filter-name>filterServlet</filter-name>  
     <filter-class>cn.base.web.interceptor.FilterServlet</filter-class>  
</filter>
<filter-mapping>
    <filter-name>filterServlet</filter-name>
   <url-pattern>/*</url-pattern>
   <dispatcher>REQUEST</dispatcher>
</filter-mapping>

FilterServlet.java

public class FilterServlet implements Filter { 
 @Override
 public void init(FilterConfig filterConfig) throws ServletException {
  System.out.println("FilterServlet 初始化");
 }
 
 @Override
 public void doFilter(ServletRequest request, ServletResponse response, FilterChain         
chain)
   throws IOException, ServletException {
  chain.doFilter(request, response);
 }
 
 @Override
 public void destroy() {  
 }
}

执行顺序:优先级比上面两个低。

以上是“spring容器启动如何实现初始化某个方法”这篇文章的所有内容,感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助,如果还想学习更多知识,欢迎关注亿速云行业资讯频道!

推荐阅读:
  1. spring boot 中怎么监听容器启动
  2. Spring IOC 容器启动的示例分析

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

spring

上一篇:C++实现LeetCode之前K个高频词的示例分析

下一篇:vue如何使用keep-alive后清除缓存

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》