spring-task的配置及使用

一:在web.xml文件中添加对容器启动的监听, 当容器启动后将自动装配application-job.xml文件
<!-- 为上下文参数(相当于全局变量)contextConfigLocation赋值,该配合指向application开头的xml文件, 定时任务的配置文件 application-job.xml也将被读取 -->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:application*.xml</param-value>
    </context-param>
    
    <!-- 对web容器启动进行监听,当容器启动后将自动装配上下文变量contextConfigLocation指向的配置文件 -->
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>


二:配置application-job.xml文件

注意在<beans>节点中配置xmlns:task和xsi:schemaLocation关于task的属性值

<?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:context="http://www.springframework.org/schema/context"
	xmlns:task="http://www.springframework.org/schema/task"
	xsi:schemaLocation="http://www.springframework.org/schema/beans 
		http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/task 
        http://www.springframework.org/schema/task/spring-task-3.0.xsd">

	<description>Job Config</description>
	
	<!-- 扫描定时器所在的包路径 -->
	<context:component-scan base-package="org.power.quickbuy.job" />

	<!-- 配置定时任务 -->
	<task:scheduled-tasks>
		<task:scheduled ref="alertJob" method="execute" cron="0 0 9 * * ?" />
	</task:scheduled-tasks>
	<task:scheduled-tasks>
		<task:scheduled ref="statisticsJob" method="execute" cron="0 0 1 * * ?" />
	</task:scheduled-tasks>
</beans>

三: 创建定时任务的类及方法

注意: 类头部需要@Service注解

package org.power.quickbuy.job;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

@Service("alertJob")
public class AlertJob {
	private static Logger logger = LoggerFactory.getLogger(AlertJob.class);
	
	public void execute(){
		logger.info("当前时间:[{}]", System.currentTimeMillis());
	}
}

四:定时器配置中的时间表达式请参照如下网址: http://blog.csdn.net/javaecrainbow/article/details/8998291



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring Security中配置跨域有多种方法。一种常见的方法是使用@CrossOrigin注解或重写addCorsMappings方法来配置跨域,但是当项目中引入了Spring Security依赖后,这种配置方式可能会失效。 为了解决这个问题,可以使用Spring Security提供的更专业的跨域方案。首先,需要创建一个继承自WebSecurityConfigurerAdapter的配置类,并重写configure方法。在configure方法中,可以通过调用HttpSecurity对象的cors方法来启用跨域配置。 在cors方法中,可以通过CorsConfigurationSource对象的configurationSource方法来配置具体的跨域设置。可以使用CorsConfiguration对象来设置允许的请求头、请求方法和请求来源。此外,还可以设置预检请求的缓存时间。最后,需要将CorsConfiguration对象注册到UrlBasedCorsConfigurationSource对象中。 下面是一个示例的配置类的代码: ```java @Configuration public class WebSecurityConfig extends WebSecurityConfigurerAdapter { @Override protected void configure(HttpSecurity http) throws Exception { http.authorizeRequests() .mvcMatchers("/hello1").permitAll() .anyRequest().authenticated() .and() .formLogin() .and() .cors() // 跨域配置 .configurationSource(configurationSource()); } CorsConfigurationSource configurationSource() { CorsConfiguration corsConfiguration = new CorsConfiguration(); corsConfiguration.setAllowedHeaders(Collections.singletonList("*")); corsConfiguration.setAllowedMethods(Collections.singletonList("*")); corsConfiguration.setAllowedOrigins(Collections.singletonList("*")); corsConfiguration.setMaxAge(3600L); UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource(); source.registerCorsConfiguration("/**", corsConfiguration); return source; } } ``` 通过以上配置Spring Security会自动应用跨域配置,并且保持其他安全配置不受影响。这种方式可以确保跨域配置Spring Security过滤器之前生效,避免了跨域配置失效的问题。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* *3* [Spring Security(七) ——跨域配置](https://blog.csdn.net/tongkongyu/article/details/125982927)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 100%"] [ .reference_list ]

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值