美文网首页JavaEEjava code资源贴
spring进阶教程(一):减少spring的XML

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

作者: 大黄蜂coder | 来源:发表于2016-09-10 23:44 被阅读313次

    前言

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

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

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

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

    <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>
    

    2.新建一个Configuration.java

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

    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();
            //正式环境(修改jdbc.properties中jdbc.environment.real属性)
            if(propertySourcesPropertyResolver.getProperty("jdbc.environment.real").equals("true")){
                druidDataSource.setUrl(propertySourcesPropertyResolver.getProperty("jdbc.real.url"));
                druidDataSource.setUsername(propertySourcesPropertyResolver
                        .getProperty("jdbc.real.user"));
                druidDataSource.setPassword(propertySourcesPropertyResolver
                        .getProperty("jdbc.real.password"));
            }else{
                //测试环境
                druidDataSource.setUrl(propertySourcesPropertyResolver.getProperty("jdbc.test.url"));
                druidDataSource.setUsername(propertySourcesPropertyResolver
                        .getProperty("jdbc.test.user"));
                druidDataSource.setPassword(propertySourcesPropertyResolver
                        .getProperty("jdbc.test.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;
        }
    }
    

    3.重点提示

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

    相关文章

      网友评论

        本文标题:spring进阶教程(一):减少spring的XML

        本文链接:https://www.haomeiwen.com/subject/ququettx.html