加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

applicationContext.xml、quartzConfig.xml配置理解

发布时间:2020-12-16 09:23:38 所属栏目:百科 来源:网络整理
导读:前言 :使用spring+mybatis+mysql的框架构建Java项目已经有段时间了,通过学习和总结对于applicationContext.xml文件以及自动化任务quartzConfig.xml文件有了很多认识,那么我想把自己的末学浅见记录下来,给有需要的朋友一点点帮助。 Java项目 applicationC

前言:使用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/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
     http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
     http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd
     http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
     http://www.springframework.org/schema/aop http://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配置。

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读