Commit bf72e314 authored by Ben Hassine Mohamed's avatar Ben Hassine Mohamed 💬

Spring Boot 2 Guru :

Part 2 Advanced Configuration : section Auto-configuration
Signed-off-by: Ben Hassine Mohamed's avatarm.benhassine <med.ingenieur@gmail.com>
parent b8e02d09
......@@ -19,9 +19,17 @@
</properties>
<dependencies>
<!--previous spring-boot-starter deleted -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>org.hsqldb</groupId>
<artifactId>hsqldb</artifactId>
</dependency>
<dependency>
......@@ -29,6 +37,7 @@
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
......
package com.java.ways.boot.basic;
public class Blog {
private long id;
private String name;
public String getMail() {
return mail;
}
public void setMail(String mail) {
this.mail = mail;
}
private String mail;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Blog(long id, String name, String mail) {
this.id = id;
this.name = name;
this.mail = mail;
}
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
@Override
public String toString() {
return String.format(
"Blog[id=%d, name='%s', mail='%s']",
id, name, mail);
}
}
package com.java.ways.boot.basic;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.jdbc.core.JdbcTemplate;
import java.util.Arrays;
@SpringBootApplication
public class SpringBootBasicApplication {
private static final Logger log = LoggerFactory.getLogger(SpringBootBasicApplication.class);
public static final String QUERY = "SELECT * from T_BLOG";
public static void main(String[] args) {
SpringApplication.run(SpringBootBasicApplication.class, args);
System.out.println("Welcome to this first basic spirng boot project , amazing features are coming soon :) ");
}
ApplicationContext ctx = SpringApplication.run(SpringBootBasicApplication.class, args);
JdbcTemplate jdbcTemplate = ctx.getBean(JdbcTemplate.class);
jdbcTemplate.query(
QUERY,
(rs, rowNum) -> new Blog(rs.getLong("id"), rs.getString("name"), rs.getString("mail"))
).forEach(blog -> log.info(blog.toString()));
}
// This Beans runs on start up
......@@ -24,7 +34,7 @@ public class SpringBootBasicApplication {
public CommandLineRunner commandLineRunner(ApplicationContext ctx) {
return args -> {
System.out.println("Let's discover automatically Spring Boot created Beans :");
System.out.println("Let's discover default Spring Boot created Beans :");
String[] beanNames = ctx.getBeanDefinitionNames();
Arrays.sort(beanNames);
......
# Populate the database with test data
spring.datasource.schema=/db/schema.sql
spring.datasource.data=/db/data.sql
insert into T_BLOG (ID, NAME, MAIL) values ('11', 'JAVA-WAYS.COM', 'med.ingenieur@gmail.com');
insert into T_BLOG (ID, NAME, MAIL) values ('22', 'JAVA', 'java@gmail.com');
insert into T_BLOG (ID, NAME, MAIL) values ('33', 'TUNISIA.COM', 'tunsianWay@gmail.com');
drop table T_BLOG if exists;
create table T_BLOG (ID integer identity primary key, NAME varchar(50) not null, MAIL varchar(50) not null );
......@@ -2,24 +2,16 @@ package com.java.ways.boot.basic;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import static org.junit.Assert.assertNotNull;
@RunWith(SpringRunner.class)
@SpringBootTest
public class SpringBootBasicApplicationTests {
@Autowired
private BasicBean basicBean;
@Test
public void contextLoads() {
}
@Test
public void contextLoads() {
assertNotNull(basicBean);
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment