iis服务器助手广告广告
返回顶部
首页 > 资讯 > 后端开发 > Python >spring容器启动实现初始化某个方法(init)
  • 884
分享到

spring容器启动实现初始化某个方法(init)

2024-04-02 19:04:59 884人浏览 泡泡鱼

Python 官方文档:入门教程 => 点击学习

摘要

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

spring容器启动 初始化某方法(init)

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、结果

这里写图片描述

6、注意事项

  • 当初始化方法中有依赖注入的时候,需要将加载注入的bean放在初始化bean之前。最好直接放在子容器中。因为父容器先于子容器初始化。否则依赖注入报错。
  • 取消初始化bean的懒加载,否则初始化方法无法执行。
  • lazy-init 设置只对scop属性为singleton的bean起作用

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() {  
 }
}

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

以上为个人经验,希望能给大家一个参考,也希望大家多多支持编程网。

--结束END--

本文标题: spring容器启动实现初始化某个方法(init)

本文链接: https://www.lsjlt.com/news/132150.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作