ua.com.codefire.springbootexample.Application.java Source code

Java tutorial

Introduction

Here is the source code for ua.com.codefire.springbootexample.Application.java

Source

/*
 * Copyright (C) 2016 CodeFireUA <edu@codefire.com.ua>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package ua.com.codefire.springbootexample;

import com.mysql.jdbc.Driver;
import javax.sql.DataSource;
import nz.net.ultraq.thymeleaf.LayoutDialect;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.jdbc.DataSourceBuilder;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.orm.jpa.EntityManagerFactoryBuilder;
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.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
import org.springframework.web.servlet.ViewResolver;
import org.springframework.web.servlet.config.annotation.DefaultServletHandlerConfigurer;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import org.thymeleaf.spring4.SpringTemplateEngine;
import org.thymeleaf.spring4.view.ThymeleafViewResolver;
import org.thymeleaf.templateresolver.ServletContextTemplateResolver;
import org.thymeleaf.templateresolver.TemplateResolver;

/**
 *
 * @author CodeFireUA <edu@codefire.com.ua>
 */
@EnableJpaRepositories(basePackages = "ua.com.codefire.springbootexample.db.repo")
@ComponentScan
@EnableWebMvc
@Configuration
@EnableAutoConfiguration
public class Application extends WebMvcConfigurerAdapter {

    public static void main(String[] args) throws Exception {
        SpringApplication.run(Application.class, args);
        System.out.println("Server started at:\nhttp://localhost:8080/");
    }

    // WEB MVC
    @Override
    public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
        configurer.enable();
    }

    //    @Bean
    //    public ViewResolver getViewResolver() {
    //        System.out.println("::: GETTING VR");
    //        
    //        
    //        
    //        return viewResolver;
    //    }

    // JPA
    @Bean
    @Primary
    @ConfigurationProperties(prefix = "application.properties")
    public DataSource primaryDataSource() {
        return DataSourceBuilder.create().driverClassName(Driver.class.getName())
                .url("jdbc:mysql://192.168.1.99:3306/test").username("student").password("12345").build();
    }

    @Bean
    public LocalContainerEntityManagerFactoryBean entityManagerFactory(EntityManagerFactoryBuilder builder) {
        return builder.dataSource(primaryDataSource()).persistenceUnit("MainPU")
                .packages("ua.com.codefire.springbootexample.db.entity").build();
    }
}