Spring Boot Not Web

Create problem with Spring Boot(without Web)

Let’s make a general problem with Spring Boot
I use following

  • Spring Boot(For properties and logging)
  • spring-context
  • spring-jdbc
  • spring

Environment

  • MySQL
  • IntelliJ

Create Spring Boot Project with IDEA

Create project and build.gradle

mkdir SpringProgram
cd SpringProgram
mkdir -p src/main/java
mkdir -p src/main/resources
mkdir -p src/main/test
touch build.gradle

Prepare build.gradle

buildscript {
    repositories {
        mavenCentral()
    }

    dependencies {
        classpath(
                'org.springframework.boot:spring-boot-gradle-plugin:1.5.2.RELEASE'
        )
    }
}

apply plugin: "java"
apply plugin: "idea"
apply plugin: 'spring-boot'


idea {
    module {
        inheritOutputDirs = false
        outputDir = file("$buildDir/classes/main/")
    }
}

jar {
    baseName = "SpringProject"
    version =  "0.0.1-SNAPSHOT"
}

repositories {
    mavenCentral()
}

dependencies {
    compile('org.springframework:spring:4.3.7.RELEASE')
    compile('org.springframework:spring-jdbc:4.3.7.RELEASE')
    compile('org.springframework:spring-context:4.3.7.RELEASE')
    compile('org.springframework.boot:spring-boot-starter')
    compile('mysql:mysql-connector-java:5.1.40')
}

Resolve Dependency

gradle idea

Download dependencies and build.

Open this folder from IntelliJ

Sample

This is Sample Application.
Start from main and try to get data from Database.

Application.java

@SpringBootApplication
@ComponentScan("com.atmarkplant.springprogram")
public class Application {

    public static void main(String[] args) {
        //ApplicationContext context = new AnnotationConfigApplicationContext(Application.class)
                ;
        ConfigurableApplicationContext context = SpringApplication.run(Application.class, args);
        MessagePrinter printer = context.getBean(MessagePrinter.class);

        DatabaseManagement databaseManagement = context.getBean(DatabaseManagement.class);
        databaseManagement.printMessage();

        printer.printMessage();
    }
}

resources/application.yml

springprogram:
  database:
    url: jdbc:mysql://localhost:3306/test
    username: root
    password: password

DataSourceConfiguration.java

@Configuration
@EnableTransactionManagement
public class DataSourceConfiguration {

    @Value("${springprogram.database.url}")
    private String url;

    @Value("${springprogram.database.username}")
    private String username;

    @Value("${springprogram.database.password}")
    private String password;


    @Bean
    public DriverManagerDataSource dataSource() {
        DriverManagerDataSource driverManagerDataSource = new DriverManagerDataSource();
        driverManagerDataSource.setDriverClassName(com.mysql.jdbc.Driver.class.getName());
        driverManagerDataSource.setUrl(url);
        driverManagerDataSource.setUsername(username);
        driverManagerDataSource.setPassword(password);
        return driverManagerDataSource;
    }

    @Bean
    public DataSourceTransactionManager transactionManager() {
        return new DataSourceTransactionManager(dataSource());
    }

    @Bean
    public JdbcTemplate jdbcTemplate() {
        return new JdbcTemplate(dataSource());
    }
}

MessagePrinter.java

@Component
public class MessagePrinter {

    public void printMessage() {
        System.out.println("Hello World!");
    }
}

Person.java

public class Person {

    private Integer id;

    private String name;

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}