spring 部分配置内容备忘

1.spring定时器简单配置:

<bean name="taskJob" class="com.netcloud.mail.util.TaskJob">
          <property name="mainExecutor" ref="mainExecutor"></property>
          <property name="emailService" ref="emailServiceImpl"></property>
          <property name="userService" ref="userServiceImpl"></property>
    </bean>
    
    <!-- 定时任务的配置  开始 -->
    <bean id="methodInvokingJobDetail" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
        <property name="targetObject">
           <ref bean="taskJob"/>
        </property>
        <property name="targetMethod">
           <value>sendMail</value>
        </property>
    <property name="concurrent" value="false"/>
</bean> <bean id="cronTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean"> <property name="jobDetail"> <ref bean="methodInvokingJobDetail" /> </property> <property name="cronExpression"> <value>0 0/1 * * * ?</value> </property> </bean> <bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean"> <property name="triggers"> <list> <ref local="cronTrigger"/> </list> </property> </bean> <!-- 定时任务配置 结束 -->

配置内容:(1)targetObject指向taskJob ,也就是com.netcloud.mail.util.TaskJob

(2)targetMethod指向sendMail,由(1)(2)可知,指定需要定时执行com.netcloud.mail.util.TaskJob类的sendMail方法。注意:此方法没有参数,如果TaskJob有两个方法sendMail()和sendMail(String argument),则spring只会去执行无参的sendMail().

(3)concurrent设置为false , 对于相同的JobDetail,当指定多个Trigger时, 很可能第一个job完成之前,第二个job就开始了。指定concurrent设为false,多个job不会并发运行,第二个job将不会在第一个job完成之前开始。

(4)triggers 可以在其中list内放入多个触发器。

关于时间的配置,描述如下:

0 0/1 * * * ? 每隔1分钟执行一下定时任务

时间大小由小到大排列,从秒开始,顺序为 秒,分,时,天,月,年    *为任意 ?为无限制。由此上面所配置的内容就是,在每天的16点26分启动znrwdb方法


具体时间设定可参考

"0/10 * * * * ?" 每10秒触发

"0 0 12 * * ?" 每天中午12点触发
"0 15 10 ? * *" 每天上午10:15触发
"0 15 10 * * ?" 每天上午10:15触发
"0 15 10 * * ? *" 每天上午10:15触发
"0 15 10 * * ? 2005" 2005年的每天上午10:15触发
"0 * 14 * * ?" 在每天下午2点到下午2:59期间的每1分钟触发
"0 0/5 14 * * ?" 在每天下午2点到下午2:55期间的每5分钟触发
"0 0/5 14,18 * * ?" 在每天下午2点到2:55期间和下午6点到6:55期间的每5分钟触发
"0 0-5 14 * * ?" 在每天下午2点到下午2:05期间的每1分钟触发
"0 10,44 14 ? 3 WED" 每年三月的星期三的下午2:10和2:44触发
"0 15 10 ? * MON-FRI" 周一至周五的上午10:15触发
"0 15 10 15 * ?" 每月15日上午10:15触发
"0 15 10 L * ?" 每月最后一日的上午10:15触发
"0 15 10 ? * 6L" 每月的最后一个星期五上午10:15触发
"0 15 10 ? * 6L 2002-2005" 2002年至2005年的每月的最后一个星期五上午10:15触发
"0 15 10 ? * 6#3" 每月的第三个星期五上午10:15触发
每隔5秒执行一次:*/5 * * * * ?
每隔1分钟执行一次:0 0/1 * * * ?
每天23点执行一次:0 0 23 * * ?
每天凌晨1点执行一次:0 0 1 * * ?
每月1号凌晨1点执行一次:0 0 1 1 * ?
每月最后一天23点执行一次:0 0 23 L * ?
每周星期天凌晨1点实行一次:0 0 1 ? * L
在26分、29分、33分执行一次:0 26,29,33 * * * ?
每天的0点、13点、18点、21点都执行一次:0 0 0,13,18,21 * * ?

2.spring组件扫描<context:component-scan/>使用详解(来源:http://blog.csdn.net/a9529lty/article/details/8251003)

我们知道如果不想在xml文件中配置bean,我们可以给我们的类加上spring组件注解,只需再配置下spring的扫描器就可以实现bean的自动载入。

如下简单实例

<!-- 定义扫描根路径为leot.test,不使用默认的扫描方式 -->
<context:component-scan base-package="leot.test" use-default-filters="false">
  <!-- 扫描符合@Service @Repository的类 -->
  <context:include-filter type="annotation" expression="org.springframework.stereotype.Service" />
  <context:include-filter type="annotation" expression="org.springframework.stereotype.Repository" />
</context:component-scan>

有了<context:component-scan>,另一个<context:annotation-config/>标签根本可以移除掉,因为已经被包含进去了。

<context:component-scan>提供两个子标签:<context:include-filter>和<context:exclude-filter>各代表引入和排除的过滤。

filter标签在Spring3有五个type,如下:

Filter Type Examples Expression Description
annotation org.example.SomeAnnotation 符合SomeAnnoation的target class(如上的例子)
assignable org.example.SomeClass 指定class或interface的全名
aspectj org.example..*Service+ AspectJ语法
regex org.example.Default.* Regelar Expression(正则表达式,如下的例子)
custom org.example.MyTypeFilter Spring3新增自訂Type,实作org.springframework.core.type.TypeFilter

正则表达式匹配自动载入bean的路径

<context:component-scan base-package="com.xhlx.finance.budget" >
  <context:include-filter type="regex" expression=".service.*"/>
</context:component-scan>

3.

原文地址:https://www.cnblogs.com/cuiyf/p/3478822.html