cn.net.withub.demo.bootsec.hello.config.SpringDataConfig.java Source code

Java tutorial

Introduction

Here is the source code for cn.net.withub.demo.bootsec.hello.config.SpringDataConfig.java

Source

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package cn.net.withub.demo.bootsec.hello.config;

import java.util.Properties;
import javax.sql.DataSource;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.jdbc.datasource.DriverManagerDataSource;
import org.springframework.orm.hibernate4.HibernateTransactionManager;
import org.springframework.orm.hibernate4.LocalSessionFactoryBean;
import org.springframework.orm.hibernate4.support.OpenSessionInViewFilter;

/**
 * ???
 *
 * @author Diluka
 */
@Configuration
@PropertySource("classpath:jdbc.properties") //
public class SpringDataConfig {

    @Value("${jdbc.url}")
    private String url;
    @Value("${jdbc.username}")
    private String userName;
    @Value("${jdbc.password}")
    private String password;
    @Value("${jdbc.driverClassName}")
    private String driverClassName;

    /**
     * ??
     *
     * @return
     */
    @Bean
    public DriverManagerDataSource dataSource() {
        DriverManagerDataSource dataSource = new DriverManagerDataSource(url, userName, password);
        dataSource.setDriverClassName(driverClassName);
        return dataSource;
    }

    /**
     * OpenSessionInViewFilter
     *
     * @return
     */
    @Bean
    public OpenSessionInViewFilter openSessionInViewFilter() {
        OpenSessionInViewFilter openSessionInViewFilter = new OpenSessionInViewFilter();
        return openSessionInViewFilter;
    }

    /**
     * ?session
     *
     * @param dataSource ???
     * @return
     */
    @Bean
    @Autowired
    public LocalSessionFactoryBean sessionFactory(DataSource dataSource) {
        LocalSessionFactoryBean sessionFactory = new LocalSessionFactoryBean();

        sessionFactory.setDataSource(dataSource); //??
        sessionFactory.setPackagesToScan("cn.net.withub.demo.bootsec.hello.entity"); //???
        sessionFactory.setHibernateProperties(hibernateProperties()); //hibernate

        return sessionFactory;
    }

    /**
     * ??
     *
     * @param sessionFactory
     * @return
     */
    @Bean
    @Autowired
    public HibernateTransactionManager transactionManager(SessionFactory sessionFactory) {
        return new HibernateTransactionManager(sessionFactory);
    }

    /**
     * hibernate
     *
     * @return
     */
    Properties hibernateProperties() {
        return new Properties() {
            {
                setProperty("hibernate.dialect", "org.hibernate.dialect.MySQLDialect");
                setProperty("hibernate.show_sql", "true");
                //setProperty("hibernate.format_sql", "true");
            }
        };
    }
}