From fff769ed9dadbb2abf2c028bae08e1ca254212e6 Mon Sep 17 00:00:00 2001 From: Julius Krah Date: Wed, 6 Jul 2016 16:09:24 +0000 Subject: [PATCH] added source for 'A Guide to Spring Boot in Eclipse' --- spring-boot/.gitignore | 3 + spring-boot/pom.xml | 142 ++++++++++-------- .../org/baeldung/boot/DemoApplication.java | 13 ++ .../java/org/baeldung/boot/model/Foo.java | 40 +++++ .../boot/repository/FooRepository.java | 8 + .../src/main/resources/demo.properties | 6 + .../baeldung/boot/DemoApplicationTests.java | 18 +++ .../boot/repository/FooRepositoryTest.java | 33 ++++ 8 files changed, 204 insertions(+), 59 deletions(-) create mode 100644 spring-boot/src/main/java/org/baeldung/boot/DemoApplication.java create mode 100644 spring-boot/src/main/java/org/baeldung/boot/model/Foo.java create mode 100644 spring-boot/src/main/java/org/baeldung/boot/repository/FooRepository.java create mode 100644 spring-boot/src/main/resources/demo.properties create mode 100644 spring-boot/src/test/java/org/baeldung/boot/DemoApplicationTests.java create mode 100644 spring-boot/src/test/java/org/baeldung/boot/repository/FooRepositoryTest.java diff --git a/spring-boot/.gitignore b/spring-boot/.gitignore index 24d64373c4..e26d6af438 100644 --- a/spring-boot/.gitignore +++ b/spring-boot/.gitignore @@ -1 +1,4 @@ /target/ +.settings/ +.classpath +.project diff --git a/spring-boot/pom.xml b/spring-boot/pom.xml index d0a66197bf..ebd920e395 100644 --- a/spring-boot/pom.xml +++ b/spring-boot/pom.xml @@ -1,73 +1,97 @@ - - 4.0.0 - com.baeldung - spring-boot - 0.0.1-SNAPSHOT - war - Spring Boot Actuator - This is simple boot application for Spring boot actuator test + + 4.0.0 + com.baeldung + spring-boot + 0.0.1-SNAPSHOT + war + Spring Boot Actuator + This is simple boot application for Spring boot actuator test - - - org.springframework.boot - spring-boot-starter-parent - 1.3.3.RELEASE - + + + org.springframework.boot + spring-boot-starter-parent + 1.3.6.RELEASE + + - - - org.springframework.boot - spring-boot-starter-web - + + UTF-8 + 1.8 + + - - org.springframework.boot - spring-boot-starter-actuator - + + + org.springframework.boot + spring-boot-starter-web + - - org.springframework.boot - spring-boot-starter-security - + + org.springframework.boot + spring-boot-starter-data-jpa + - - io.dropwizard.metrics - metrics-core - - + + org.springframework.boot + spring-boot-starter-actuator + - - spring-boot - - - src/main/resources - true - - + + org.springframework.boot + spring-boot-starter-security + - + + io.dropwizard.metrics + metrics-core + - - org.springframework.boot - spring-boot-maven-plugin - + + com.h2database + h2 + - - org.apache.maven.plugins - maven-compiler-plugin - - 1.8 - 1.8 - - + + org.springframework.boot + spring-boot-starter-test + test + + - - org.apache.maven.plugins - maven-war-plugin - + + spring-boot + + + src/main/resources + true + + - + - + + org.springframework.boot + spring-boot-maven-plugin + + + + org.apache.maven.plugins + maven-compiler-plugin + + 1.8 + 1.8 + + + + + org.apache.maven.plugins + maven-war-plugin + + + + + \ No newline at end of file diff --git a/spring-boot/src/main/java/org/baeldung/boot/DemoApplication.java b/spring-boot/src/main/java/org/baeldung/boot/DemoApplication.java new file mode 100644 index 0000000000..69e78c3fee --- /dev/null +++ b/spring-boot/src/main/java/org/baeldung/boot/DemoApplication.java @@ -0,0 +1,13 @@ +package org.baeldung.boot; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +@SpringBootApplication +public class DemoApplication { + + public static void main(String[] args) { + System.setProperty("spring.config.name", "demo"); + SpringApplication.run(DemoApplication.class, args); + } +} diff --git a/spring-boot/src/main/java/org/baeldung/boot/model/Foo.java b/spring-boot/src/main/java/org/baeldung/boot/model/Foo.java new file mode 100644 index 0000000000..0db0be4db1 --- /dev/null +++ b/spring-boot/src/main/java/org/baeldung/boot/model/Foo.java @@ -0,0 +1,40 @@ +package org.baeldung.boot.model; + +import java.io.Serializable; + +import javax.persistence.Entity; +import javax.persistence.GeneratedValue; +import javax.persistence.Id; + +@Entity +public class Foo implements Serializable { + private static final long serialVersionUID = 1L; + @Id + @GeneratedValue + private Integer id; + private String name; + + public Foo() { + } + + public Foo(String name) { + this.name = 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; + } + +} diff --git a/spring-boot/src/main/java/org/baeldung/boot/repository/FooRepository.java b/spring-boot/src/main/java/org/baeldung/boot/repository/FooRepository.java new file mode 100644 index 0000000000..de8ce8196d --- /dev/null +++ b/spring-boot/src/main/java/org/baeldung/boot/repository/FooRepository.java @@ -0,0 +1,8 @@ +package org.baeldung.boot.repository; + +import org.baeldung.boot.model.Foo; +import org.springframework.data.jpa.repository.JpaRepository; + +public interface FooRepository extends JpaRepository { + public Foo findByName(String name); +} diff --git a/spring-boot/src/main/resources/demo.properties b/spring-boot/src/main/resources/demo.properties new file mode 100644 index 0000000000..649b64f59b --- /dev/null +++ b/spring-boot/src/main/resources/demo.properties @@ -0,0 +1,6 @@ +spring.output.ansi.enabled=never +server.port=7070 + +# Security +security.user.name=admin +security.user.password=password \ No newline at end of file diff --git a/spring-boot/src/test/java/org/baeldung/boot/DemoApplicationTests.java b/spring-boot/src/test/java/org/baeldung/boot/DemoApplicationTests.java new file mode 100644 index 0000000000..4e04334238 --- /dev/null +++ b/spring-boot/src/test/java/org/baeldung/boot/DemoApplicationTests.java @@ -0,0 +1,18 @@ +package org.baeldung.boot; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.boot.test.SpringApplicationConfiguration; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; +import org.springframework.test.context.web.WebAppConfiguration; + +@RunWith(SpringJUnit4ClassRunner.class) +@SpringApplicationConfiguration(classes = DemoApplication.class) +@WebAppConfiguration +public class DemoApplicationTests { + + @Test + public void contextLoads() { + } + +} diff --git a/spring-boot/src/test/java/org/baeldung/boot/repository/FooRepositoryTest.java b/spring-boot/src/test/java/org/baeldung/boot/repository/FooRepositoryTest.java new file mode 100644 index 0000000000..e357800e11 --- /dev/null +++ b/spring-boot/src/test/java/org/baeldung/boot/repository/FooRepositoryTest.java @@ -0,0 +1,33 @@ +package org.baeldung.boot.repository; + +import static org.junit.Assert.assertThat; + +import org.baeldung.boot.DemoApplicationTests; +import org.baeldung.boot.model.Foo; + +import static org.hamcrest.Matchers.notNullValue; +import static org.hamcrest.Matchers.is; + +import org.junit.Before; +import org.junit.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.transaction.annotation.Transactional; + +@Transactional +public class FooRepositoryTest extends DemoApplicationTests { + @Autowired + private FooRepository fooRepository; + + @Before + public void setUp() { + fooRepository.save(new Foo("Foo")); + fooRepository.save(new Foo("Bar")); + } + + @Test + public void testFindByName() { + Foo foo = fooRepository.findByName("Bar"); + assertThat(foo, notNullValue()); + assertThat(foo.getId(), is(2)); + } +}