Spingmvc,quartz
接口json响应
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.9.6</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.9.6</version>
</dependency>
@RequestMapping("/testJson")
@ResponseBody
public TestResponse testJson(TestRequest request) throws ServletException{
System.out.println(request);
TestResponse res = new TestResponse();
res.setId("idsss");
res.setName(request.getName());
return res;
}
基础配置
<!--是用于激活那些已经在spring容器里注册过的bean(无论是通过xml的方式还是通过package sanning的方式)上面的注解。-->
<context:annotation-config/>
<!--除了具有<context:annotation-config>的功能之外,<context:component-scan>还可以在指定的package下扫描以及注册javabean-->
<context:component-scan base-package="cn.com" />
<!--会自动注册RequestMappingHandlerMapping与RequestMappingHandlerAdapter两个Bean,这是Spring MVC为@Controller分发请求所必需的,并且提供了数据绑定支持,@NumberFormatannotation支持,@DateTimeFormat支持,@Valid支持读写XML的支持(JAXB)和读写JSON的支持(默认Jackson)等功能-->
<mvc:annotation-driven></mvc:annotation-driven>
<!--会在Spring MVC上下文中定义一个org.springframework.web.servlet.resource.DefaultServletHttpRequestHandler,它会像一个检查员,对进入DispatcherServlet的URL进行筛查,如果发现是静态资源的请求,就将该请求转由Web应用服务器默认的Servlet处理,如果不是静态资源的请求,才由DispatcherServlet继续处理-->
<mvc:default-servlet-handler></mvc:default-servlet-handler>
<!-- 声明自动为spring容器中那些配置@aspectJ切面的bean创建代理,织入切面 -->
<aop:aspectj-autoproxy></aop:aspectj-autoproxy>
<!--<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">-->
<!--<property name="messageConverters">-->
<!--<list>-->
<!--<bean id = "jacksonMessageConverter" class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">-->
<!--<property name="supportedMediaTypes">-->
<!--<list>-->
<!--<value>text/html;charset=UTF-8</value>-->
<!--<value>text/plain;charset=UTF-8</value>-->
<!--<value>application/json;charset=UTF-8</value>-->
<!--</list>-->
<!--</property>-->
<!--</bean>-->
<!--</list>-->
<!--</property>-->
<!--</bean>-->
<!--<bean class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver">-->
<!--<property name="defaultViews">-->
<!--<list>-->
<!--<bean class="org.springframework.web.servlet.view.json.MappingJackson2JsonView" />-->
<!--</list>-->
<!--</property>-->
<!--</bean>-->
<!--<bean id="demoService" class="service.imp.DemoServiceImpl"></bean>-->
<!-- <bean id="myAction" class="cn.com.action.MyAction">
<property name="demoService" ref="demoService"></property>
</bean> -->
<!--<bean id="myFileter" class="cn.com.action.MyFileter"></bean>-->
<!--<mvc:interceptors>-->
<!--<mvc:interceptor>-->
<!--<mvc:mapping path="/**"/>-->
<!--<bean class="cn.com.action.MyInterceptor"></bean>-->
<!--</mvc:interceptor>-->
<!--</mvc:interceptors>-->
<!--tomcat 启动-->
<dependency>
<groupId>javax</groupId>
<artifactId>javaee-api</artifactId>
<version>6.0</version>
<scope>provided</scope>
</dependency>
<!--<dependency>-->
<!--<groupId>javax.servlet</groupId>-->
<!--<artifactId>servlet-api</artifactId>-->
<!--<version>2.5</version>-->
<!--<scope>compile</scope>-->
<!--</dependency>-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>4.3.1.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>4.3.1.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>4.3.1.RELEASE</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.9.5</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.9.6</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.9.6</version>
</dependency>
<build>
<finalName>my2</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>8</source>
<target>8</target>
</configuration>
</plugin>
</plugins>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
<includes>
<include>*.*</include>
<include>**/*.*</include>
</includes>
</resource>
</resources>
</build>
<profiles>
<profile>
<id>dev</id>
<properties>
<env>-dev</env>
</properties>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
</profile>
<profile>
<id>test</id>
<properties>
<env>-test</env>
</properties>
</profile>
</profiles>
使用
<import resource="classpath*:mapi${env}/my-dao.xml"/>
clean install -Dmaven.test.skip=true -Ptest
<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<display-name>Archetype Created Web Application</display-name>
<servlet>
<servlet-name>springM</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:my-dubbo-provider.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springM</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<filter>
<filter-name>SessionFilter</filter-name>
<filter-class>cn.com.action.MyFileter</filter-class>
<!--<init-param>-->
<!--<param-name>logonStrings</param-name><!– 对登录页面不进行过滤 –>-->
<!--<param-value>/project/index.jsp;login.do</param-value>-->
<!--</init-param>-->
<init-param>
<param-name>includeStrings</param-name><!-- 只对指定过滤参数后缀进行过滤 -->
<param-value>.do;.jsp</param-value>
</init-param>
<!--<init-param>-->
<!--<param-name>redirectPath</param-name><!– 未通过跳转到登录界面 –>-->
<!--<param-value>/index.jsp</param-value>-->
<!--</init-param>-->
<!--<init-param>-->
<!--<param-name>disabletestfilter</param-name><!– Y:过滤无效 –>-->
<!--<param-value>N</param-value>-->
<!--</init-param>-->
</filter>
<filter-mapping>
<filter-name>SessionFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
quartz.springmvc 实现
applicationContext.xml
<import resource="classpath*:services.xml" />
<beans profile="dev">
<bean id="placeholderConfig" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath*:services-dev/services.properties</value>
</list>
</property>
</bean>
services.xml
<context:component-scan base-package="com.pingan.jkx.bj.services.service" />
<context:component-scan
base-package="com.pingan.jkx.bj.services.schedules" />
<task:scheduled-tasks>
<task:scheduled ref="deviceStatsticScheduled" method="job" cron="${deviceid.statstic.scheduled}" />
</task:scheduled-tasks>
services.properties
new.policy.scheduled=0 */1 * * * ?
@Service
public class DeviceStatsticScheduled {
public void job() {
task:scheduled 配置默认是单线程串行的,如下配置可以配置多线程。
<task:scheduler id="scheduler" pool-size="4" />
<task:scheduled-tasks scheduler="scheduler">
<task:scheduled ref="scheduleXmh" method="job" cron="0/2 * * * * ?" />
</task:scheduled-tasks>

浙公网安备 33010602011771号