spring进阶教程(一):减少spring的XML(javaconfig)

前言

作为一个java程序猿,相信大家都都用过spring,大家应该注意到了,spring的配置基本都是用xml实现,其实这样很难查错切难以理解,下面告诉大家如何减少spirng的XML配置,实现xml转成javaconfig。

参考项目:https://github.com/bigbeef/cppba-web
开源地址:https://github.com/bigbeef
个人博客:http://blog.cppba.com

看看我们传统的xml配置文件

这个是我们使用druid配置dataSource的xml代码:

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
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close">
<!-- 基本属性 url、user、password -->
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.user}" />
<property name="password" value="${jdbc.password}" />
<!-- 配置初始化大小、最小、最大 -->
<property name="initialSize" value="1" />
<property name="minIdle" value="1" />
<property name="maxActive" value="20" />
<!-- 配置获取连接等待超时的时间 -->
<property name="maxWait" value="60000" />
<!-- 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒 -->
<property name="timeBetweenEvictionRunsMillis" value="60000" />
<!-- 配置一个连接在池中最小生存的时间,单位是毫秒 -->
<property name="minEvictableIdleTimeMillis" value="300000" />
<property name="validationQuery" value="SELECT 'x'" />
<property name="testWhileIdle" value="true" />
<property name="testOnBorrow" value="false" />
<property name="testOnReturn" value="false" />
<!-- 打开PSCache,并且指定每个连接上PSCache的大小 -->
<property name="poolPreparedStatements" value="true" />
<property name="maxPoolPreparedStatementPerConnectionSize" value="20" />
<!-- 配置监控统计拦截的filters,去掉后监控界面sql无法统计 -->
<property name="filters" value="stat" />
</bean>

新建一个Configuration.java

下面是我自己开源项目cppba-web中hibernate.java的配置信息:

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
package com.cppba.config.hibernate;
import com.alibaba.druid.pool.DruidDataSource;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.orm.hibernate4.HibernateTransactionManager;
import org.springframework.orm.hibernate4.LocalSessionFactoryBean;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import javax.sql.DataSource;
import java.sql.SQLException;
import java.util.Properties;
import static com.cppba.config.ApplicationInitializer.propertySourcesPropertyResolver;
@Configuration
@EnableTransactionManagement
public class HibernateConfiguration {
//datasource
@Bean(initMethod = "init", destroyMethod = "close")
public DataSource dataSource() throws SQLException {
DruidDataSource druidDataSource = new DruidDataSource();
druidDataSource.setUrl(propertySourcesPropertyResolver.getProperty("jdbc.url"));
druidDataSource.setUsername(propertySourcesPropertyResolver
.getProperty("jdbc.user"));
druidDataSource.setPassword(propertySourcesPropertyResolver
.getProperty("jdbc.password"));
druidDataSource.setInitialSize(1);
druidDataSource.setMinIdle(1);
druidDataSource.setMaxActive(20);
druidDataSource.setMaxWait(60000);
druidDataSource.setTimeBetweenEvictionRunsMillis(60000);
druidDataSource.setMinEvictableIdleTimeMillis(300000);
druidDataSource.setValidationQuery("SELECT 'x'");
druidDataSource.setTestWhileIdle(true);
druidDataSource.setTestOnBorrow(false);
druidDataSource.setTestOnReturn(false);
return druidDataSource;
}
//sessionFactory
@Bean
public LocalSessionFactoryBean sessionFactory() throws SQLException {
LocalSessionFactoryBean localSessionFactoryBean = new LocalSessionFactoryBean();
localSessionFactoryBean.setDataSource(this.dataSource());
Properties properties1 = new Properties();
properties1.setProperty("hibernate.dialect", "org.hibernate.dialect.MySQL5Dialect");
properties1.setProperty("hibernate.show_sql", "false");
localSessionFactoryBean.setHibernateProperties(properties1);
localSessionFactoryBean.setPackagesToScan("*");
return localSessionFactoryBean;
}
//txManager事务开启
@Bean
public HibernateTransactionManager txManager() throws SQLException {
HibernateTransactionManager hibernateTransactionManager = new HibernateTransactionManager();
hibernateTransactionManager.setSessionFactory(sessionFactory().getObject());
return hibernateTransactionManager;
}
}

重点提示

xml中bean转成java代码的重点在于:
1.配置类上加上@Configuration
2.bean方法上面加上@Bean