想知道spring事务管理和任务调度的详细配置吗?
1.基本配置
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
|
<?
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:context
=
"http://www.springframework.org/schema/context"
xmlns:aop
=
"http://www.springframework.org/schema/aop"
xmlns:tx
=
"http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-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/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">
<!-- 打开Spring的自动扫描机制 -->
<
context:component-scan
base-package
=
"com.sshdemo"
/>
<!-- 打开aop注解支持 -->
<
aop:aspectj-autoproxy
/>
<!-- 定义数据源Bean,使用C3P0数据源实现 -->
<
bean
id
=
"dataSource"
class
=
"com.mchange.v2.c3p0.ComboPooledDataSource"
destroy-method
=
"close"
>
<!-- 指定连接数据库的驱动 -->
<
property
name
=
"driverClass"
value
=
"com.mysql.jdbc.Driver"
/>
<!-- 指定连接数据库的URL -->
<
property
name
=
"jdbcUrl"
value
=
"jdbc:mysql://localhost/ssh"
/>
<!-- 指定连接数据库的用户名 -->
<
property
name
=
"user"
value
=
"root"
/>
<!-- 指定连接数据库的密码 -->
<
property
name
=
"password"
value
=
"root"
/>
<!--连接池中保留的最大连接数。Default: 15 -->
<
property
name
=
"maxPoolSize"
value
=
"40"
/>
<!-- 指定连接池的最小连接数 -->
<
property
name
=
"minPoolSize"
value
=
"10"
/>
<!-- 指定连接池的初始化连接数 取值应在minPoolSize与maxPoolSize之间。默认: 3 -->
<
property
name
=
"initialPoolSize"
value
=
"5"
/>
<!-- 解决Mysql中的8小时问题: -->
<!--最大空闲时间,25000秒内未使用则连接被丢弃。若为0则永不丢弃。Default: 0 -->
<
property
name
=
"maxIdleTime"
value
=
"25000"
/>
<!--如果设为true那么在取得连接的同时将校验连接的有效性。Default: false -->
<
property
name
=
"testConnectionOnCheckin"
value
=
"true"
/>
<!--每18000秒检查所有连接池中的空闲连接。Default: 0 -->
<
property
name
=
"idleConnectionTestPeriod"
value
=
"18000"
/>
</
bean
>
<!--定义了Hibernate的SessionFactory -->
<
bean
id
=
"sessionFactory"
class
=
"org.springframework.orm.hibernate3.LocalSessionFactoryBean"
>
<
property
name
=
"dataSource"
ref
=
"dataSource"
/>
<!-- 配置Hibernate的参数 -->
<
property
name
=
"hibernateProperties"
>
<
props
>
<!-- 指定数据库的方言 -->
<
prop
key
=
"hibernate.dialect"
>org.hibernate.dialect.MySQL5InnoDBDialect</
prop
>
<
prop
key
=
"hibernate.show_sql"
>true</
prop
>
<
prop
key
=
"hibernate.hbm2ddl.auto"
>update</
prop
>
<!-- JDBC执行批量更新语句的大小 清除缓存(定期清除缓存,减小压力 -->
<
prop
key
=
"hibernate.jdbc.batch_size"
>30</
prop
>
</
props
>
</
property
>
<
property
name
=
"mappingResources"
>
<!-- 映射的文件 -->
<
list
>
<
value
>com/sshdemo/model/Hibernate.hbm.xml</
value
>
</
list
>
</
property
>
</
bean
>
|
<!-- aop拦截 -->
1
2
3
4
5
6
7
8
9
10
|
<
aop:config
>
<
aop:aspect
id
=
"DemoImp"
ref
=
"aspectDemoImp"
>
<
aop:pointcut
expression
=
"execution(* com.sshdemo.service.imp.*.*(*))"
id
=
"myPointCut"
/>
<
aop:before
pointcut-ref
=
"myPointCut"
method
=
"checkSecurity"
/>
<
aop:around
pointcut-ref
=
"myPointCut"
method
=
"doLoggInfo"
/>
<
aop:after
pointcut-ref
=
"myPointCut"
method
=
"doTranscation"
/>
</
aop:aspect
>
</
aop:config
>
<
bean
id
=
"aspectDemoImp"
class
=
"com.sshdemo.aspect.AspectDemoImpl"
/>
</
beans
>
|
2.事务管理
aop
<!-- 事务处理 (aop:config)-->
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
<
bean
id
=
"transactionManager"
class
=
"org.springframework.orm.hibernate3.HibernateTransactionManager"
>
<
property
name
=
"sessionFactory"
ref
=
"sessionFactory"
/>
</
bean
>
<!-- 采用@Transactional注解方式使用事务 -->
<!-- <tx:annotation-driven transaction-manager="transactionManager"/> -->
<
tx:advice
id
=
"txAdvice"
transaction-manager
=
"transactionManager"
>
<
tx:attributes
>
<
tx:method
name
=
"find*"
propagation
=
"NOT_SUPPORTED"
read-only
=
"true"
/>
<
tx:method
name
=
"insert*"
propagation
=
"REQUIRED"
/>
</
tx:attributes
>
</
tx:advice
>
<
aop:config
>
<
aop:pointcut
expression
=
"execution(* com.sshdemo.service.imp.*.*(*))"
id
=
"txPointCut"
/>
<
aop:advisor
advice-ref
=
"txAdvice"
pointcut-ref
=
"txPointCut"
/>
</
aop:config
>
|
3.任务调度配置
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<!-- QuertZ任务调度 -->
1
2
3
4
5
6
7
8
9
|
<
bean
id
=
"myQuertZ"
class
=
"com.sshdemo.quartZ.MydemoQuartZ"
></
bean
>
<
bean
id
=
"testQuartZ"
class
=
"org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean"
>
<
property
name
=
"targetObject"
>
<
ref
bean
=
"myQuertZ"
/>
</
property
>
<
property
name
=
"targetMethod"
>
<
value
>doRepeatReport</
value
>
</
property
>
</
bean
>
|
<!--触发器的bean的设置,在这里我们设置了我们要触发的jobDetail是哪个。这里我们定义了要触发的jobDetail是TestQuartZ,
即触发器去触发哪个bean..并且我们还定义了触发的时间:每天5:17pm-->
1
2
3
4
5
6
7
8
9
|
<
bean
id
=
"quertZDemo"
class
=
"org.springframework.scheduling.quartz.CronTriggerBean"
>
<
property
name
=
"jobDetail"
>
<
ref
bean
=
"testQuartZ"
/>
</
property
>
<
property
name
=
"cronExpression"
>
<!-- 触发时间(表达式) -->
<
value
>0/10 * * ? * *</
value
>
</
property
>
</
bean
>
|
<!--管理触发器的总设置,管理我们的触发器列表,可以在bean的list中放置多个触发器。
-->
1
2
3
4
5
6
7
8
|
<
bean
autowire
=
"no"
class
=
"org.springframework.scheduling.quartz.SchedulerFactoryBean"
>
<
property
name
=
"triggers"
>
<
list
>
<
ref
bean
=
"quertZDemo"
/>
</
list
>
</
property
>
</
bean
>
</
beans
>
|