applicationContext、quartzConfig配置

简介: 版权声明:欢迎转载,请注明沉默王二原创。 https://blog.csdn.net/qing_gee/article/details/41864501 使用spring+mybatis+mysql的框架构建Java项目已经有段时间了,通过学习和总结对于applicationContext.xml文件以及自动化任务quartzConfig.xml文件有了很多认识,那么我想把自己的末学浅见记录下来,给有需要的朋友一点点帮助。
版权声明:欢迎转载,请注明沉默王二原创。 https://blog.csdn.net/qing_gee/article/details/41864501

使用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配置。

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


相关文章
|
17天前
|
存储 弹性计算 人工智能
【2025云栖精华内容】 打造持续领先,全球覆盖的澎湃算力底座——通用计算产品发布与行业实践专场回顾
2025年9月24日,阿里云弹性计算团队多位产品、技术专家及服务器团队技术专家共同在【2025云栖大会】现场带来了《通用计算产品发布与行业实践》的专场论坛,本论坛聚焦弹性计算多款通用算力产品发布。同时,ECS云服务器安全能力、资源售卖模式、计算AI助手等用户体验关键环节也宣布升级,让用云更简单、更智能。海尔三翼鸟云服务负责人刘建锋先生作为特邀嘉宾,莅临现场分享了关于阿里云ECS g9i推动AIoT平台的场景落地实践。
【2025云栖精华内容】 打造持续领先,全球覆盖的澎湃算力底座——通用计算产品发布与行业实践专场回顾
|
9天前
|
云安全 人工智能 安全
Dify平台集成阿里云AI安全护栏,构建AI Runtime安全防线
阿里云 AI 安全护栏加入Dify平台,打造可信赖的 AI
|
12天前
|
人工智能 运维 Java
Spring AI Alibaba Admin 开源!以数据为中心的 Agent 开发平台
Spring AI Alibaba Admin 正式发布!一站式实现 Prompt 管理、动态热更新、评测集构建、自动化评估与全链路可观测,助力企业高效构建可信赖的 AI Agent 应用。开源共建,现已上线!
1045 33
|
11天前
|
机器学习/深度学习 人工智能 搜索推荐
万字长文深度解析最新Deep Research技术:前沿架构、核心技术与未来展望
近期发生了什么自 2025 年 2 月 OpenAI 正式发布Deep Research以来,深度研究/深度搜索(Deep Research / Deep Search)正在成为信息检索与知识工作的全新范式:系统以多步推理驱动大规模联网检索、跨源证据。
806 55
|
9天前
|
文字识别 测试技术 开发者
Qwen3-VL新成员 2B、32B来啦!更适合开发者体质
Qwen3-VL家族重磅推出2B与32B双版本,轻量高效与超强推理兼备,一模型通吃多模态与纯文本任务!
697 11
下一篇
开通oss服务