spring项目启动后执行指定的方法

spring项目如何在启动项目是执行一些操作,在spring中能通过那些操作实现这个功能呢。

1.方法一

我在spring的配置文件中添加上这条,这个配置只能在启动项目是执行一遍。
还有一点 要注意 这个方法不能是controller层的方法

<-- class是类的全名加包名 这是指定运行的方法在那个类里面   -->
<-- scope 值得范围 这里给的参数是 singleton   -->
<-- inti-method 是指要执行的方法  -->
<bean id="startRun" class="com.shr.bojs.StartRun" scope="singleton" init-method="test"></bean>
package com.shr.jobs;

public class StartRun {


    public void test(){
        System.out.println("开始执行 startRun 方法!!!");
    }



}
log4j:WARN No appenders could be found for logger (org.springframework.web.context.ContextLoader).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
开始执行 startRun 方法!!!
SLF4J: Class path contains multiple SLF4J bindings.
SLF4J: Found binding in [jar:file:/D:/tomcat7.manage/webapps/newBLManager/WEB-INF/lib/slf4j-log4j12-1.5.11.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: Found binding in [jar:file:/D:/tomcat7.manage/webapps/newBLManager/WEB-INF/lib/slf4j-nop-1.5.2.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: See http://www.slf4j.org/codes.html#multiple_bindings for an explanation.

2.方法二
使用@PostContruct ,在方法上添加@PostConstruct注解
注意 一定要放在能被扫面到的地方,如果你写在一个无法被扫描到的位置是不能执行的。(service层肯定能被扫描到)

@Service
public class TestRun{

    @PostConstruct
    public void text(){
        System.out.println("项目开始运行我也就执行!!");
    }
}
SLF4J: Class path contains multiple SLF4J bindings.
SLF4J: Found binding in [jar:file:/D:/tomcat7.manage/webapps/newBLManager/WEB-INF/lib/slf4j-log4j12-1.5.11.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: Found binding in [jar:file:/D:/tomcat7.manage/webapps/newBLManager/WEB-INF/lib/slf4j-nop-1.5.2.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: See http://www.slf4j.org/codes.html#multiple_bindings for an explanation.
Logging initialized using 'class org.apache.ibatis.logging.stdout.StdOutImpl' adapter.
项目开始运行我也就执行!!
五月 19, 2018 12:21:14 上午 org.apache.catalina.core.ApplicationContext log
信息: Initializing Spring FrameworkServlet 'springMVC'

3.方法三
实现initiailzingBean接口。

afterPropertiesSet这个方法就会在项目启动时执行。


@Service
public class ArraignedLogService implements InitializingBean {

    @Override
    public void afterPropertiesSet() throws Exception {
        System.out.println("我是通过实现接口 initializingBean来执行的!!!");

    }
}
SLF4J: Class path contains multiple SLF4J bindings.
SLF4J: Found binding in [jar:file:/D:/tomcat7.manage/webapps/newBLManager/WEB-INF/lib/slf4j-log4j12-1.5.11.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: Found binding in [jar:file:/D:/tomcat7.manage/webapps/newBLManager/WEB-INF/lib/slf4j-nop-1.5.2.jar!/org/slf4j/impl/StaticLoggerBinder.class] 
Logging initialized using 'class org.apache.ibatis.logging.stdout.StdOutImpl' adapter.
我是通过实现接口 initializingBean来执行的!!!
五月 19, 2018 12:38:33 上午 org.apache.catalina.core.ApplicationContext log
信息: Initializing Spring FrameworkServlet 'springMVC'

测试了以上三种,没有问题。只不过我自己写了一个util包。使用注解@PostConstruce 没有起效果。使用initializingBean也没起到效果。但是写在service层是没有问题的。开始以为是注解驱动没有包含util包,所以没被执行。但是这个实现的接口是怎会事呢。
这个还的看看,肯定是哪个配置没到位。
4.第四种,也是比较牛逼的一种(个人认为哦,原因就是它的配置比较多。。。)

这是在 web.xml中配置的

1 <context-param>  
2     <param-name>contextConfigLocation</param-name>  
3     <param-value>  
4              classpath:conf/spring-config.xml,  
5              classpath:conf/quartz-config.xml
6         </param-value>  
7 </context-param

这是在quartz-config中配置的

<?xml version="1.0" encoding="UTF-8"?>
 2 <beans xmlns="http://www.springframework.org/schema/beans"
 3        xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
 4        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 5        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd  
 6        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd  
 7        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd"
 8        default-lazy-init="false">
 9     <bean id="quartzJob" class="xx.xx.xx.QuartzJob"/>
10     <bean id="jobDetail" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
11         <property name="targetObject">
12             <ref bean="quartzJob"/>
13         </property>
14         <property name="targetMethod">
15             <value>xxxxxx(你在QuartzJob中的方法)</value>
16         </property>
17     </bean>
18 
19     <!-- 项目启动后任务就执行一次 -->
20     <bean id="simpleTrigger" class="org.springframework.scheduling.quartz.SimpleTriggerBean">
21         <property name="jobDetail" ref="jobDetail"/>
22         <property name="startDelay" value="500"/>
23         <property name="repeatInterval" value="0"/>
24         <property name="repeatCount" value="0"/>
25     </bean>
26     
27     <span style="white-space:pre"></span>
28     <bean id="startQuertz" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
29         <property name="triggers">
30             <list>
31                 <ref bean="simpleTrigger"/>
32             </list>
33         </property>
34     </bean>
35 </beans>

这是第二版本 ,这个是可以定时执行的,这里设置的半小时执行一次

<?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:mvc="http://www.springframework.org/schema/mvc"  
       xmlns:context="http://www.springframework.org/schema/context"  
       xmlns:util="http://www.springframework.org/schema/util"  
       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.0.xsd  
        http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd">  

    <!-- 启动触发器的配置开始 -->
    <bean name="startQuertz" lazy-init="false"  autowire="no"
        class="org.springframework.scheduling.quartz.SchedulerFactoryBean">  
        <property name="triggers">
            <list>
                <ref bean="ConnectJobTrigger" />
            </list>
        </property>  
    </bean>
    <!-- 启动触发器的配置结束 --> 

    <!-- quartz-2.x的配置 -->  
    <bean id="ConnectJobTrigger"  
        class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">  
        <property name="jobDetail">  
            <ref bean="ConnectJobDetail" />
        </property>  
        <property name="cronExpression">
            <value>0 0,30 8-17 * * ?</value>
        </property>
    </bean>

    <!-- job的配置开始 -->  
    <bean id="ConnectJobDetail" 
        class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">  
        <property name="targetObject">
            <ref bean="ConnectJob" />
        </property>
        <property name="targetMethod">  
            <value>process</value>
        </property>
        <!-- 上一次未执行完成的,要等待有再执行。 -->
        <property name="concurrent" value="false"></property>
    </bean>

    <!-- 工作的bean -->
    <bean id="ConnectJob" class="com.shr.jobs.ConnectJob" />


</beans>  

猜你喜欢

转载自blog.csdn.net/liguangix/article/details/80371055