Merge pull request #8633 from albanoj2/BAEL-3744
BAEL-3744: Added examples of how design patterns are used in the Spring framework
This commit is contained in:
commit
d18fe0535d
|
@ -1,72 +1,88 @@
|
||||||
<?xml version="1.0" encoding="UTF-8"?>
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
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">
|
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>
|
<modelVersion>4.0.0</modelVersion>
|
||||||
<artifactId>spring-core-3</artifactId>
|
<artifactId>spring-core-3</artifactId>
|
||||||
<name>spring-core-3</name>
|
<name>spring-core-3</name>
|
||||||
|
|
||||||
<parent>
|
<parent>
|
||||||
<groupId>com.baeldung</groupId>
|
<groupId>com.baeldung</groupId>
|
||||||
<artifactId>parent-spring-5</artifactId>
|
<artifactId>parent-spring-5</artifactId>
|
||||||
<version>0.0.1-SNAPSHOT</version>
|
<version>0.0.1-SNAPSHOT</version>
|
||||||
<relativePath>../parent-spring-5</relativePath>
|
<relativePath>../parent-spring-5</relativePath>
|
||||||
</parent>
|
</parent>
|
||||||
|
|
||||||
<dependencies>
|
<dependencies>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.springframework</groupId>
|
<groupId>org.springframework</groupId>
|
||||||
<artifactId>spring-beans</artifactId>
|
<artifactId>spring-beans</artifactId>
|
||||||
<version>${spring.version}</version>
|
<version>${spring.version}</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.springframework</groupId>
|
<groupId>org.springframework</groupId>
|
||||||
<artifactId>spring-context</artifactId>
|
<artifactId>spring-tx</artifactId>
|
||||||
<version>${spring.version}</version>
|
<version>${spring.version}</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.springframework</groupId>
|
<groupId>org.springframework</groupId>
|
||||||
<artifactId>spring-core</artifactId>
|
<artifactId>spring-jdbc</artifactId>
|
||||||
<version>${spring.version}</version>
|
<version>${spring.version}</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>javax.annotation</groupId>
|
<groupId>org.springframework.boot</groupId>
|
||||||
<artifactId>javax.annotation-api</artifactId>
|
<artifactId>spring-boot-starter-web</artifactId>
|
||||||
<version>${annotation-api.version}</version>
|
<version>${spring.boot.version}</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.springframework</groupId>
|
<groupId>org.springframework</groupId>
|
||||||
<artifactId>spring-test</artifactId>
|
<artifactId>spring-context</artifactId>
|
||||||
<version>${spring.version}</version>
|
<version>${spring.version}</version>
|
||||||
<scope>test</scope>
|
</dependency>
|
||||||
</dependency>
|
<dependency>
|
||||||
<dependency>
|
<groupId>org.springframework</groupId>
|
||||||
<groupId>org.junit.jupiter</groupId>
|
<artifactId>spring-core</artifactId>
|
||||||
<artifactId>junit-jupiter-engine</artifactId>
|
<version>${spring.version}</version>
|
||||||
<version>${junit-jupiter.version}</version>
|
</dependency>
|
||||||
<scope>test</scope>
|
<dependency>
|
||||||
</dependency>
|
<groupId>javax.annotation</groupId>
|
||||||
<dependency>
|
<artifactId>javax.annotation-api</artifactId>
|
||||||
<groupId>org.junit.jupiter</groupId>
|
<version>${annotation-api.version}</version>
|
||||||
<artifactId>junit-jupiter-api</artifactId>
|
</dependency>
|
||||||
<version>${junit-jupiter.version}</version>
|
<dependency>
|
||||||
<scope>test</scope>
|
<groupId>org.springframework</groupId>
|
||||||
</dependency>
|
<artifactId>spring-test</artifactId>
|
||||||
</dependencies>
|
<version>${spring.version}</version>
|
||||||
|
<scope>test</scope>
|
||||||
<build>
|
</dependency>
|
||||||
<plugins>
|
<dependency>
|
||||||
<plugin>
|
<groupId>org.junit.jupiter</groupId>
|
||||||
<groupId>org.apache.maven.plugins</groupId>
|
<artifactId>junit-jupiter-engine</artifactId>
|
||||||
<artifactId>maven-surefire-plugin</artifactId>
|
<version>${junit-jupiter.version}</version>
|
||||||
<version>${maven.surefire.version}</version>
|
<scope>test</scope>
|
||||||
</plugin>
|
</dependency>
|
||||||
</plugins>
|
<dependency>
|
||||||
</build>
|
<groupId>org.junit.jupiter</groupId>
|
||||||
|
<artifactId>junit-jupiter-api</artifactId>
|
||||||
<properties>
|
<version>${junit-jupiter.version}</version>
|
||||||
<maven.surefire.version>2.22.1</maven.surefire.version>
|
<scope>test</scope>
|
||||||
<annotation-api.version>1.3.2</annotation-api.version>
|
</dependency>
|
||||||
</properties>
|
</dependencies>
|
||||||
|
|
||||||
|
<build>
|
||||||
|
<plugins>
|
||||||
|
<plugin>
|
||||||
|
<groupId>org.apache.maven.plugins</groupId>
|
||||||
|
<artifactId>maven-surefire-plugin</artifactId>
|
||||||
|
<version>${maven.surefire.version}</version>
|
||||||
|
</plugin>
|
||||||
|
</plugins>
|
||||||
|
</build>
|
||||||
|
|
||||||
|
<properties>
|
||||||
|
<maven.surefire.version>2.22.1</maven.surefire.version>
|
||||||
|
<annotation-api.version>1.3.2</annotation-api.version>
|
||||||
|
<spring.boot.version>2.2.2.RELEASE</spring.boot.version>
|
||||||
|
</properties>
|
||||||
|
|
||||||
</project>
|
</project>
|
|
@ -0,0 +1,19 @@
|
||||||
|
package com.baeldung.spring.patterns.factory;
|
||||||
|
|
||||||
|
import org.springframework.context.ApplicationContext;
|
||||||
|
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
|
||||||
|
|
||||||
|
public class Application {
|
||||||
|
|
||||||
|
@SuppressWarnings("resource")
|
||||||
|
public static void main(String[] args) {
|
||||||
|
|
||||||
|
ApplicationContext context = new AnnotationConfigApplicationContext(ApplicationConfig.class);
|
||||||
|
|
||||||
|
Foo foo = context.getBean(Foo.class);
|
||||||
|
Bar bar = context.getBean(Bar.class, "Some name");
|
||||||
|
|
||||||
|
System.out.println(foo);
|
||||||
|
System.out.println("Bar's name: " + bar.getName());
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,9 @@
|
||||||
|
package com.baeldung.spring.patterns.factory;
|
||||||
|
|
||||||
|
import org.springframework.context.annotation.ComponentScan;
|
||||||
|
import org.springframework.context.annotation.Configuration;
|
||||||
|
|
||||||
|
@Configuration
|
||||||
|
@ComponentScan(basePackageClasses = ApplicationConfig.class)
|
||||||
|
public class ApplicationConfig {
|
||||||
|
}
|
|
@ -0,0 +1,20 @@
|
||||||
|
package com.baeldung.spring.patterns.factory;
|
||||||
|
|
||||||
|
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
|
||||||
|
import org.springframework.context.annotation.Scope;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
|
@Component
|
||||||
|
@Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)
|
||||||
|
public class Bar {
|
||||||
|
|
||||||
|
private String name;
|
||||||
|
|
||||||
|
public Bar(String name) {
|
||||||
|
this.name = name;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getName() {
|
||||||
|
return name;
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,7 @@
|
||||||
|
package com.baeldung.spring.patterns.factory;
|
||||||
|
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
|
@Component
|
||||||
|
public class Foo {
|
||||||
|
}
|
|
@ -0,0 +1,12 @@
|
||||||
|
package com.baeldung.spring.patterns.proxy;
|
||||||
|
|
||||||
|
import org.springframework.boot.SpringApplication;
|
||||||
|
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||||
|
|
||||||
|
@SpringBootApplication
|
||||||
|
public class Application {
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
SpringApplication.run(Application.class, args);
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,18 @@
|
||||||
|
package com.baeldung.spring.patterns.proxy;
|
||||||
|
|
||||||
|
public class Book {
|
||||||
|
|
||||||
|
private String author;
|
||||||
|
|
||||||
|
public Book(String author) {
|
||||||
|
this.author = author;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getAuthor() {
|
||||||
|
return author;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setAuthor(String author) {
|
||||||
|
this.author = author;
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,18 @@
|
||||||
|
package com.baeldung.spring.patterns.proxy;
|
||||||
|
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.web.bind.annotation.PostMapping;
|
||||||
|
import org.springframework.web.bind.annotation.RequestParam;
|
||||||
|
import org.springframework.web.bind.annotation.RestController;
|
||||||
|
|
||||||
|
@RestController
|
||||||
|
public class BookController {
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private BookManager manager;
|
||||||
|
|
||||||
|
@PostMapping("/book")
|
||||||
|
public Book create(@RequestParam String author) {
|
||||||
|
return manager.create(author);
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,18 @@
|
||||||
|
package com.baeldung.spring.patterns.proxy;
|
||||||
|
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.stereotype.Service;
|
||||||
|
import org.springframework.transaction.annotation.Transactional;
|
||||||
|
|
||||||
|
@Service
|
||||||
|
public class BookManager {
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private BookRepository repository;
|
||||||
|
|
||||||
|
@Transactional
|
||||||
|
public Book create(String author) {
|
||||||
|
System.out.println(repository.getClass().getName());
|
||||||
|
return repository.create(author);
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,11 @@
|
||||||
|
package com.baeldung.spring.patterns.proxy;
|
||||||
|
|
||||||
|
import org.springframework.stereotype.Repository;
|
||||||
|
|
||||||
|
@Repository
|
||||||
|
public class BookRepository {
|
||||||
|
|
||||||
|
public Book create(String author) {
|
||||||
|
return new Book(author);
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,12 @@
|
||||||
|
package com.baeldung.spring.patterns.singleton;
|
||||||
|
|
||||||
|
import org.springframework.boot.SpringApplication;
|
||||||
|
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||||
|
|
||||||
|
@SpringBootApplication
|
||||||
|
public class Application {
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
SpringApplication.run(Application.class, args);
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,4 @@
|
||||||
|
package com.baeldung.spring.patterns.singleton;
|
||||||
|
|
||||||
|
public class Book {
|
||||||
|
}
|
|
@ -0,0 +1,19 @@
|
||||||
|
package com.baeldung.spring.patterns.singleton;
|
||||||
|
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.web.bind.annotation.GetMapping;
|
||||||
|
import org.springframework.web.bind.annotation.PathVariable;
|
||||||
|
import org.springframework.web.bind.annotation.RestController;
|
||||||
|
|
||||||
|
@RestController
|
||||||
|
public class BookController {
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private BookRepository repository;
|
||||||
|
|
||||||
|
@GetMapping("/book/{id}")
|
||||||
|
public Book findById(@PathVariable long id) {
|
||||||
|
System.out.println(repository);
|
||||||
|
return repository.findById(id).get();
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,17 @@
|
||||||
|
package com.baeldung.spring.patterns.singleton;
|
||||||
|
|
||||||
|
import java.util.Optional;
|
||||||
|
|
||||||
|
import org.springframework.stereotype.Repository;
|
||||||
|
|
||||||
|
@Repository
|
||||||
|
public class BookRepository {
|
||||||
|
|
||||||
|
public long count() {
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Optional<Book> findById(long id) {
|
||||||
|
return Optional.of(new Book());
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,18 @@
|
||||||
|
package com.baeldung.spring.patterns.singleton;
|
||||||
|
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.web.bind.annotation.GetMapping;
|
||||||
|
import org.springframework.web.bind.annotation.RestController;
|
||||||
|
|
||||||
|
@RestController
|
||||||
|
public class LibraryController {
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private BookRepository repository;
|
||||||
|
|
||||||
|
@GetMapping("/count")
|
||||||
|
public Long findCount() {
|
||||||
|
System.out.println(repository);
|
||||||
|
return repository.count();
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,32 @@
|
||||||
|
package com.baeldung.spring.patterns.template;
|
||||||
|
|
||||||
|
public class Book {
|
||||||
|
|
||||||
|
private long id;
|
||||||
|
private String title;
|
||||||
|
private String author;
|
||||||
|
|
||||||
|
public long getId() {
|
||||||
|
return id;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setId(long id) {
|
||||||
|
this.id = id;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getTitle() {
|
||||||
|
return title;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setTitle(String title) {
|
||||||
|
this.title = title;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getAuthor() {
|
||||||
|
return author;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setAuthor(String author) {
|
||||||
|
this.author = author;
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,21 @@
|
||||||
|
package com.baeldung.spring.patterns.template;
|
||||||
|
|
||||||
|
import java.sql.ResultSet;
|
||||||
|
import java.sql.SQLException;
|
||||||
|
|
||||||
|
import org.springframework.jdbc.core.RowMapper;
|
||||||
|
|
||||||
|
public class BookRowMapper implements RowMapper<Book> {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Book mapRow(ResultSet rs, int rowNum) throws SQLException {
|
||||||
|
|
||||||
|
Book book = new Book();
|
||||||
|
|
||||||
|
book.setId(rs.getLong("id"));
|
||||||
|
book.setTitle(rs.getString("title"));
|
||||||
|
book.setAuthor(rs.getString("author"));
|
||||||
|
|
||||||
|
return book;
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,36 @@
|
||||||
|
package com.baeldung.spring.patterns.factory;
|
||||||
|
|
||||||
|
import static org.hamcrest.CoreMatchers.is;
|
||||||
|
import static org.hamcrest.MatcherAssert.assertThat;
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertNotNull;
|
||||||
|
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
import org.springframework.context.ApplicationContext;
|
||||||
|
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
|
||||||
|
|
||||||
|
public class AnnotationConfigApplicationContextUnitTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@SuppressWarnings("resource")
|
||||||
|
public void whenGetSimpleBean_thenReturnConstructedBean() {
|
||||||
|
|
||||||
|
ApplicationContext context = new AnnotationConfigApplicationContext(ApplicationConfig.class);
|
||||||
|
|
||||||
|
Foo foo = context.getBean(Foo.class);
|
||||||
|
|
||||||
|
assertNotNull(foo);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@SuppressWarnings("resource")
|
||||||
|
public void whenGetPrototypeBean_thenReturnConstructedBean() {
|
||||||
|
|
||||||
|
String expectedName = "Some name";
|
||||||
|
ApplicationContext context = new AnnotationConfigApplicationContext(ApplicationConfig.class);
|
||||||
|
|
||||||
|
Bar bar = context.getBean(Bar.class, expectedName);
|
||||||
|
|
||||||
|
assertNotNull(bar);
|
||||||
|
assertThat(bar.getName(), is(expectedName));
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,9 @@
|
||||||
|
package com.baeldung.spring.patterns.factory;
|
||||||
|
|
||||||
|
import org.springframework.context.annotation.ComponentScan;
|
||||||
|
import org.springframework.context.annotation.Configuration;
|
||||||
|
|
||||||
|
@Configuration
|
||||||
|
@ComponentScan(basePackageClasses = ApplicationConfig.class)
|
||||||
|
public class ApplicationConfig {
|
||||||
|
}
|
|
@ -0,0 +1,20 @@
|
||||||
|
package com.baeldung.spring.patterns.factory;
|
||||||
|
|
||||||
|
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
|
||||||
|
import org.springframework.context.annotation.Scope;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
|
@Component
|
||||||
|
@Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)
|
||||||
|
public class Bar {
|
||||||
|
|
||||||
|
private String name;
|
||||||
|
|
||||||
|
public Bar(String name) {
|
||||||
|
this.name = name;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getName() {
|
||||||
|
return name;
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,36 @@
|
||||||
|
package com.baeldung.spring.patterns.factory;
|
||||||
|
|
||||||
|
import static org.hamcrest.CoreMatchers.is;
|
||||||
|
import static org.hamcrest.MatcherAssert.assertThat;
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertNotNull;
|
||||||
|
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
import org.springframework.context.ApplicationContext;
|
||||||
|
import org.springframework.context.support.ClassPathXmlApplicationContext;
|
||||||
|
|
||||||
|
public class ClassPathXmlApplicationContextUnitTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@SuppressWarnings("resource")
|
||||||
|
public void givenXmlConfiguration_whenGetSimpleBean_thenReturnConstructedBean() {
|
||||||
|
|
||||||
|
ApplicationContext context = new ClassPathXmlApplicationContext("patterns-context.xml");
|
||||||
|
|
||||||
|
Foo foo = context.getBean(Foo.class);
|
||||||
|
|
||||||
|
assertNotNull(foo);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@SuppressWarnings("resource")
|
||||||
|
public void givenXmlConfiguration_whenGetPrototypeBean_thenReturnConstructedBean() {
|
||||||
|
|
||||||
|
String expectedName = "Some name";
|
||||||
|
ApplicationContext context = new ClassPathXmlApplicationContext("patterns-context.xml");
|
||||||
|
|
||||||
|
Bar bar = context.getBean(Bar.class, expectedName);
|
||||||
|
|
||||||
|
assertNotNull(bar);
|
||||||
|
assertThat(bar.getName(), is(expectedName));
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,7 @@
|
||||||
|
package com.baeldung.spring.patterns.factory;
|
||||||
|
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
|
@Component
|
||||||
|
public class Foo {
|
||||||
|
}
|
|
@ -0,0 +1,9 @@
|
||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<beans xmlns="http://www.springframework.org/schema/beans"
|
||||||
|
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||||
|
xsi:schemaLocation="http://www.springframework.org/schema/beans
|
||||||
|
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
|
||||||
|
|
||||||
|
<bean id="foo" class="com.baeldung.spring.patterns.factory.Foo" />
|
||||||
|
<bean id="bar" scope="prototype" class="com.baeldung.spring.patterns.factory.Bar" />
|
||||||
|
</beans>
|
Loading…
Reference in New Issue