Merge branch 'master' of https://github.com/eugenp/tutorials
This commit is contained in:
commit
c1ca5969c2
|
@ -7,7 +7,6 @@ This module contains articles about core Java input/output(IO) APIs.
|
|||
- [A Guide to the Java FileReader Class](https://www.baeldung.com/java-filereader)
|
||||
- [The Java File Class](https://www.baeldung.com/java-io-file)
|
||||
- [Java FileWriter](https://www.baeldung.com/java-filewriter)
|
||||
- [Differences Between the Java WatchService API and the Apache Commons IO Monitor Library](https://www.baeldung.com/java-watchservice-vs-apache-commons-io-monitor-library)
|
||||
- [Comparing getPath(), getAbsolutePath(), and getCanonicalPath() in Java](https://www.baeldung.com/java-path)
|
||||
- [Quick Use of FilenameFilter](https://www.baeldung.com/java-filename-filter)
|
||||
- [Guide to BufferedReader](https://www.baeldung.com/java-buffered-reader)
|
||||
|
|
|
@ -13,3 +13,4 @@ This module contains articles about Apache Commons libraries.
|
|||
- [Apache Commons BeanUtils](https://www.baeldung.com/apache-commons-beanutils)
|
||||
- [Histograms with Apache Commons Frequency](https://www.baeldung.com/apache-commons-frequency)
|
||||
- [An Introduction to Apache Commons Lang 3](https://www.baeldung.com/java-commons-lang-3)
|
||||
- [Differences Between the Java WatchService API and the Apache Commons IO Monitor Library](https://www.baeldung.com/java-watchservice-vs-apache-commons-io-monitor-library)
|
|
@ -57,6 +57,11 @@
|
|||
<artifactId>xchart</artifactId>
|
||||
<version>${xchart-version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>commons-io</groupId>
|
||||
<artifactId>commons-io</artifactId>
|
||||
<version>${common-io.version}</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<properties>
|
||||
|
@ -68,6 +73,7 @@
|
|||
<commons.dbutils.version>1.6</commons.dbutils.version>
|
||||
<xchart-version>3.5.2</xchart-version>
|
||||
<common-math3.version>3.6.1</common-math3.version>
|
||||
<common-io.version>2.5</common-io.version>
|
||||
</properties>
|
||||
|
||||
</project>
|
||||
|
|
|
@ -31,6 +31,7 @@
|
|||
<dependency>
|
||||
<groupId>io.rest-assured</groupId>
|
||||
<artifactId>rest-assured</artifactId>
|
||||
<version>${rest-assured.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
|
|
|
@ -0,0 +1,23 @@
|
|||
package com.baeldung.statmentVsPreparedstatment;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.DriverManager;
|
||||
import java.sql.SQLException;
|
||||
|
||||
public class DatasourceFactory {
|
||||
|
||||
private Connection connection;
|
||||
|
||||
public Connection getConnection() throws ClassNotFoundException, SQLException {
|
||||
Class.forName("org.h2.Driver");
|
||||
connection = DriverManager.getConnection("jdbc:h2:mem:db_basic", "SA", "");
|
||||
connection.setAutoCommit(false);
|
||||
return connection;
|
||||
}
|
||||
|
||||
public boolean createTables() throws SQLException {
|
||||
String query = "create table if not exists PERSONS (ID INT, NAME VARCHAR(45))";
|
||||
return connection.createStatement().executeUpdate(query) == 0;
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,42 @@
|
|||
package com.baeldung.statmentVsPreparedstatment;
|
||||
|
||||
import java.util.Objects;
|
||||
|
||||
public class PersonEntity {
|
||||
private int id;
|
||||
private String name;
|
||||
|
||||
public PersonEntity(int id, String name) {
|
||||
this.id = id;
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public int getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(int id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
@Override public boolean equals(Object o) {
|
||||
if (this == o)
|
||||
return true;
|
||||
if (o == null || getClass() != o.getClass())
|
||||
return false;
|
||||
PersonEntity that = (PersonEntity) o;
|
||||
return id == that.id && Objects.equals(name, that.name);
|
||||
}
|
||||
|
||||
@Override public int hashCode() {
|
||||
return Objects.hash(id, name);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,88 @@
|
|||
package com.baeldung.statmentVsPreparedstatment;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.PreparedStatement;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
public class PreparedStatementPersonDao {
|
||||
|
||||
private final Connection connection;
|
||||
|
||||
public PreparedStatementPersonDao(Connection connection) {
|
||||
this.connection = connection;
|
||||
}
|
||||
|
||||
public Optional<PersonEntity> getById(int id) throws SQLException {
|
||||
String query = "SELECT id, name FROM persons WHERE id = ?";
|
||||
|
||||
PreparedStatement preparedStatement = connection.prepareStatement(query);
|
||||
preparedStatement.setInt(1, id);
|
||||
ResultSet resultSet = preparedStatement.executeQuery();
|
||||
|
||||
if (resultSet.first()) {
|
||||
|
||||
PersonEntity result = new PersonEntity(resultSet.getInt("id"),
|
||||
resultSet.getString("name"));
|
||||
|
||||
return Optional.of(result);
|
||||
} else {
|
||||
return Optional.empty();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public void insert(PersonEntity personEntity) throws SQLException {
|
||||
|
||||
String query = "INSERT INTO persons(id, name) VALUES( ?, ?)";
|
||||
|
||||
PreparedStatement preparedStatement = connection.prepareStatement(query);
|
||||
preparedStatement.setInt(1, personEntity.getId());
|
||||
preparedStatement.setString(2, personEntity.getName());
|
||||
preparedStatement.executeUpdate();
|
||||
|
||||
}
|
||||
|
||||
public void insert(List<PersonEntity> personEntities) throws SQLException {
|
||||
String query = "INSERT INTO persons(id, name) VALUES( ?, ?)";
|
||||
|
||||
PreparedStatement preparedStatement = connection.prepareStatement(query);
|
||||
for (PersonEntity personEntity : personEntities) {
|
||||
preparedStatement.setInt(1, personEntity.getId());
|
||||
preparedStatement.setString(2, personEntity.getName());
|
||||
preparedStatement.addBatch();
|
||||
}
|
||||
preparedStatement.executeBatch();
|
||||
|
||||
}
|
||||
|
||||
public void update(PersonEntity personEntity) throws SQLException {
|
||||
String query = "UPDATE persons SET name = ? WHERE id = ?";
|
||||
PreparedStatement preparedStatement = connection.prepareStatement(query);
|
||||
preparedStatement.setString(1, personEntity.getName());
|
||||
preparedStatement.setInt(2, personEntity.getId());
|
||||
preparedStatement.executeUpdate();
|
||||
}
|
||||
|
||||
public void deleteById(int id) throws SQLException {
|
||||
String query = "DELETE FROM persons WHERE id = ?";
|
||||
PreparedStatement preparedStatement = connection.prepareStatement(query);
|
||||
preparedStatement.setInt(1, id);
|
||||
preparedStatement.executeUpdate();
|
||||
}
|
||||
|
||||
public List<PersonEntity> getAll() throws SQLException {
|
||||
String query = "SELECT id, name FROM persons";
|
||||
|
||||
PreparedStatement preparedStatement = connection.prepareStatement(query);
|
||||
ResultSet resultSet = preparedStatement.executeQuery();
|
||||
List<PersonEntity> result = new ArrayList<>();
|
||||
while (resultSet.next()) {
|
||||
result.add(new PersonEntity(resultSet.getInt("id"), resultSet.getString("name")));
|
||||
}
|
||||
return result;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,75 @@
|
|||
package com.baeldung.statmentVsPreparedstatment;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
import java.sql.Statement;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
public class StatementPersonDao {
|
||||
|
||||
private final Connection connection;
|
||||
|
||||
public StatementPersonDao(Connection connection) {
|
||||
this.connection = connection;
|
||||
}
|
||||
|
||||
public Optional<PersonEntity> getById(int id) throws SQLException {
|
||||
String query = "SELECT id, name, FROM persons WHERE id = '" + id + "'";
|
||||
|
||||
Statement statement = connection.createStatement();
|
||||
ResultSet resultSet = statement.executeQuery(query);
|
||||
|
||||
if (resultSet.first()) {
|
||||
PersonEntity result = new PersonEntity(resultSet.getInt("id"),
|
||||
resultSet.getString("name"));
|
||||
return Optional.of(result);
|
||||
} else {
|
||||
return Optional.empty();
|
||||
}
|
||||
}
|
||||
|
||||
public void insert(PersonEntity personEntity) throws SQLException {
|
||||
String query = "INSERT INTO persons(id, name) VALUES(" + personEntity.getId() + ", '"
|
||||
+ personEntity.getName() + "')";
|
||||
|
||||
Statement statement = connection.createStatement();
|
||||
statement.executeUpdate(query);
|
||||
}
|
||||
|
||||
public void insert(List<PersonEntity> personEntities) throws SQLException {
|
||||
for (PersonEntity personEntity : personEntities) {
|
||||
insert(personEntity);
|
||||
}
|
||||
}
|
||||
|
||||
public void update(PersonEntity personEntity) throws SQLException {
|
||||
|
||||
String query = "UPDATE persons SET name = '" + personEntity.getName() + "' WHERE id = "
|
||||
+ personEntity.getId();
|
||||
|
||||
Statement statement = connection.createStatement();
|
||||
statement.executeUpdate(query);
|
||||
|
||||
}
|
||||
|
||||
public void deleteById(int id) throws SQLException {
|
||||
String query = "DELETE FROM persons WHERE id = " + id;
|
||||
Statement statement = connection.createStatement();
|
||||
statement.executeUpdate(query);
|
||||
}
|
||||
|
||||
public List<PersonEntity> getAll() throws SQLException {
|
||||
String query = "SELECT id, name, FROM persons";
|
||||
|
||||
Statement statement = connection.createStatement();
|
||||
ResultSet resultSet = statement.executeQuery(query);
|
||||
List<PersonEntity> result = new ArrayList<>();
|
||||
while (resultSet.next()) {
|
||||
result.add(new PersonEntity(resultSet.getInt("id"), resultSet.getString("name")));
|
||||
}
|
||||
return result;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,22 @@
|
|||
package com.baeldung.statmentVsPreparedstatment;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.SQLException;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
class DatasourceFactoryUnitTest {
|
||||
|
||||
@Test
|
||||
void whenCreateConnectionAndTables_thenConnectionIsOpenAndTableIsCreated()
|
||||
throws SQLException, ClassNotFoundException {
|
||||
DatasourceFactory factory = new DatasourceFactory();
|
||||
Connection connection = factory.getConnection();
|
||||
|
||||
assertFalse(connection.isClosed());
|
||||
assertTrue(factory.createTables());
|
||||
}
|
||||
}
|
|
@ -0,0 +1,94 @@
|
|||
package com.baeldung.statmentVsPreparedstatment;
|
||||
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.SQLException;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
class PreparedStatementPersonDaoUnitTest {
|
||||
private PreparedStatementPersonDao dao;
|
||||
|
||||
@BeforeEach
|
||||
void setup() throws SQLException, ClassNotFoundException {
|
||||
DatasourceFactory datasourceFactory = new DatasourceFactory();
|
||||
Connection connection = datasourceFactory.getConnection();
|
||||
datasourceFactory.createTables();
|
||||
dao = new PreparedStatementPersonDao(connection);
|
||||
}
|
||||
|
||||
@Test
|
||||
void whenInsertAPerson_thenItNeverThrowsAnException() {
|
||||
assertDoesNotThrow(() -> dao.insert(new PersonEntity(1, "john")));
|
||||
}
|
||||
|
||||
@Test
|
||||
void whenInsertAPersonWithQuoteInText_thenItNeverThrowsAnException() {
|
||||
assertDoesNotThrow(() -> dao.insert(new PersonEntity(1, "O'Brien")));
|
||||
}
|
||||
|
||||
@Test
|
||||
void whenGetAPersonById_thenItReturnThePersonInDatabase() throws SQLException {
|
||||
dao.insert(new PersonEntity(1, "john"));
|
||||
|
||||
Optional<PersonEntity> maybeEmployee = dao.getById(1);
|
||||
assertTrue(maybeEmployee.isPresent());
|
||||
|
||||
PersonEntity personEntity = maybeEmployee.get();
|
||||
|
||||
assertEquals(1, personEntity.getId());
|
||||
assertEquals("john", personEntity.getName());
|
||||
}
|
||||
|
||||
@Test
|
||||
void whenInsertAndGetMultiplePersons_thenItNeverThrowsAnException() throws SQLException {
|
||||
assertDoesNotThrow(() -> dao.insert(
|
||||
Arrays.asList(new PersonEntity(1, "john"), new PersonEntity(2, "skit"))));
|
||||
|
||||
List<PersonEntity> result = dao.getAll();
|
||||
|
||||
assertEquals(Arrays.asList(new PersonEntity(1, "john"), new PersonEntity(2, "skit")),
|
||||
result);
|
||||
}
|
||||
|
||||
@Test
|
||||
void whenUpdateAnExistentPerson_thenItReturnsTheUpdatedPerson() throws SQLException {
|
||||
dao.insert(new PersonEntity(1, "john"));
|
||||
dao.update(new PersonEntity(1, "johnny"));
|
||||
|
||||
Optional<PersonEntity> maybePerson = dao.getById(1);
|
||||
|
||||
assertTrue(maybePerson.isPresent());
|
||||
|
||||
PersonEntity personEntity = maybePerson.get();
|
||||
|
||||
assertEquals(1, personEntity.getId());
|
||||
assertEquals("johnny", personEntity.getName());
|
||||
}
|
||||
|
||||
@Test
|
||||
void whenDeleteAPersonById_thenItWillBeAbsentInDatabase() throws SQLException {
|
||||
dao.insert(new PersonEntity(1, "john"));
|
||||
dao.deleteById(1);
|
||||
|
||||
Optional<PersonEntity> maybePerson = dao.getById(1);
|
||||
|
||||
assertFalse(maybePerson.isPresent());
|
||||
}
|
||||
|
||||
@Test
|
||||
void whenAHackerUpdateAPerson_thenItUpdatesTheTargetPerson() throws SQLException {
|
||||
dao.insert(Arrays.asList(new PersonEntity(1, "john"), new PersonEntity(2, "skeet")));
|
||||
dao.update(new PersonEntity(1, "hacker' --"));
|
||||
|
||||
List<PersonEntity> result = dao.getAll();
|
||||
|
||||
assertEquals(Arrays.asList(new PersonEntity(1, "hacker' --"), new PersonEntity(2, "skeet")),
|
||||
result);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,96 @@
|
|||
package com.baeldung.statmentVsPreparedstatment;
|
||||
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.SQLException;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
class StatementPersonDaoUnitTest {
|
||||
|
||||
private StatementPersonDao dao;
|
||||
|
||||
@BeforeEach
|
||||
void setup() throws SQLException, ClassNotFoundException {
|
||||
DatasourceFactory datasourceFactory = new DatasourceFactory();
|
||||
Connection connection = datasourceFactory.getConnection();
|
||||
datasourceFactory.createTables();
|
||||
dao = new StatementPersonDao(connection);
|
||||
}
|
||||
|
||||
@Test
|
||||
void whenInsertAPerson_thenItNeverThrowsAnException() {
|
||||
assertDoesNotThrow(() -> dao.insert(new PersonEntity(1, "john")));
|
||||
}
|
||||
|
||||
@Test
|
||||
void whenInsertAPersonWithQuoteInText_thenItWillThrowAnException() {
|
||||
assertThrows(SQLException.class, () -> dao.insert(new PersonEntity(1, "O'Brien")));
|
||||
}
|
||||
|
||||
@Test
|
||||
void whenGetAPersonById_thenItReturnThePersonInDatabase() throws SQLException {
|
||||
dao.insert(new PersonEntity(1, "john"));
|
||||
|
||||
Optional<PersonEntity> maybeEmployee = dao.getById(1);
|
||||
|
||||
assertTrue(maybeEmployee.isPresent());
|
||||
|
||||
PersonEntity personEntity = maybeEmployee.get();
|
||||
|
||||
assertEquals(1, personEntity.getId());
|
||||
assertEquals("john", personEntity.getName());
|
||||
}
|
||||
|
||||
@Test
|
||||
void whenInsertAndGetMultiplePersons_thenItNeverThrowsAnException() throws SQLException {
|
||||
assertDoesNotThrow(() -> dao.insert(
|
||||
Arrays.asList(new PersonEntity(1, "john"), new PersonEntity(2, "skeet"))));
|
||||
|
||||
List<PersonEntity> result = dao.getAll();
|
||||
|
||||
assertEquals(Arrays.asList(new PersonEntity(1, "john"), new PersonEntity(2, "skeet")),
|
||||
result);
|
||||
}
|
||||
|
||||
@Test
|
||||
void whenUpdateAnExistentPerson_thenItReturnsTheUpdatedPerson() throws SQLException {
|
||||
dao.insert(new PersonEntity(1, "john"));
|
||||
dao.update(new PersonEntity(1, "johnny"));
|
||||
|
||||
Optional<PersonEntity> maybePerson = dao.getById(1);
|
||||
|
||||
assertTrue(maybePerson.isPresent());
|
||||
|
||||
PersonEntity personEntity = maybePerson.get();
|
||||
|
||||
assertEquals(1, personEntity.getId());
|
||||
assertEquals("johnny", personEntity.getName());
|
||||
}
|
||||
|
||||
@Test
|
||||
void whenDeleteAPersonById_thenItWillBeAbsentInDatabase() throws SQLException {
|
||||
dao.insert(new PersonEntity(1, "john"));
|
||||
dao.deleteById(1);
|
||||
|
||||
Optional<PersonEntity> maybePerson = dao.getById(1);
|
||||
|
||||
assertFalse(maybePerson.isPresent());
|
||||
}
|
||||
|
||||
@Test
|
||||
void whenAHackerUpdateAPerson_thenItAllPersonsAreUpdated() throws SQLException {
|
||||
dao.insert(Arrays.asList(new PersonEntity(1, "john"), new PersonEntity(2, "skeet")));
|
||||
dao.update(new PersonEntity(1, "hacker' --"));
|
||||
|
||||
List<PersonEntity> result = dao.getAll();
|
||||
|
||||
assertEquals(Arrays.asList(new PersonEntity(1, "hacker"), new PersonEntity(2, "hacker")),
|
||||
result);
|
||||
}
|
||||
}
|
|
@ -1,3 +1,5 @@
|
|||
### Relevant Articles:
|
||||
|
||||
- [Hibernate could not initialize proxy – no Session](https://www.baeldung.com/hibernate-initialize-proxy-exception)
|
||||
- [Hibernate Error “No Persistence Provider for EntityManager”](https://www.baeldung.com/hibernate-no-persistence-provider)
|
||||
- [TransactionRequiredException Error](https://www.baeldung.com/jpa-transaction-required-exception)
|
|
@ -11,7 +11,5 @@ This module contains articles specific to use of Hibernate as a JPA implementati
|
|||
- [Criteria API – An Example of IN Expressions](https://www.baeldung.com/jpa-criteria-api-in-expressions)
|
||||
- [One-to-One Relationship in JPA](https://www.baeldung.com/jpa-one-to-one)
|
||||
- [Enabling Transaction Locks in Spring Data JPA](https://www.baeldung.com/java-jpa-transaction-locks)
|
||||
- [TransactionRequiredException Error](https://www.baeldung.com/jpa-transaction-required-exception)
|
||||
- [JPA/Hibernate Persistence Context](https://www.baeldung.com/jpa-hibernate-persistence-context)
|
||||
- [Quick Guide to EntityManager#getReference()](https://www.baeldung.com/jpa-entity-manager-get-reference)
|
||||
- [Hibernate Error “No Persistence Provider for EntityManager”](https://www.baeldung.com/hibernate-no-persistence-provider)
|
||||
|
|
|
@ -98,7 +98,6 @@
|
|||
<cglib.version>3.2.4</cglib.version>
|
||||
<nosqlunit.version>0.10.0</nosqlunit.version>
|
||||
<embedded-redis.version>0.6</embedded-redis.version>
|
||||
<spring-boot.version>2.1.9.RELEASE</spring-boot.version>
|
||||
</properties>
|
||||
|
||||
</project>
|
||||
|
|
|
@ -31,11 +31,6 @@ public class RedisConfig {
|
|||
return new ReactiveRedisTemplate<>(factory, context);
|
||||
}
|
||||
|
||||
@Bean
|
||||
public ReactiveRedisTemplate<String, String> reactiveRedisTemplateString(ReactiveRedisConnectionFactory connectionFactory) {
|
||||
return new ReactiveRedisTemplate<>(connectionFactory, RedisSerializationContext.string());
|
||||
}
|
||||
|
||||
@Bean
|
||||
public ReactiveKeyCommands keyCommands(final ReactiveRedisConnectionFactory reactiveRedisConnectionFactory) {
|
||||
return reactiveRedisConnectionFactory.getReactiveConnection()
|
||||
|
|
|
@ -0,0 +1,13 @@
|
|||
package com.baeldung.spring.data.redis;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
|
||||
@SpringBootApplication
|
||||
public class SpringRedisApplication {
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(SpringRedisApplication.class, args);
|
||||
}
|
||||
|
||||
}
|
|
@ -2,9 +2,6 @@ package com.baeldung.spring.data.reactive.redis.template;
|
|||
|
||||
|
||||
import com.baeldung.spring.data.reactive.redis.SpringRedisReactiveApplication;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import org.junit.AfterClass;
|
||||
import org.junit.Before;
|
||||
import org.junit.BeforeClass;
|
||||
|
@ -13,7 +10,7 @@ import org.junit.runner.RunWith;
|
|||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.data.redis.core.ReactiveListOperations;
|
||||
import org.springframework.data.redis.core.ReactiveRedisTemplate;
|
||||
import org.springframework.data.redis.core.ReactiveStringRedisTemplate;
|
||||
import org.springframework.test.annotation.DirtiesContext;
|
||||
import org.springframework.test.annotation.DirtiesContext.ClassMode;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
|
@ -21,6 +18,8 @@ import reactor.core.publisher.Mono;
|
|||
import reactor.test.StepVerifier;
|
||||
import redis.embedded.RedisServerBuilder;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
@RunWith(SpringRunner.class)
|
||||
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT, classes = SpringRedisReactiveApplication.class)
|
||||
@DirtiesContext(classMode = ClassMode.BEFORE_CLASS)
|
||||
|
@ -30,7 +29,7 @@ public class RedisTemplateListOpsIntegrationTest {
|
|||
private static redis.embedded.RedisServer redisServer;
|
||||
|
||||
@Autowired
|
||||
private ReactiveRedisTemplate<String, String> redisTemplate;
|
||||
private ReactiveStringRedisTemplate redisTemplate;
|
||||
|
||||
private ReactiveListOperations<String, String> reactiveListOps;
|
||||
|
||||
|
|
|
@ -1,44 +1,40 @@
|
|||
package com.baeldung.spring.data.redis;
|
||||
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.UUID;
|
||||
|
||||
import com.baeldung.spring.data.redis.queue.RedisMessagePublisher;
|
||||
import com.baeldung.spring.data.redis.queue.RedisMessageSubscriber;
|
||||
import org.junit.AfterClass;
|
||||
import org.junit.BeforeClass;
|
||||
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.annotation.DirtiesContext;
|
||||
import org.springframework.test.annotation.DirtiesContext.ClassMode;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
|
||||
import com.baeldung.spring.data.redis.config.RedisConfig;
|
||||
import com.baeldung.spring.data.redis.queue.RedisMessagePublisher;
|
||||
import com.baeldung.spring.data.redis.queue.RedisMessageSubscriber;
|
||||
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
import redis.embedded.RedisServerBuilder;
|
||||
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@ContextConfiguration(classes = RedisConfig.class)
|
||||
import java.util.UUID;
|
||||
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
@RunWith(SpringRunner.class)
|
||||
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT, classes = SpringRedisApplication.class)
|
||||
@DirtiesContext(classMode = ClassMode.BEFORE_CLASS)
|
||||
public class RedisMessageListenerIntegrationTest {
|
||||
|
||||
private static redis.embedded.RedisServer redisServer;
|
||||
|
||||
|
||||
@Autowired
|
||||
private RedisMessagePublisher redisMessagePublisher;
|
||||
|
||||
|
||||
@BeforeClass
|
||||
public static void startRedisServer() throws IOException {
|
||||
public static void startRedisServer() {
|
||||
redisServer = new RedisServerBuilder().port(6379).setting("maxmemory 256M").build();
|
||||
redisServer.start();
|
||||
}
|
||||
|
||||
|
||||
@AfterClass
|
||||
public static void stopRedisServer() throws IOException {
|
||||
public static void stopRedisServer() {
|
||||
redisServer.stop();
|
||||
}
|
||||
|
||||
|
|
6
pom.xml
6
pom.xml
|
@ -386,6 +386,7 @@
|
|||
|
||||
<module>core-java-modules</module>
|
||||
<module>core-kotlin-modules</module>
|
||||
<module>core-scala</module>
|
||||
|
||||
<module>couchbase</module>
|
||||
<module>custom-pmd</module>
|
||||
|
@ -559,6 +560,7 @@
|
|||
|
||||
<module>atomikos</module>
|
||||
<module>reactive-systems</module>
|
||||
<module>slack</module>
|
||||
</modules>
|
||||
|
||||
</profile>
|
||||
|
@ -707,6 +709,7 @@
|
|||
<module>spring-rest-shell</module>
|
||||
<module>spring-rest-simple</module>
|
||||
<module>spring-resttemplate</module>
|
||||
<module>spring-resttemplate-2</module>
|
||||
<module>spring-rest-testing</module>
|
||||
<module>spring-roo</module>
|
||||
|
||||
|
@ -901,6 +904,7 @@
|
|||
|
||||
<module>core-java-modules</module>
|
||||
<module>core-kotlin-modules</module>
|
||||
<module>core-scala</module>
|
||||
|
||||
<module>couchbase</module>
|
||||
<module>custom-pmd</module>
|
||||
|
@ -1071,6 +1075,7 @@
|
|||
|
||||
<module>atomikos</module>
|
||||
<module>reactive-systems</module>
|
||||
<module>slack</module>
|
||||
</modules>
|
||||
|
||||
</profile>
|
||||
|
@ -1211,6 +1216,7 @@
|
|||
<module>spring-rest-shell</module>
|
||||
<module>spring-rest-simple</module>
|
||||
<module>spring-resttemplate</module>
|
||||
<module>spring-resttemplate-2</module>
|
||||
<module>spring-rest-testing</module>
|
||||
<module>spring-roo</module>
|
||||
|
||||
|
|
|
@ -116,7 +116,6 @@
|
|||
|
||||
<properties>
|
||||
<guava.version>18.0</guava.version>
|
||||
<junit-platform.version>1.2.0</junit-platform.version>
|
||||
<git-commit-id-plugin.version>2.2.4</git-commit-id-plugin.version>
|
||||
</properties>
|
||||
|
||||
|
|
|
@ -41,7 +41,5 @@
|
|||
<properties>
|
||||
<java.version>1.8</java.version>
|
||||
<spring-boot-starter-data-jpa.version>2.2.5.RELEASE</spring-boot-starter-data-jpa.version>
|
||||
<junit.version>4.11</junit.version>
|
||||
</properties>
|
||||
|
||||
</project>
|
||||
|
|
|
@ -10,9 +10,9 @@
|
|||
|
||||
<parent>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>parent-modules</artifactId>
|
||||
<version>1.0.0-SNAPSHOT</version>
|
||||
<relativePath>..</relativePath>
|
||||
<artifactId>parent-boot-2</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<relativePath>../parent-boot-2</relativePath>
|
||||
</parent>
|
||||
|
||||
<modules>
|
||||
|
@ -56,17 +56,15 @@
|
|||
</build>
|
||||
|
||||
<properties>
|
||||
<spring-session.version>1.2.2.RELEASE</spring-session.version>
|
||||
<spring-cloud-dependencies.version>Brixton.SR7</spring-cloud-dependencies.version>
|
||||
<spring-cloud-starter-config.version>1.2.2.RELEASE</spring-cloud-starter-config.version>
|
||||
<spring-cloud-config-server.version>1.2.2.RELEASE</spring-cloud-config-server.version>
|
||||
<spring-cloud-starter-eureka.version>2.0.2.RELEASE</spring-cloud-starter-eureka.version>
|
||||
<spring-cloud-starter-feign.version>1.4.6.RELEASE</spring-cloud-starter-feign.version>
|
||||
<spring-cloud-starter-hystrix.version>1.2.3.RELEASE</spring-cloud-starter-hystrix.version>
|
||||
<spring-cloud-stream.version>1.3.0.RELEASE</spring-cloud-stream.version>
|
||||
<spring-boot-starter-web.version>1.4.2.RELEASE</spring-boot-starter-web.version>
|
||||
<spring-boot-maven-plugin.version>1.4.2.RELEASE</spring-boot-maven-plugin.version>
|
||||
<spring-cloud-starter-zuul.version>1.2.3.RELEASE</spring-cloud-starter-zuul.version>
|
||||
<spring-cloud-dependencies.version>Hoxton.SR4</spring-cloud-dependencies.version>
|
||||
<spring-cloud-starter-config.version>2.2.3.RELEASE</spring-cloud-starter-config.version>
|
||||
<spring-cloud-config-server.version>2.2.3.RELEASE</spring-cloud-config-server.version>
|
||||
<spring-cloud-starter-eureka.version>1.4.7.RELEASE</spring-cloud-starter-eureka.version>
|
||||
<spring-cloud-starter-feign.version>1.4.7.RELEASE</spring-cloud-starter-feign.version>
|
||||
<spring-cloud-starter-hystrix.version>1.4.7.RELEASE</spring-cloud-starter-hystrix.version>
|
||||
<spring-cloud-stream.version>3.0.6.RELEASE</spring-cloud-stream.version>
|
||||
<spring-boot-starter-web.version>2.3.1.RELEASE</spring-boot-starter-web.version>
|
||||
<spring-boot-maven-plugin.version>2.3.1.RELEASE</spring-boot-maven-plugin.version>
|
||||
</properties>
|
||||
|
||||
</project>
|
||||
|
|
|
@ -4,12 +4,14 @@ import org.springframework.boot.autoconfigure.SpringBootApplication;
|
|||
import org.springframework.boot.builder.SpringApplicationBuilder;
|
||||
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
|
||||
|
||||
import static org.springframework.boot.WebApplicationType.NONE;
|
||||
|
||||
@SpringBootApplication
|
||||
@EnableDiscoveryClient
|
||||
public class DiscoveryClientApplication {
|
||||
|
||||
public static void main(String[] args) {
|
||||
new SpringApplicationBuilder(DiscoveryClientApplication.class).web(true)
|
||||
new SpringApplicationBuilder(DiscoveryClientApplication.class).web(NONE)
|
||||
.run(args);
|
||||
}
|
||||
|
||||
|
|
|
@ -1,13 +1,16 @@
|
|||
package com.baeldung.spring.cloud.consul.health;
|
||||
|
||||
import org.springframework.boot.WebApplicationType;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.boot.builder.SpringApplicationBuilder;
|
||||
|
||||
import static org.springframework.boot.WebApplicationType.NONE;
|
||||
|
||||
@SpringBootApplication
|
||||
public class ServiceDiscoveryApplication {
|
||||
|
||||
public static void main(String[] args) {
|
||||
new SpringApplicationBuilder(ServiceDiscoveryApplication.class).web(true)
|
||||
new SpringApplicationBuilder(ServiceDiscoveryApplication.class).web(NONE)
|
||||
.run(args);
|
||||
}
|
||||
|
||||
|
|
|
@ -4,12 +4,14 @@ import org.springframework.boot.autoconfigure.SpringBootApplication;
|
|||
import org.springframework.boot.builder.SpringApplicationBuilder;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import static org.springframework.boot.WebApplicationType.NONE;
|
||||
|
||||
@SpringBootApplication
|
||||
@RestController
|
||||
public class DistributedPropertiesApplication {
|
||||
|
||||
public static void main(String[] args) {
|
||||
new SpringApplicationBuilder(DistributedPropertiesApplication.class).web(true)
|
||||
new SpringApplicationBuilder(DistributedPropertiesApplication.class).web(NONE)
|
||||
.run(args);
|
||||
}
|
||||
|
||||
|
|
|
@ -85,7 +85,6 @@
|
|||
<spring-cloud-function.version>1.0.1.RELEASE</spring-cloud-function.version>
|
||||
<aws-lambda-events.version>2.0.2</aws-lambda-events.version>
|
||||
<aws-lambda-java-core.version>1.1.0</aws-lambda-java-core.version>
|
||||
<spring-boot.version>2.0.4.RELEASE</spring-boot.version>
|
||||
<spring-boot-thin.version>1.0.10.RELEASE</spring-boot-thin.version>
|
||||
</properties>
|
||||
|
||||
|
|
|
@ -36,18 +36,22 @@
|
|||
<dependency>
|
||||
<groupId>org.springframework.cloud</groupId>
|
||||
<artifactId>spring-cloud-starter-hystrix</artifactId>
|
||||
<version>${spring-cloud-starter-hystrix.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.cloud</groupId>
|
||||
<artifactId>spring-cloud-starter-hystrix-dashboard</artifactId>
|
||||
<version>${spring-cloud-starter-hystrix.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.cloud</groupId>
|
||||
<artifactId>spring-cloud-starter-feign</artifactId>
|
||||
<version>${spring-cloud-starter-feign.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-web</artifactId>
|
||||
<version>${spring-boot-starter-web.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
package com.baeldung.spring.cloud.hystrix.rest.consumer;
|
||||
|
||||
import com.baeldung.spring.cloud.hystrix.rest.producer.GreetingController;
|
||||
import org.springframework.cloud.netflix.feign.FeignClient;
|
||||
import org.springframework.cloud.openfeign.FeignClient;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.web.bind.annotation.PathVariable;
|
||||
|
||||
|
|
|
@ -3,8 +3,8 @@ package com.baeldung.spring.cloud.hystrix.rest.consumer;
|
|||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker;
|
||||
import org.springframework.cloud.netflix.feign.EnableFeignClients;
|
||||
import org.springframework.cloud.netflix.hystrix.dashboard.EnableHystrixDashboard;
|
||||
import org.springframework.cloud.openfeign.EnableFeignClients;
|
||||
|
||||
@SpringBootApplication
|
||||
@EnableCircuitBreaker
|
||||
|
|
|
@ -28,13 +28,13 @@ public class MultipleOutputsServiceApplicationIntegrationTest {
|
|||
@Test
|
||||
public void whenSendMessage_thenResponseIsInAOutput() {
|
||||
whenSendMessage(1);
|
||||
thenPayloadInChannelIs(pipe.anOutput(), 1);
|
||||
thenPayloadInChannelIs(pipe.anOutput(), "1");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenSendMessage_thenResponseIsInAnotherOutput() {
|
||||
whenSendMessage(11);
|
||||
thenPayloadInChannelIs(pipe.anotherOutput(), 11);
|
||||
thenPayloadInChannelIs(pipe.anotherOutput(), "11");
|
||||
}
|
||||
|
||||
private void whenSendMessage(Integer val) {
|
||||
|
@ -43,7 +43,7 @@ public class MultipleOutputsServiceApplicationIntegrationTest {
|
|||
.build());
|
||||
}
|
||||
|
||||
private void thenPayloadInChannelIs(MessageChannel channel, Integer expectedValue) {
|
||||
private void thenPayloadInChannelIs(MessageChannel channel, String expectedValue) {
|
||||
Object payload = messageCollector.forChannel(channel)
|
||||
.poll()
|
||||
.getPayload();
|
||||
|
|
|
@ -28,13 +28,13 @@ public class MultipleOutputsWithConditionsServiceIntegrationTest {
|
|||
@Test
|
||||
public void whenSendMessage_thenResponseIsInAOutput() {
|
||||
whenSendMessage(1);
|
||||
thenPayloadInChannelIs(pipe.anOutput(), 1);
|
||||
thenPayloadInChannelIs(pipe.anOutput(), "1");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenSendMessage_thenResponseIsInAnotherOutput() {
|
||||
whenSendMessage(11);
|
||||
thenPayloadInChannelIs(pipe.anotherOutput(), 11);
|
||||
thenPayloadInChannelIs(pipe.anotherOutput(), "11");
|
||||
}
|
||||
|
||||
private void whenSendMessage(Integer val) {
|
||||
|
@ -43,7 +43,7 @@ public class MultipleOutputsWithConditionsServiceIntegrationTest {
|
|||
.build());
|
||||
}
|
||||
|
||||
private void thenPayloadInChannelIs(MessageChannel channel, Integer expectedValue) {
|
||||
private void thenPayloadInChannelIs(MessageChannel channel, String expectedValue) {
|
||||
Object payload = messageCollector.forChannel(channel)
|
||||
.poll()
|
||||
.getPayload();
|
||||
|
|
|
@ -35,6 +35,6 @@ public class MyLoggerApplicationIntegrationTest {
|
|||
.poll()
|
||||
.getPayload();
|
||||
|
||||
assertEquals("[1]: This is my message", payload.toString());
|
||||
assertEquals("{\"message\":\"[1]: This is my message\"}", payload.toString());
|
||||
}
|
||||
}
|
||||
|
|
|
@ -30,6 +30,10 @@
|
|||
<artifactId>spring-boot-starter</artifactId>
|
||||
<version>${spring-boot.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-web</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-web</artifactId>
|
||||
|
@ -56,7 +60,10 @@
|
|||
<artifactId>spring-cloud-starter-feign</artifactId>
|
||||
<version>${spring-cloud-starter-feign.version}</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework.cloud</groupId>
|
||||
<artifactId>spring-cloud-starter-openfeign</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-test</artifactId>
|
||||
|
@ -71,7 +78,6 @@
|
|||
</dependencies>
|
||||
|
||||
<properties>
|
||||
<spring-cloud-starter-feign.version>1.2.5.RELEASE</spring-cloud-starter-feign.version>
|
||||
<hamcrest-core.version>1.3</hamcrest-core.version>
|
||||
</properties>
|
||||
|
||||
|
|
|
@ -2,8 +2,8 @@ package com.baeldung.spring.cloud.greeting;
|
|||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
|
||||
import org.springframework.cloud.netflix.feign.EnableFeignClients;
|
||||
import org.springframework.cloud.netflix.feign.FeignClient;
|
||||
import org.springframework.cloud.openfeign.EnableFeignClients;
|
||||
import org.springframework.cloud.openfeign.FeignClient;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMethod;
|
||||
|
|
|
@ -18,8 +18,7 @@
|
|||
</modules>
|
||||
|
||||
<properties>
|
||||
<spring-boot.version>1.5.2.RELEASE</spring-boot.version>
|
||||
<springframework.version>4.3.7.RELEASE</springframework.version>
|
||||
<springframework.version>5.2.7.RELEASE</springframework.version>
|
||||
<spring-cloud-starter-zookeeper-discovery.version>1.0.3.RELEASE</spring-cloud-starter-zookeeper-discovery.version>
|
||||
</properties>
|
||||
|
||||
|
|
|
@ -6,13 +6,9 @@ This module contains articles about core Spring functionality
|
|||
|
||||
- [Guide to Spring @Autowired](http://www.baeldung.com/spring-autowire)
|
||||
- [Spring Profiles](http://www.baeldung.com/spring-profiles)
|
||||
- [A Spring Custom Annotation for a Better DAO](http://www.baeldung.com/spring-annotation-bean-pre-processor)
|
||||
- [Quick Guide to Spring Bean Scopes](http://www.baeldung.com/spring-bean-scopes)
|
||||
- [Custom Scope in Spring](http://www.baeldung.com/spring-custom-scope)
|
||||
- [@Order in Spring](http://www.baeldung.com/spring-order)
|
||||
- [Spring @Primary Annotation](http://www.baeldung.com/spring-primary)
|
||||
- [Spring Events](https://www.baeldung.com/spring-events)
|
||||
- [Spring Null-Safety Annotations](https://www.baeldung.com/spring-null-safety-annotations)
|
||||
- [Using @Autowired in Abstract Classes](https://www.baeldung.com/spring-autowired-abstract-class)
|
||||
- [Running Setup Data on Startup in Spring](https://www.baeldung.com/running-setup-logic-on-startup-in-spring)
|
||||
- More articles: [[<-- prev]](/spring-core)[[next -->]](/spring-core-3)
|
||||
|
|
|
@ -7,8 +7,8 @@ This module contains articles about core Spring functionality
|
|||
- [Understanding getBean() in Spring](https://www.baeldung.com/spring-getbean)
|
||||
- [Guide to the Spring BeanFactory](https://www.baeldung.com/spring-beanfactory)
|
||||
- [How to use the Spring FactoryBean?](https://www.baeldung.com/spring-factorybean)
|
||||
- [Spring – Injecting Collections](https://www.baeldung.com/spring-injecting-collections)
|
||||
- [Design Patterns in the Spring Framework](https://www.baeldung.com/spring-framework-design-patterns)
|
||||
- [Injecting a Value in a Static Field in Spring](https://www.baeldung.com/spring-inject-static-field)
|
||||
- [Difference Between BeanFactory and ApplicationContext](https://www.baeldung.com/spring-beanfactory-vs-applicationcontext)
|
||||
- [A Spring Custom Annotation for a Better DAO](http://www.baeldung.com/spring-annotation-bean-pre-processor)
|
||||
- [Custom Scope in Spring](http://www.baeldung.com/spring-custom-scope)
|
||||
- More articles: [[<-- prev]](/spring-core-2)
|
||||
|
|
|
@ -8,4 +8,7 @@ This module contains articles about core Spring functionality
|
|||
- [How to dynamically Autowire a Bean in Spring](https://www.baeldung.com/spring-dynamic-autowire)
|
||||
- [Spring @Import Annotation](https://www.baeldung.com/spring-import-annotation)
|
||||
- [Spring BeanPostProcessor](https://www.baeldung.com/spring-beanpostprocessor)
|
||||
- [Using @Autowired in Abstract Classes](https://www.baeldung.com/spring-autowired-abstract-class)
|
||||
- [Running Setup Data on Startup in Spring](https://www.baeldung.com/running-setup-logic-on-startup-in-spring)
|
||||
- [Constructor Injection in Spring with Lombok](https://www.baeldung.com/spring-injection-lombok)
|
||||
- More articles: [[<-- prev]](/spring-core-3)
|
||||
|
|
|
@ -24,11 +24,26 @@
|
|||
<artifactId>spring-core</artifactId>
|
||||
<version>${spring.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-web</artifactId>
|
||||
<version>${spring.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-webmvc</artifactId>
|
||||
<version>${spring.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-expression</artifactId>
|
||||
<version>${spring.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.projectlombok</groupId>
|
||||
<artifactId>lombok</artifactId>
|
||||
<version>${lombok.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.google.guava</groupId>
|
||||
<artifactId>guava</artifactId>
|
||||
|
@ -64,6 +79,12 @@
|
|||
<version>${assertj-core.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>javax.servlet</groupId>
|
||||
<artifactId>javax.servlet-api</artifactId>
|
||||
<version>4.0.0</version>
|
||||
</dependency>
|
||||
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
|
|
|
@ -0,0 +1,28 @@
|
|||
package com.baeldung.applicationcontext;
|
||||
|
||||
import org.springframework.context.MessageSource;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.context.support.ResourceBundleMessageSource;
|
||||
|
||||
@Configuration
|
||||
public class AccountConfig {
|
||||
|
||||
@Bean
|
||||
public AccountService accountService() {
|
||||
return new AccountService(accountRepository());
|
||||
}
|
||||
|
||||
@Bean
|
||||
public AccountRepository accountRepository() {
|
||||
return new AccountRepository();
|
||||
}
|
||||
|
||||
@Bean
|
||||
public MessageSource messageSource() {
|
||||
ResourceBundleMessageSource messageSource = new ResourceBundleMessageSource();
|
||||
messageSource.setBasename("config/messages");
|
||||
return messageSource;
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,5 @@
|
|||
package com.baeldung.applicationcontext;
|
||||
|
||||
public class AccountRepository {
|
||||
|
||||
}
|
|
@ -0,0 +1,32 @@
|
|||
package com.baeldung.applicationcontext;
|
||||
|
||||
import java.util.Locale;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.context.MessageSource;
|
||||
|
||||
public class AccountService {
|
||||
|
||||
@Autowired
|
||||
private AccountRepository accountRepository;
|
||||
|
||||
@Autowired
|
||||
private MessageSource messageSource;
|
||||
|
||||
public void setAccountRepository(AccountRepository accountRepository) {
|
||||
this.accountRepository = accountRepository;
|
||||
}
|
||||
|
||||
public AccountRepository getAccountRepository() {
|
||||
return accountRepository;
|
||||
}
|
||||
|
||||
public AccountService(AccountRepository accountRepository) {
|
||||
this.accountRepository = accountRepository;
|
||||
}
|
||||
|
||||
public String getAccountName() {
|
||||
return messageSource.getMessage("account.name", null, Locale.ENGLISH);
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,24 @@
|
|||
package com.baeldung.applicationcontext;
|
||||
|
||||
import javax.servlet.ServletContext;
|
||||
import javax.servlet.ServletException;
|
||||
import javax.servlet.ServletRegistration;
|
||||
|
||||
import org.springframework.web.WebApplicationInitializer;
|
||||
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
|
||||
import org.springframework.web.servlet.DispatcherServlet;
|
||||
|
||||
public class MyWebApplicationInitializer implements WebApplicationInitializer {
|
||||
|
||||
public void onStartup(ServletContext container) throws ServletException {
|
||||
AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext();
|
||||
context.register(AccountConfig.class);
|
||||
context.setServletContext(container);
|
||||
|
||||
ServletRegistration.Dynamic servlet = container.addServlet("dispatcher", new DispatcherServlet(context));
|
||||
servlet.setLoadOnStartup(1);
|
||||
|
||||
servlet.addMapping("/");
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,24 @@
|
|||
package com.baeldung.applicationcontext;
|
||||
|
||||
import javax.servlet.ServletContext;
|
||||
import javax.servlet.ServletException;
|
||||
import javax.servlet.ServletRegistration;
|
||||
|
||||
import org.springframework.web.WebApplicationInitializer;
|
||||
import org.springframework.web.context.support.XmlWebApplicationContext;
|
||||
import org.springframework.web.servlet.DispatcherServlet;
|
||||
|
||||
public class MyXmlWebApplicationInitializer implements WebApplicationInitializer {
|
||||
|
||||
public void onStartup(ServletContext container) throws ServletException {
|
||||
XmlWebApplicationContext context = new XmlWebApplicationContext();
|
||||
context.setConfigLocation("/WEB-INF/spring/applicationContext.xml");
|
||||
context.setServletContext(container);
|
||||
|
||||
ServletRegistration.Dynamic servlet = container.addServlet("dispatcher", new DispatcherServlet(context));
|
||||
servlet.setLoadOnStartup(1);
|
||||
|
||||
servlet.addMapping("/");
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,10 @@
|
|||
package com.baeldung.applicationcontext;
|
||||
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@Component
|
||||
public class UserService {
|
||||
|
||||
// user service code
|
||||
|
||||
}
|
|
@ -0,0 +1,71 @@
|
|||
package com.baeldung.applicationcontext;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertNotNull;
|
||||
|
||||
import org.junit.Ignore;
|
||||
import org.junit.Test;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
|
||||
import org.springframework.context.support.ClassPathXmlApplicationContext;
|
||||
import org.springframework.context.support.FileSystemXmlApplicationContext;
|
||||
|
||||
public class ApplicationContextUnitTest {
|
||||
|
||||
@Test
|
||||
public void givenAnnotationConfigAppContext_whenSpringConfig_thenMappingSuccess() {
|
||||
ApplicationContext context = new AnnotationConfigApplicationContext(AccountConfig.class);
|
||||
AccountService accountService = context.getBean(AccountService.class);
|
||||
|
||||
assertNotNull(accountService);
|
||||
assertNotNull(accountService.getAccountRepository());
|
||||
|
||||
((AnnotationConfigApplicationContext) context).close();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenClasspathXmlAppContext_whenAnnotationConfig_thenMappingSuccess() {
|
||||
ApplicationContext context = new ClassPathXmlApplicationContext("applicationcontext/user-bean-config.xml");
|
||||
UserService userService = context.getBean(UserService.class);
|
||||
|
||||
assertNotNull(userService);
|
||||
|
||||
((ClassPathXmlApplicationContext) context).close();
|
||||
}
|
||||
|
||||
@Test
|
||||
@Ignore
|
||||
public void givenFileXmlAppContext_whenXMLConfig_thenMappingSuccess() {
|
||||
String path = "D:/workspaces/Baeldung/tutorials/spring-core-4/src/test/resources/applicationcontext/account-bean-config.xml";
|
||||
|
||||
ApplicationContext context = new FileSystemXmlApplicationContext(path);
|
||||
AccountService accountService = context.getBean("accountService", AccountService.class);
|
||||
|
||||
assertNotNull(accountService);
|
||||
assertNotNull(accountService.getAccountRepository());
|
||||
|
||||
((FileSystemXmlApplicationContext) context).close();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenClasspathXmlAppContext_whenXMLConfig_thenMappingSuccess() {
|
||||
ApplicationContext context = new ClassPathXmlApplicationContext("applicationcontext/account-bean-config.xml");
|
||||
AccountService accountService = context.getBean("accountService", AccountService.class);
|
||||
|
||||
assertNotNull(accountService);
|
||||
assertNotNull(accountService.getAccountRepository());
|
||||
|
||||
((ClassPathXmlApplicationContext) context).close();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenMessagesInFile_whenMessageResourceUsed_thenReadMessage() {
|
||||
ApplicationContext context = new AnnotationConfigApplicationContext(AccountConfig.class);
|
||||
AccountService accountService = context.getBean(AccountService.class);
|
||||
|
||||
assertEquals("TestAccount", accountService.getAccountName());
|
||||
|
||||
((AnnotationConfigApplicationContext) context).close();
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,13 @@
|
|||
<?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.xsd">
|
||||
|
||||
<bean id="accountService" class="com.baeldung.applicationcontext.AccountService">
|
||||
<constructor-arg name="accountRepository" ref="accountRepository" />
|
||||
</bean>
|
||||
|
||||
<bean id="accountRepository" class="com.baeldung.applicationcontext.AccountRepository" />
|
||||
</beans>
|
|
@ -0,0 +1,15 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<beans xmlns="http://www.springframework.org/schema/beans"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xmlns:context="http://www.springframework.org/schema/context"
|
||||
xsi:schemaLocation="
|
||||
http://www.springframework.org/schema/beans
|
||||
http://www.springframework.org/schema/beans/spring-beans.xsd
|
||||
http://www.springframework.org/schema/context
|
||||
http://www.springframework.org/schema/context/spring-context.xsd">
|
||||
|
||||
<context:annotation-config />
|
||||
<context:component-scan
|
||||
base-package="com.baeldung.applicationcontext" />
|
||||
|
||||
</beans>
|
|
@ -0,0 +1 @@
|
|||
account.name=TestAccount
|
|
@ -3,10 +3,8 @@
|
|||
This module contains articles about core Spring functionality
|
||||
|
||||
### Relevant Articles:
|
||||
- [Wiring in Spring: @Autowired, @Resource and @Inject](https://www.baeldung.com/spring-annotations-resource-inject-autowire)
|
||||
- [Constructor Injection in Spring with Lombok](https://www.baeldung.com/spring-injection-lombok)
|
||||
|
||||
- [Introduction to Spring’s StreamUtils](https://www.baeldung.com/spring-stream-utils)
|
||||
- [XML-Based Injection in Spring](https://www.baeldung.com/spring-xml-injection)
|
||||
- [A Quick Guide to the Spring @Lazy Annotation](https://www.baeldung.com/spring-lazy-annotation)
|
||||
- [BeanNameAware and BeanFactoryAware Interfaces in Spring](https://www.baeldung.com/spring-bean-name-factory-aware)
|
||||
- [Access a File from the Classpath in a Spring Application](https://www.baeldung.com/spring-classpath-file-access)
|
||||
|
|
|
@ -0,0 +1,11 @@
|
|||
## Spring Dependency Injection
|
||||
|
||||
This module contains articles about dependency injection with Spring
|
||||
|
||||
### Relevant Articles
|
||||
|
||||
- [Injecting Spring Beans into Unmanaged Objects](https://www.baeldung.com/spring-inject-bean-into-unmanaged-objects)
|
||||
- [Injecting a Value in a Static Field in Spring](https://www.baeldung.com/spring-inject-static-field)
|
||||
- [Spring – Injecting Collections](https://www.baeldung.com/spring-injecting-collections)
|
||||
- [Wiring in Spring: @Autowired, @Resource and @Inject](https://www.baeldung.com/spring-annotations-resource-inject-autowire)
|
||||
- More articles: [[<-- prev]](/spring-di)
|
|
@ -25,11 +25,26 @@
|
|||
<artifactId>spring-boot-starter-data-jpa</artifactId>
|
||||
<version>${spring-boot.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-web</artifactId>
|
||||
<version>${spring-boot.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-aspects</artifactId>
|
||||
<version>${spring.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.projectlombok</groupId>
|
||||
<artifactId>lombok</artifactId>
|
||||
<version>${lombok.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>javax.inject</groupId>
|
||||
<artifactId>javax.inject</artifactId>
|
||||
<version>${javax.inject.version}</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
|
@ -61,5 +76,6 @@
|
|||
<properties>
|
||||
<spring-boot.version>2.3.1.RELEASE</spring-boot.version>
|
||||
<aspectj-plugin.version>1.11</aspectj-plugin.version>
|
||||
<javax.inject.version>1</javax.inject.version>
|
||||
</properties>
|
||||
</project>
|
Some files were not shown because too many files have changed in this diff Show More
Loading…
Reference in New Issue