Commit 0609b93a authored by Alexander Kosarev's avatar Alexander Kosarev 🇦🇺

project init

parent 25a84c3c
/target/
!.mvn/wrapper/maven-wrapper.jar
### STS ###
.apt_generated
.classpath
.factorypath
.project
.settings
.springBeans
.sts4-cache
### IntelliJ IDEA ###
.idea
*.iws
*.iml
*.ipr
### NetBeans ###
/nbproject/private/
/build/
/nbbuild/
/dist/
/nbdist/
/.nb-gradle/
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.1.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>name.alexkosarev.tutorial</groupId>
<artifactId>tutorial-spring-functional-bean-registration</artifactId>
<version>1.0.0</version>
<name>tutorial-spring-functional-bean-registration</name>
<description/>
<properties>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
<java.version>11</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-activemq</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
package name.alexkosarev.tutorial;
import name.alexkosarev.tutorial.note.NoteBeansContextInitializer;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
@SpringBootApplication
public class SpringFunctionalBeanRegistrationApplication {
public static void main(String[] args) {
new SpringApplicationBuilder(SpringFunctionalBeanRegistrationApplication.class)
.initializers(new NoteBeansContextInitializer())
.build()
.run();
}
}
package name.alexkosarev.tutorial.note;
import lombok.RequiredArgsConstructor;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
@RequiredArgsConstructor
public class DefaultNoteService implements NoteService {
private final NoteCrudRepository noteCrudRepository;
@Override
public List<Note> findAll() {
return this.noteCrudRepository.findAll();
}
@Override
public Note findOneById(String id) {
return this.noteCrudRepository.findById(id).orElseThrow();
}
@Override
public Note create(String title, String content) {
return this.noteCrudRepository.save(new Note(UUID.randomUUID().toString(), title, content));
}
}
package name.alexkosarev.tutorial.note;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import javax.persistence.Entity;
import javax.persistence.Id;
@Data
@NoArgsConstructor
@AllArgsConstructor
@Builder
@Entity
public class Note {
@Id
private String id;
private String title;
private String content;
}
package name.alexkosarev.tutorial.note;
import name.alexkosarev.tutorial.note.web.NoteRestController;
import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory;
import org.springframework.context.ApplicationContextInitializer;
import org.springframework.context.support.GenericApplicationContext;
public class NoteBeansContextInitializer implements ApplicationContextInitializer<GenericApplicationContext> {
@Override
public void initialize(GenericApplicationContext applicationContext) {
applicationContext.registerBean(TomcatServletWebServerFactory.class);
applicationContext.registerBean(NoteService.class,
() -> new DefaultNoteService(applicationContext.getBean(NoteCrudRepository.class)));
applicationContext.registerBean(NoteRestController.class);
}
}
package name.alexkosarev.tutorial.note;
import org.springframework.data.repository.CrudRepository;
import java.util.List;
public interface NoteCrudRepository extends CrudRepository<Note, String> {
List<Note> findAll();
}
package name.alexkosarev.tutorial.note;
import java.util.List;
public interface NoteService {
List<Note> findAll();
Note findOneById(String id);
Note create(String title, String content);
}
package name.alexkosarev.tutorial.note.web;
import lombok.RequiredArgsConstructor;
import name.alexkosarev.tutorial.note.Note;
import name.alexkosarev.tutorial.note.NoteService;
import name.alexkosarev.tutorial.note.web.request.CreateNoteRequest;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import java.util.List;
import java.util.NoSuchElementException;
@RequestMapping("notes")
@RequiredArgsConstructor
public class NoteRestController {
private final NoteService noteService;
@GetMapping
public ResponseEntity<List<Note>> findAll() {
return ResponseEntity.ok(this.noteService.findAll());
}
@GetMapping("{id}")
public ResponseEntity<Note> findOneById(@PathVariable String id) {
try {
return ResponseEntity.ok(this.noteService.findOneById(id));
} catch (NoSuchElementException e) {
return ResponseEntity.notFound().build();
}
}
@PostMapping
public ResponseEntity<Note> create(@RequestBody CreateNoteRequest request) {
return ResponseEntity.ok(this.noteService.create(request.getTitle(), request.getContent()));
}
}
package name.alexkosarev.tutorial.note.web.request;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@NoArgsConstructor
@AllArgsConstructor
@Builder
public class CreateNoteRequest {
private String title;
private String content;
}
org.springframework.context.ApplicationContextInitializer=name.alexkosarev.tutorial.note.NoteBeansContextInitializer
package name.alexkosarev.tutorial;
import name.alexkosarev.tutorial.note.NoteBeansContextInitializer;
import name.alexkosarev.tutorial.note.NoteService;
import org.junit.Assert;
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.ContextConfiguration;
import org.springframework.test.context.junit4.SpringRunner;
@RunWith(SpringRunner.class)
@SpringBootTest
@ContextConfiguration(initializers = NoteBeansContextInitializer.class)
public class SpringFunctionalBeanRegistrationApplicationTests {
@Autowired
private NoteService noteService;
@Test
public void contextLoads() {
Assert.assertNotNull(this.noteService);
}
}
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