applicationContext、quartzConfig配置

简介: applicationContext、quartzConfig配置

使用spring+mybatis+mysql的框架构建Java项目已经有段时间了,通过学习和总结对于applicationContext.xml文件以及自动化任务quartzConfig.xml文件有了很多认识,那么我想把自己的末学浅见记录下来,给有需要的朋友一点点帮助。

Java项目


applicationContext.xml配置项中包含了 引入jdbc配置文件、创建jdbc数据源、quartz等,见以下内容

<?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:aop="http://www.springframework.org/schema/aop"

xmlns:tx="http://www.springframework.org/schema/tx"

xmlns:jdbc="http://www.springframework.org/schema/jdbc"

xmlns:context="http://www.springframework.org/schema/context"

xmlns:task="http://www.springframework.org/schema/task"

xsi:schemaLocation="

   http://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context-3.0.xsd

   http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-3.0.xsd

   http://www.springframework.org/schema/jdbchttp://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd

   http://www.springframework.org/schema/txhttp://www.springframework.org/schema/tx/spring-tx-3.0.xsd

   http://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop-3.0.xsd

   http://www.springframework.org/schema/task  

   http://www.springframework.org/schema/task/spring-task-3.1.xsd ">


<!-- 引入jdbc配置文件 -->

<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">

       <property name="locations">

           <value>conf/jdbc.properties</value>

       </property>

   </bean>


<!--创建jdbc数据源 -->

<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">

 <property name="driverClassName" value="${driver}"></property>

 <!-- 加上utf8的编码方式后,项目在向数据库存放数据时会转码为utf8,获取数据时同样会最后转为utf8,这样就会防止编码不一致的时候出现乱码 -->

 <property name="url" value="${url}?useUnicode=true&characterEncoding=utf8&"></property>

 <property name="username" value="${username}"></property>

 <property name="password" value="${password}"></property>

 

 <!-- 初始化连接数 -->

 <property name="initialSize" value="10"></property>

 <!-- 最大连接数量 -->

 <property name="maxActive" value="100"></property>

 <!-- 最大空闲数量 -->

 <property name="maxIdle" value="20"></property>

 <!-- 最小空闲数量 -->

 <property name="minIdle" value="5"></property>

 <!-- 超时等待时间 -->

 <property name="maxWait" value="60000"></property>

 

 <!-- 开启池的prepared statement 池功能 -->

 <property name="poolPreparedStatements" value="true"></property>

 <!-- statement池能够同时分配的打开的statements的最大数量 -->

 <property name="maxOpenPreparedStatements" value="5"></property>

 

 <!-- 以下两个元素要一起使用,作用是为每次使用sqlsession的connection时进行验证,防止连接失效出现错误 -->

 <property name="testOnBorrow" value="true" />

 <property name="validationQuery">

  <value>select 1 from DUAL</value>

 </property>

</bean>


<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">

 <!--实体类-->

 <property name="typeAliasesPackage" value="com.database.entity" />

 <property name="dataSource" ref="dataSource" />

</bean>


<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">

       <property name="dataSource" ref="dataSource" />

   </bean>

 

   <!--

    1.该元素除了替代annotation-config元素告诉spring我们使用注解的方式自动装配外,还允许spring自动检测bean和定义bean

    2.base-package标识了该元素所扫描的包

    3.include-filter过滤出expression所匹配的类

   -->

   <context:component-scan base-package="com.honzh.socket">

    <context:include-filter type="regex" expression=".biz.*"/>

    <context:include-filter type="regex" expression=".service.*"/>

   </context:component-scan>

 

<!--定义注解驱动的事务,该元素告诉spring检查上下文中所有的bean并查找transactional注解的bean,不管是类级别还是方法级别-->

   <tx:annotation-driven/>

 

<!--mybatis-spring提供的类,将mapper接口生成代理注入到spring中-->

   <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">

       <property name="basePackage" value="com.honbase.mapper" />

   </bean>

 

<!-- 定义quartz专属的工作类 -->

<bean id="systemService" class="com.hoess.service.SystemService"></bean>


<!-- 导入quartz的定时任务 -->

<import resource="quartzConfig.xml" />

</beans>

上面我把注意点都通过注释写了出来,下面是定时quartzConfig.xml任务的配置,见以下内容

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"

xmlns:tx="http://www.springframework.org/schema/tx" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xmlns:context="http://www.springframework.org/schema/context"

xmlns:aop="http://www.springframework.org/schema/aop" xmlns:p="http://www.springframework.org/schema/p"

xsi:schemaLocation="http://www.springframework.org/schema/beans

     http://www.springframework.org/schema/beans/spring-beans.xsd

     http://www.springframework.org/schema/tx

     http://www.springframework.org/schema/tx/spring-tx.xsd

     http://www.springframework.org/schema/context

     http://www.springframework.org/schema/context/spring-context.xsd

     http://www.springframework.org/schema/aop

     http://www.springframework.org/schema/aop/spring-aop.xsd">

   <!-- 定时任务Quartz配置 -->

<!-- 开盘 -->

   <bean id="open" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">

    <property name="targetObject">

        <ref bean="systemService"/>

    </property>

    <property name="targetMethod">

        <value>open</value>

    </property>

    <property name="concurrent">

           <value>false</value>

       </property>

   </bean>

 

   <bean id="openTime" class="org.springframework.scheduling.quartz.CronTriggerBean">

       <property name="jobDetail">

          <ref bean="open"/>

       </property>

       <property name="cronExpression">

            <value>0 0 19 * * ?</value>

       </property>

   </bean>

<bean id="startQuertz" lazy-init="false" autowire="no" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">

       <property name="triggers">

           <list>

               <ref bean="openTime"/>

        </list>

       </property>

   </bean>

</beans>

以上内容我就不加注释了,比较容易理解,然后是对应SystemService类的开盘执行方法

// 开盘

public void open() {

 logger.info("--------------------开盘------------------------");

}

Java web项目


只有一个点不相同,我只把他列出来

<!-- 引入jdbc配置文件 -->

<context:property-placeholder location="classpath:jdbc.properties" />

导入的jdbc文件方式不一样,这种是spring最基本的外部文件导入方式,通过在类路径的src根目录找出jdbc配置。


总结:这些配置不是经常需要变动,一个项目只需要配置一次就够了。

相关文章
|
6月前
|
Java Spring 容器
BeanFactory 和 ApplicationContext 有什么区别
BeanFactory 和 ApplicationContext 有什么区别
20 1
|
8月前
|
XML Java 数据格式
BeanFactory 与 ApplicationContext 区别
BeanFactory 是 Spring 框架中 IoC 容器的顶层接⼝, 它只是⽤来定义⼀些基础功能, 定义⼀些基础规范
21 0
|
9月前
|
Java Spring 容器
BeanFactory 和 ApplicationContext 有什么区别?
BeanFactory 和 ApplicationContext 有什么区别?
41 0
|
10月前
|
XML 人工智能 Java
Spring中的BeanFactory和ApplicationContext介绍
Spring中的BeanFactory和ApplicationContext介绍
|
11月前
|
Java Spring
使用JDBCTemplate实现与Spring结合,方法公用 ——Spring配置(applicationContext.xml)
使用JDBCTemplate实现与Spring结合,方法公用 ——Spring配置(applicationContext.xml)
53 0
|
缓存 Java Spring
ApplicationContext(1)--Spring源码解析(一)
ApplicationContext(1)--Spring源码解析(一)
ApplicationContext(1)--Spring源码解析(一)
|
XML 缓存 Java
|
XML 自动驾驶 小程序
BeanFactory和ApplicationContext有什么区别?
BeanFactory和ApplicationContext有什么区别?
BeanFactory和ApplicationContext有什么区别?
|
Java 程序员 API
spring的BeanFactory和ApplicationContext
在Spring框架中,BeanFactory和ApplicationContext都是大名鼎鼎,BeanFactory是基础ApplicationContext是扩展这句话怎么理解?请随本文一同去了解它们
171 0
spring的BeanFactory和ApplicationContext
|
XML 缓存 Java
spring(二)BeanFactory与ApplicationContext
简介 Spring的本质是一个bean工厂(beanFactory)或者说bean容器,它按照我们的要求,生产我们需要的各种各样的bean,提供给我们使用。 BeanFactory 是 Spring 框架的基础设施,面向 Spring 本身; ApplicationContext 面向使用 Spring 框架的开发者,几乎所有的应用场合我们都直接使用 ApplicationContext 而非底层的 BeanFactory。