io.github.azige.bbs.config.DataConfiguration.java Source code

Java tutorial

Introduction

Here is the source code for io.github.azige.bbs.config.DataConfiguration.java

Source

/*
 * Copyright 2015 Azige.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package io.github.azige.bbs.config;

import org.apache.tomcat.jdbc.pool.DataSource;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.instrument.classloading.ReflectiveLoadTimeWeaver;
import org.springframework.orm.jpa.JpaTransactionManager;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
import org.springframework.transaction.annotation.EnableTransactionManagement;

/**
 *
 * @author Azige
 */
@Configuration
@EnableJpaRepositories("io.github.azige.bbs.data")
@EnableTransactionManagement
public class DataConfiguration {

    @Bean(destroyMethod = "close")
    public DataSource dataSource() {
        DataSource bean = new DataSource();
        bean.setDriverClassName("org.apache.derby.jdbc.ClientDriver");
        bean.setUrl("jdbc:derby://localhost:1527/bbs");
        bean.setUsername("bbs");
        bean.setPassword("asdasd");
        bean.setMaxActive(8);
        bean.setInitialSize(1);
        return bean;
    }

    @Bean
    public LocalContainerEntityManagerFactoryBean entityManagerFactory() {
        LocalContainerEntityManagerFactoryBean bean = new LocalContainerEntityManagerFactoryBean();
        bean.setDataSource(dataSource());
        bean.setLoadTimeWeaver(new ReflectiveLoadTimeWeaver());
        return bean;
    }

    @Bean
    public JpaTransactionManager transactionManager() {
        JpaTransactionManager bean = new JpaTransactionManager();
        bean.setEntityManagerFactory(entityManagerFactory().getObject());
        return bean;
    }
}