app.App.java Source code

Java tutorial

Introduction

Here is the source code for app.App.java

Source

package app;

import java.util.Arrays;
import static springfox.documentation.builders.PathSelectors.regex;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.slf4j.LoggerFactory;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.domain.EntityScan;
import org.springframework.cache.Cache;
import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.cache.concurrent.ConcurrentMapCache;
import org.springframework.cache.support.SimpleCacheManager;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
import org.springframework.context.annotation.ImportResource;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Component;
import profileManagement.api.UserApplicationService;
import profileManagement.api.RoleApplicationService;
import profileManagement.domain.repository.IRoleRepository;
import profileManagement.domain.repository.IUserRepository;
import profileManagement.domain.repository.IPrivilegeRepository;
import profileManagement.api.PrivilegeApplicationService;
import profileManagement.infra.persistance.repository.UserRepository;
import profileManagement.infra.persistance.repository.RoleRepository;
import profileManagement.infra.persistance.repository.PrivilegeRepository;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
import springfox.documentation.builders.PathSelectors;
import com.google.common.base.Predicates;
import springfox.documentation.builders.RequestHandlerSelectors;

@SpringBootApplication
@Configuration
@ImportResource({ "classpath:applicationContext.xml" })
@EnableAutoConfiguration
@ComponentScan(basePackages = { "profileManagement" })
@EnableJpaRepositories(basePackages = { "profileManagement" })
@EntityScan(basePackages = { "profileManagement" })
@EnableAspectJAutoProxy
@EnableCaching
@EnableSwagger2

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

    @Bean
    IUserRepository UserRepository() {
        return new UserRepository();
    }

    @Bean
    public UserApplicationService UserApplicationService(IUserRepository userRepo, IRoleRepository roleRepo) {
        System.out
                .println("****************************************************************************************"
                        + userRepo + roleRepo);
        return new UserApplicationService(userRepo, roleRepo);
    }

    @Bean
    public RoleApplicationService RoleApplicationService(IRoleRepository roleRepo,
            IPrivilegeRepository privilegeRepo) {
        return new RoleApplicationService(roleRepo, privilegeRepo);
    }

    @Bean
    public PrivilegeApplicationService PrivilegeApplicationService(IPrivilegeRepository privilegeRepo) {
        return new PrivilegeApplicationService(privilegeRepo);
    }

    @Bean
    public Docket newsApi() {
        return new Docket(DocumentationType.SWAGGER_2).groupName("test").apiInfo(apiInfo()).select()
                .apis(RequestHandlerSelectors.any()) //
                .paths(Predicates.not(PathSelectors.regex("/error.*"))).build();
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder().title("Spring REST Sample with Swagger")
                .description("Spring REST Sample with Swagger")
                .termsOfServiceUrl("http://www-03.ibm.com/software/sla/sladb.nsf/sla/bm?Open")
                .contact("Niklas Heidloff").license("Apache License Version 2.0")
                .licenseUrl("https://github.com/IBM-Bluemix/news-aggregator/blob/master/LICENSE").version("2.0")
                .build();
    }

    @Aspect
    @Component
    private class ServiceMonitorConfig {

        public ServiceMonitorConfig() {

        }

        @Around("execution(* profileManagement..*Service.*(..))")
        public Object logServiceAccess(ProceedingJoinPoint pjp) throws Throwable {

            double begin = System.currentTimeMillis();
            Object retVal = pjp.proceed();
            double end = System.currentTimeMillis();

            System.out.println(
                    "****************************************************************************************");
            // LoggerFactory.getLogger(ServiceMonitorConfig.class).info("Completed: " + pjp + " for user : " +  SecurityContextHolder.getContext().getAuthentication().getPrincipal() + " in " + (end - begin) + " ms "); 
            return retVal;
        }

    }
}