com.xsb.conf.Application.java Source code

Java tutorial

Introduction

Here is the source code for com.xsb.conf.Application.java

Source

/*
 * Copyright 2012-2013 the original author or authors.
 *
 * 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 com.xsb.conf;

import org.apache.commons.dbcp.BasicDataSource;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.boot.context.web.SpringBootServletInitializer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;

/***
 * ?
 * @author hell
 *
 */
@Configuration
@EnableAutoConfiguration
@EnableConfigurationProperties
@ComponentScan("com.xsb") //???
@EnableTransactionManagement
public class Application extends SpringBootServletInitializer {

    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
        return application.sources(Application.class);
    }

    /***
     * ??
     * @return
     */
    @Bean
    @Primary
    @ConfigurationProperties(prefix = "database", locations = "classpath:dbcp.properties")
    public BasicDataSource database() {
        BasicDataSource basicDataSource = new BasicDataSource();
        /*basicDataSource.setUrl("jdbc:mysql://localhost:3306/xsb?zeroDateTimeBehavior=convertToNull&allowMultiQueries=true&characterEncoding=UTF-8");
        basicDataSource.setDriverClassName("org.gjt.mm.mysql.Driver");
        basicDataSource.setUsername("root");
        basicDataSource.setPassword("123456");
        basicDataSource.setMaxActive(100);
        basicDataSource.setMaxIdle(30);
        basicDataSource.setMaxWait(30);
        basicDataSource.setMinIdle(10);
        basicDataSource.setValidationQuery("select 1 from dual");
        basicDataSource.setTestOnBorrow(true);*/
        return basicDataSource;
    }

    @Bean
    public PlatformTransactionManager txManager() {
        return new DataSourceTransactionManager(database());
    }

    @Bean
    public SqlSessionFactory sqlSessionFactory() throws Exception {
        SqlSessionFactoryBean ssfb = new SqlSessionFactoryBean();

        ssfb.setDataSource(database());

        PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();

        ssfb.setMapperLocations(resolver.getResources("classpath:com/xsb/dao/mapper/*.xml"));

        return ssfb.getObject();
    }

    public static void main(String[] args) throws Exception {
        SpringApplication.run(Application.class, args);
    }

}