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

前言

作为一个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代码:


        
        
        
        

        
        
        
        

        
        

        
        

        
        

        
        
        
        

        
        
        

        
        
    

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)