[size=large][/size][size=large][/size][align=center][/align][size=large][/size]这个小小的测试我的工作目录如下
[img]http://dl.iteye.com/upload/attachment/588775/4629b91f-b494-3b59-b7ab-b94904925f5b.png[/img]
package Test;
public class QuartzJob {
public void work()
{
System.out.println("Quartz的任务调度!!!");
}
[size=large][/size][size=large][/size][size=x-large][/size]
}
applicationcontext.xml配置如下
<?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:jee="http://www.springframework.org/schema/jee"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-2.0.xsd">
<!-- 要调用的工作类 -->
<bean id="quartzJob" class="Test.QuartzJob"></bean>
<!-- 定义调用对象和调用对象的方法 -->
<bean id="jobtask" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
<!-- 调用的类 -->
<property name="targetObject">
<ref bean="quartzJob"/>
</property>
<!-- 调用类中的方法 -->
<property name="targetMethod">
<value>work</value>
</property>
</bean>
<!-- 定义触发时间 -->
<bean id="doTime" class="org.springframework.scheduling.quartz.CronTriggerBean">
<property name="jobDetail">
<ref bean="jobtask"/>
</property>
<!-- cron表达式 -->
<property name="cronExpression">
<!-- 每隔10秒执行一次-->
<value>0/10 * * * * ?</value>
</property>
</bean>
<!-- 总管理类 如果将lazy-init='false'那么容器启动就会执行调度程序 -->
<bean id="startQuertz" lazy-init="false" autowire="no" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<property name="triggers">
<list>
<ref bean="doTime"/>
</list>
</property>
</bean>
</beans>
为了要初始化,我们要在web.xml中配置spring.xml信息
<?xml version="1.0" encoding="UTF-8"?>
<web-app 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">
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<!--确定多个配置文件-->
<context-param>
<!-- 参数名为contextConfigLocation-->
<param-name>contextConfigLocation</param-name>
<!-- 多个配置文件之间以,隔-->
<param-value>/WEB-INF/applicationcontext.xml</param-value>
</context-param>
<!-- 采用listener创建Applicat工onContext 实例-->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
</web-app>
为什么要这么写呢?
spring提供了一个ServletContextListener 接口的实现类ContextLoaderListener,该类可以做为listener使用,他会在创建时自动查找WEB-INF/下的applicationcontext.xml如果只有一个配置文件,并且文件名为applicationcontext.xml时只要加<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
由于contextLoadListener会优先寻找参数为contextConfigLocation。在配置多个配置文件时如下:
<context-param>
<!-- 参数名为contextConfigLocation-->
<param-name>contextConfigLocation</param-name>
<!-- 多个配置文件之间以空格隔开,也可以加或者不加,只要加空格就行>
<param-value>/WEB-INF/NewFile.xml /WEB-INF/applicationcontext.xml</param-value>
</context-param>
我们最好设参数名为contextConfigLocation便于查找applicationcontext.xml.
第二:
我们还可以用servlet类 ContextLoaderServlet.该servlet启动时会自动查找web-inf下面的applicationcontext.xml文件。
通常<load-on-startup>参数设得越小越优先启动。所以越小越好
<servlet>
<servlet-name>context</servlet口-arne>
<servlet-class>org.springframework.web.context.ContextLoaderServlet</
servlet-class>
<load-on-startup>l</load-o 口-startup>
</servlet>
2, 使用匹配符
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext*.xml</param-value>
</context-param>
比如说用到Hibernate,则把hibernate相关的配置放在applicationContext-hibernate.xml这一个文件,而一些全局相关的信息则放在applicationContext.xml,其他的配置类似.这样就可以加载了,不必写用空格或是逗号分开!
3, 如果使用struts加载多个spring配置文件.下面这个配置的其实也是contextConfigLocation变量.
struts-config.xml里面加这个
<plug-in className="org.springframework.web.struts.ContextLoaderPlugIn">
<set-property property="contextConfigLocation"
value="/WEB-INF/applicationContext.xml,
/WEB-INF/action-servlet.xml,,,,,,,"/>
4,如果是非j2ee应用直接程序加载.
ApplicationContext act = new ClassPathXmlApplicationContext(new String[]{"bean1.xml","bean2.xml"});
BeanDefinitionRegistry reg = new DefaultListableBeanFactory();
XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(reg);
reader.loadBeanDefinitions(new ClassPathResource("bean1.xml"));
reader.loadBeanDefinitions(new ClassPathResource("bean2.xml"));
BeanFactory bf = (BeanFactory)reg;
[img]http://dl.iteye.com/upload/attachment/588775/4629b91f-b494-3b59-b7ab-b94904925f5b.png[/img]
package Test;
public class QuartzJob {
public void work()
{
System.out.println("Quartz的任务调度!!!");
}
[size=large][/size][size=large][/size][size=x-large][/size]
}
applicationcontext.xml配置如下
<?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:jee="http://www.springframework.org/schema/jee"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-2.0.xsd">
<!-- 要调用的工作类 -->
<bean id="quartzJob" class="Test.QuartzJob"></bean>
<!-- 定义调用对象和调用对象的方法 -->
<bean id="jobtask" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
<!-- 调用的类 -->
<property name="targetObject">
<ref bean="quartzJob"/>
</property>
<!-- 调用类中的方法 -->
<property name="targetMethod">
<value>work</value>
</property>
</bean>
<!-- 定义触发时间 -->
<bean id="doTime" class="org.springframework.scheduling.quartz.CronTriggerBean">
<property name="jobDetail">
<ref bean="jobtask"/>
</property>
<!-- cron表达式 -->
<property name="cronExpression">
<!-- 每隔10秒执行一次-->
<value>0/10 * * * * ?</value>
</property>
</bean>
<!-- 总管理类 如果将lazy-init='false'那么容器启动就会执行调度程序 -->
<bean id="startQuertz" lazy-init="false" autowire="no" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<property name="triggers">
<list>
<ref bean="doTime"/>
</list>
</property>
</bean>
</beans>
为了要初始化,我们要在web.xml中配置spring.xml信息
<?xml version="1.0" encoding="UTF-8"?>
<web-app 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">
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<!--确定多个配置文件-->
<context-param>
<!-- 参数名为contextConfigLocation-->
<param-name>contextConfigLocation</param-name>
<!-- 多个配置文件之间以,隔-->
<param-value>/WEB-INF/applicationcontext.xml</param-value>
</context-param>
<!-- 采用listener创建Applicat工onContext 实例-->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
</web-app>
为什么要这么写呢?
spring提供了一个ServletContextListener 接口的实现类ContextLoaderListener,该类可以做为listener使用,他会在创建时自动查找WEB-INF/下的applicationcontext.xml如果只有一个配置文件,并且文件名为applicationcontext.xml时只要加<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
由于contextLoadListener会优先寻找参数为contextConfigLocation。在配置多个配置文件时如下:
<context-param>
<!-- 参数名为contextConfigLocation-->
<param-name>contextConfigLocation</param-name>
<!-- 多个配置文件之间以空格隔开,也可以加或者不加,只要加空格就行>
<param-value>/WEB-INF/NewFile.xml /WEB-INF/applicationcontext.xml</param-value>
</context-param>
我们最好设参数名为contextConfigLocation便于查找applicationcontext.xml.
第二:
我们还可以用servlet类 ContextLoaderServlet.该servlet启动时会自动查找web-inf下面的applicationcontext.xml文件。
通常<load-on-startup>参数设得越小越优先启动。所以越小越好
<servlet>
<servlet-name>context</servlet口-arne>
<servlet-class>org.springframework.web.context.ContextLoaderServlet</
servlet-class>
<load-on-startup>l</load-o 口-startup>
</servlet>
2, 使用匹配符
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext*.xml</param-value>
</context-param>
比如说用到Hibernate,则把hibernate相关的配置放在applicationContext-hibernate.xml这一个文件,而一些全局相关的信息则放在applicationContext.xml,其他的配置类似.这样就可以加载了,不必写用空格或是逗号分开!
3, 如果使用struts加载多个spring配置文件.下面这个配置的其实也是contextConfigLocation变量.
struts-config.xml里面加这个
<plug-in className="org.springframework.web.struts.ContextLoaderPlugIn">
<set-property property="contextConfigLocation"
value="/WEB-INF/applicationContext.xml,
/WEB-INF/action-servlet.xml,,,,,,,"/>
4,如果是非j2ee应用直接程序加载.
ApplicationContext act = new ClassPathXmlApplicationContext(new String[]{"bean1.xml","bean2.xml"});
BeanDefinitionRegistry reg = new DefaultListableBeanFactory();
XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(reg);
reader.loadBeanDefinitions(new ClassPathResource("bean1.xml"));
reader.loadBeanDefinitions(new ClassPathResource("bean2.xml"));
BeanFactory bf = (BeanFactory)reg;