Skip to content

springboot

6 posts with the tag “springboot”

How to resolve 'plugin org.springframework.boot was not found in any of the following sources exception' when building spring boot applications with gradle

Problem

When we build spring boot applications with gradle, sometimes we get this error :

Terminal window
Build file '/Users/bswen/private/bw/bswen-github/bswen-project/spring-boot-23/build.gradle' line: 2
Plugin [id: 'org.springframework.boot', version: '2.3.2.RELEASE'] was not found in any of the following sources:
* What went wrong:
Plugin [id: 'org.springframework.boot', version: '2.3.2.RELEASE'] was not found in any of the following sources:
- Gradle Core Plugins (plugin is not in 'org.gradle' namespace)
- Plugin Repositories (could not resolve plugin artifact 'org.springframework.boot:org.springframework.boot.gradle.plugin:2.3.2.RELEASE')
Searched in the following repositories:
Gradle Central Plugin Repository

Environments

The environments are as follows:

  • java version: jdk 1.8+
  • spring boot version: 2.3.2.RELEASE+

The build.gradle

The build.gradle of this spring boot application is as follows:

build.gradle
plugins {
id 'org.springframework.boot' version '2.3.2.RELEASE'
id 'io.spring.dependency-management' version '1.0.8.RELEASE'
id 'java'
}
group 'com.bswen'
version '1.0-SNAPSHOT'
sourceCompatibility = 1.8
repositories {
mavenLocal()
maven { url 'https://maven.aliyun.com/repository/google/' }
maven { url 'https://maven.aliyun.com/repository/public/' }
maven { url 'https://maven.aliyun.com/repository/spring/' }
maven { url 'https://maven.aliyun.com/repository/gradle-plugin/' }
maven { url 'https://maven.aliyun.com/repository/spring-plugin/' }
mavenCentral()
}
dependencies {
testCompile group: 'junit', name: 'junit', version: '4.12'
}

How to resolve the problem

This error is caused by the incorrectly configured build.gradle in the project. You should add buildscript block to the build.gradle, this buildscript block is required by the build.gradle script.

According to this post (by Peter Niederwieser):

and this comment by Ashish:

build.gradle and buildscript relationship

So please do as follows:

  1. Open the build.gradle ,add the lines:
build.gradle
buildscript {
repositories {
mavenLocal()
maven { url 'https://maven.aliyun.com/repository/google/' }
maven { url 'https://maven.aliyun.com/repository/public/' }
maven { url 'https://maven.aliyun.com/repository/spring/' }
maven { url 'https://maven.aliyun.com/repository/gradle-plugin/' }
maven { url 'https://maven.aliyun.com/repository/spring-plugin/' }
mavenCentral()
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:2.3.2.RELEASE")
}
}
  1. After the above change, the whole build.gradle file is as follows:
build.gradle
buildscript {
repositories {
mavenLocal()
maven { url 'https://maven.aliyun.com/repository/google/' }
maven { url 'https://maven.aliyun.com/repository/public/' }
maven { url 'https://maven.aliyun.com/repository/spring/' }
maven { url 'https://maven.aliyun.com/repository/gradle-plugin/' }
maven { url 'https://maven.aliyun.com/repository/spring-plugin/' }
mavenCentral()
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:2.3.2.RELEASE")
}
}
plugins {
id 'org.springframework.boot' version '2.3.2.RELEASE'
id 'io.spring.dependency-management' version '1.0.8.RELEASE'
id 'java'
}
group 'com.bswen'
version '1.0-SNAPSHOT'
sourceCompatibility = 1.8
repositories {
mavenLocal()
maven { url 'https://maven.aliyun.com/repository/google/' }
maven { url 'https://maven.aliyun.com/repository/public/' }
maven { url 'https://maven.aliyun.com/repository/spring/' }
maven { url 'https://maven.aliyun.com/repository/gradle-plugin/' }
maven { url 'https://maven.aliyun.com/repository/spring-plugin/' }
mavenCentral()
}
dependencies {
testCompile group: 'junit', name: 'junit', version: '4.12'
}

More about BuildScript

The buildscript block in a build.gradle file is used to configure the build script itself. It allows you to define dependencies and repositories that are needed for the build script to run. This is different from the dependencies and repositories that are needed for the project being built.

Here is an example of a buildscript block:

buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:4.1.0'
}
}

In this example:

  • The repositories block specifies where to find the dependencies needed for the build script. In this case, it is using Maven Central.
  • The dependencies block specifies the dependencies needed for the build script. Here, it includes the Android Gradle plugin.

This configuration is necessary for setting up the environment in which the build script will execute.

The code base of this example is uploaded to github.com, you can click this web url to download the code.

Final Words + More Resources

My intention with this article was to help others who might be considering solving such a problem. So I hope that’s been the case here. If you still have any questions, don’t hesitate to ask me by email: Email me

Here are also the most important links from this article along with some further resources that will help you in this scope:

Oh, and if you found these resources useful, don’t forget to support me by starring the repo on GitHub!

How to resolve IllegalArgumentException Could not resolve placeholder when using @Value in Spring Boot application

1. Introduction

When we read properties from files into Spring Boot applications, Sometimes, you will get exception of reading the properties.

This post will demo how to resolve the IllegalArgumentException:Could not resolve placeholder exception when using @Value in Spring app or Spring Boot apps.

2. Environments

  • Spring Boot

3. The Exception

When we use @Value like this:

@Component
@PropertySource(value={"sysconfig.properties"})
public class SysConfig {
@Value("${myProp}")
private String myProp;
...
}

However, most importantly, if there is no myProp property in sysconfig.properties, we will get this exception:

Terminal window
Caused by: java.lang.IllegalArgumentException: Could not resolve placeholder 'myProp' in value "${myProp}"
at org.springframework.util.PropertyPlaceholderHelper.parseStringValue(PropertyPlaceholderHelper.java:174)
at org.springframework.util.PropertyPlaceholderHelper.replacePlaceholders(PropertyPlaceholderHelper.java:126)
at org.springframework.core.env.AbstractPropertyResolver.doResolvePlaceholders(AbstractPropertyResolver.java:236)
at org.springframework.core.env.AbstractPropertyResolver.resolveRequiredPlaceholders(AbstractPropertyResolver.java:210)
at org.springframework.context.support.PropertySourcesPlaceholderConfigurer$2.resolveStringValue(PropertySourcesPlaceholderConfigurer.java:172)
at org.springframework.beans.factory.support.AbstractBeanFactory.resolveEmbeddedValue(AbstractBeanFactory.java:846)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1087)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1067)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:583)
at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:87)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:364)
... 35 common frames omitted

The key error message Caused by: java.lang.IllegalArgumentException: Could not resolve placeholder 'myProp' in value "${myProp}" indicated that there is no property named myProp in the custom properties file named sysconfig.properties.

4. How to fix it

4.1 Way 1: Add the property key to your properties file

src/main/resources/sysconfig.properties
myProp = test

4.2 Way 2: Provide a default value for the property

If you don’t want to provide the key in your file, you can just set @Value’s default value:

@Component
@PropertySource(value={"sysconfig.properties"})
public class SysConfig {
@Value("${myProp:defaultValue}")
private String myProp;
...
}

You can change the defaultValue to your real default value.

4.3 Way 3: Provide an empty default value for the property

If you want to provide an empty default value, just do like this:

@Component
@PropertySource(value={"sysconfig.properties"})
public class SysConfig {
@Value("${myProp:}")
private String myProp;
...
}

Notice the myProp: , the colon is important.

Just leave empty after the colon, then you would get an empty default value for property myProp.

5. Summary

You can see that @Value must be used carefully, if you get the above exceptions ,just provide the default values of the @Value.

Final Words + More Resources

My intention with this article was to help others who might be considering solving such a problem. So I hope that’s been the case here. If you still have any questions, don’t hesitate to ask me by email: Email me

Here are also the most important links from this article along with some further resources that will help you in this scope:

Oh, and if you found these resources useful, don’t forget to support me by starring the repo on GitHub!

Three simple ways to read properties of spring boot configuration files

1. The purpose of this post

In this post, I will demonstrate three simple ways to read properties files into spring boot applications,including the @Value ,@Configuration and @PropertySource annotations.

2. Environments

  • springboot
  • java 1.8+

3. Way 1: Read from application.properties by @Value annotation

Add these properties to your default configuration file:

src/main/resources/application.properties
info.name=Mike
info.desc=Good boy

Use @Value like this:

src/main/java/com/test/sb1jt/commands/MyCommand.java
package com.test.sb1jt.commands;
import com.test.sb1jt.config.InfoConfig;
import com.test.sb1jt.config.SysConfig;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;
@Component
public class MyCommand implements CommandLineRunner {
private static Logger logger = LoggerFactory.getLogger(MyCommand.class);
@Value("${info.name}") //1.Auto configure name property by reading info.name from application.properties
private String name;
@Value("${info.desc}") //2.Auto configure desc property by reading info.desc from application.properties
private String desc;
@Override
public void run(String... strings) throws Exception {
logger.info("MyCommand run");
logger.info("name is {},desc is {}",name,desc); //3. print the name and desc
}
}

Run the code ,we get this:

Terminal window
2019-05-23 12:45:58.827 INFO 31595 --- [ main] com.test.sb1jt.commands.MyCommand : name is Mike,desc is Good boy

4. Way 2: Read from application.properties by @ConfigurationProperties annotation

Instead of @Value annotion, you can use @ConfigurationProperties, which can read some specific prefixed properties from your application.properties to your class properties.

Add these properties to your spring boot application.properties:

src/main/resources/application.properties
info.name=Mike
info.desc=Good boy

Add a class which use @ConfigurationProperties like this:

src/main/java/com/test/sb1jt/config/InfoConfig.java
package com.test.sb1jt.config;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
@Component
@ConfigurationProperties(prefix = "info")
public class InfoConfig {
private String name; // info.name mapped to this property
private String desc; // info.desc mapped to this property
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getDesc() {
return desc;
}
public void setDesc(String desc) {
this.desc = desc;
}
}

Then print properties like this:

src/main/java/com/test/sb1jt/commands/MyCommand.java
package com.test.sb1jt.commands;
import com.test.sb1jt.config.InfoConfig;
import com.test.sb1jt.config.SysConfig;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;
import com.test.sb1jt.config.InfoConfig;
@Component
public class MyCommand implements CommandLineRunner {
private static Logger logger = LoggerFactory.getLogger(MyCommand.class);
@Autowired
private InfoConfig infoConfig;
@Override
public void run(String... strings) throws Exception {
logger.info("MyCommand run");
logger.info("infoconfig name is {},desc is {}",infoConfig.getName(),
infoConfig.getDesc());
}
}

Run the code ,we get this:

Terminal window
2019-05-23 12:45:58.827 INFO 31595 --- [ main] com.test.sb1jt.commands.MyCommand : infoconfig name is Mike,desc is Good boy

5. Way 3: Read from your custom xxx.properties by @ProperySource annotation

By reading previous sections, you can see that by using @Value or @ConfigurationProperties, it’s easy to read properties in springboot’s default application.properties, but what should we do if we want read properties from a custom properties file?

According to this document,Spring provides @PropertySource annotation to read custom file properties.

Create a new file named sysconfig.properties in src/main/resources/ , add these properties to it:

src/main/resources/sysconfig.properties
sysname = BDS
sysdesc = Big data system

Create a new class to use the @PropertySource like this:

package com.test.sb1jt.config;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;
@Component
@PropertySource(value={"sysconfig.properties"}) //read from your custom src/main/resources/sysconfig.properties
public class SysConfig {
@Value("${sysname}") // mapping from sysname to this property
private String sysName;
@Value("${sysdesc}") // mapping from sysdesc to this property
private String sysDesc;
public String getSysName() {
return sysname;
}
public void setSysName(String sysName) {
this.sysName = sysName;
}
public String getSysDesc() {
return sysDesc;
}
public void setSysDesc(String sysDesc) {
this.sysDesc = sysDesc;
}
}

Print the custom properties like this:

package com.test.sb1jt.commands;
import com.test.sb1jt.config.InfoConfig;
import com.test.sb1jt.config.SysConfig;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;
@Component
public class MyCommand implements CommandLineRunner {
private static Logger logger = LoggerFactory.getLogger(MyCommand.class);
@Autowired
private SysConfig sysConfig; //inject the sysConfig instance
@Override
public void run(String... strings) throws Exception {
logger.info("MyCommand run");
logger.info("sysconfig sysname is {},sysdesc is {}",
sysConfig.getSysName(),sysConfig.getSysDesc());
}
}

Run the code ,we get this:

Terminal window
2019-05-23 12:45:58.827 INFO 31595 --- [ main] com.test.sb1jt.commands.MyCommand : sysconfig sysname is BDS,sysdesc is Big data system

6. Conclusion

You can use @Value ,@Configuration to read default application.properties or use the @PropertySource in your custom properties file in SpringBoot apps.

Final Words + More Resources

My intention with this article was to help others who might be considering solving such a problem. So I hope that’s been the case here. If you still have any questions, don’t hesitate to ask me by email: Email me

Here are also the most important links from this article along with some further resources that will help you in this scope:

Oh, and if you found these resources useful, don’t forget to support me by starring the repo on GitHub!

How to solve Error assembling WAR webxml attribute is required exception when building spring boot wars

1. The purpose of this post

When build springboot app as war file, sometime we encounter this exception:

Terminal window
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-war-plugin:2.2:war (default-war) on project: Error assembling WAR: webxml attribute is required (or pre-existing WEB-INF/web.xml if executing in update mode) -> [Help 1]

The error shows that :

  • it needs a webxml attribute to construct the WAR file
  • Or you should provide a WEB-INF/web.xml to build the WAR file

2. Environments

  • spring boot 1.x and 2.x

3. The solution

3.1 The original pom.xml

Before solve , we can look at the original pom.xml

pom.xml
<build>
<finalName>${artifactId}</finalName>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
<configuration>
<classifier>exec</classifier>
</configuration>
</execution>
</executions>
</plugin>
</build>

We use the spring-boot-maven-plugin to execute the goal repackage with exec classifier.

The classifier, which uses exec here, indicating that the generated jar file will have a -EXEC suffix.

In summary, this configuration uses spring-boot-maven-plugin plug-in to reintegrate the project and generate a executable jar file with the -EXEC suffix.

But, we want to WAR file to be generated, not a executable jar file!

So , we need to change the pom.xml to generate a WAR.

3.2 The right pom.xml

We should add maven-war-plugin to build springboot as a WAR file:

pom.xml
<build>
<finalName>${artifactId}</finalName>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
<configuration>
<classifier>exec</classifier>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>3.0.0</version>
</plugin>
</plugins>
</build>

Please pay attention to the version of the maven-war-plugin, it must be 3.0.0+, or else you must add as follows:

<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>2.6</version>
<configuration>
<failOnMissingWebXml>false</failOnMissingWebXml>
</configuration>
</plugin>

Hope it helps for you.

Final Words + More Resources

My intention with this article was to help others who might be considering solving such problem. So I hope that’s been the case here. If you still have any questions, don’t hesitate to ask me by email: [email protected].

Here are also the most important links from this article along with some further resources that will help you in this scope:

Oh, and if you found these resources useful, don’t forget to support me by starring the repo on GitHub!

How to connect to multiple redis server when using spring boot ?

Purpose

In this article , I will demo how to connect to multiple redis servers using spring boot. Just as following picture shows, if you don’t know how to setup two redis servers in your environment, you can refer to this post.

image-20210305165358100

Environment

  • Spring boot 2+
  • Jdk 8+
  • Redis

Prepare the test data in redis

I deployed two redis docker container bw-redis1 and bw-redis2, now I use command line to set some values in them:

(base) [root@local ~]# docker exec -it bw-redis1 redis-cli
127.0.0.1:6379> set k1 redis1
OK
127.0.0.1:6379> get k1
"redis1"
127.0.0.1:6379> exit
(base) [root@local ~]# docker exec -it bw-redis2 redis-cli
127.0.0.1:6379> set k1 redis2
OK
127.0.0.1:6379> get k1
"redis2"
127.0.0.1:6379> exit
(base) [root@local ~]#

The Project Layout

The layout of this demo project is as follows, all the example code can be found here:

spring-boot-multiredis git:(master) tree .
.
├── pom.xml
├── src
│   ├── main
│   │   ├── java
│   │   │   └── com
│   │   │   └── bswen
│   │   │   └── sbmr
│   │   │   ├── MultiRedisApplication.java
│   │   │   ├── config
│   │   │   │   ├── Redis1Configuration.java
│   │   │   │   ├── Redis1Property.java
│   │   │   │   ├── Redis2Configuration.java
│   │   │   │   ├── Redis2Property.java
│   │   │   │   └── RedisCommonProperty.java
│   │   │   └── service
│   │   │   ├── MultiRedisTestRunner.java
│   │   └── resources
│   │   └── application.properties

The Pom

We use maven to define the dependencies, I excluded the default lettuce sdk, because it’s not stable. I’d rather use the jedis sdk instead.

pom.xml
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>1.8</java.version>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<!-- Import dependency management from Spring Boot -->
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>2.4.2</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<artifactId>spring-boot-multiredis</artifactId>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
<exclusions>
<exclusion>
<groupId>io.lettuce</groupId>
<artifactId>lettuce-core</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>3.3.0</version>
</dependency>
</dependencies>

The code

1. Config file

Here in the src/main/resources/application.properties, I defined two redis server configurations,

src/main/resources/application.properties
spring.redis.host=localhost
spring.redis.port=6379
spring.redis.database=0
spring.redis2.host=localhost
spring.redis2.port=26379
spring.redis2.database=0

Here we define two redis connection, one is localhost:6379, the other is localhost:26379. You should change them to your real configurations.

2. Property config classes

To read the above properties file into spring boot, we must define configuration classes:

  1. At first, we define a common class as the base class of redis configuration classes
src/main/java/com.bswen.sbmr/config/RedisCommonProperty.java
public class RedisCommonProperty {
private String host;
private int port;
private int database;
//getters setters
}
  1. Then, we define Redis1Property and Redis2Property class to denote the different redis configurations
src/main/java/com.bswen.sbmr/config/Redis1Property.java
@Configuration
@ConfigurationProperties(prefix = "spring.redis")
public class Redis1Property extends RedisCommonProperty {
}

And this:

src/main/java/com.bswen.sbmr/config/Redis2Property.java
@Configuration
@ConfigurationProperties(prefix = "spring.redis2")
public class Redis2Property extends RedisCommonProperty {
}

Pay attention to the ConfigurationProperties annotation, we use it to load different properties from the application.properties file.

  1. Thirdly, we should define how to get the RedisTemplate and RedisConnectionFactory from above properties

For redis server1, we define Redis1Configuration:

src/main/java/com.bswen.sbmr/config/Redis1Configuration.java
@Configuration
public class Redis1Configuration {
@Autowired
private Redis1Property redis1Property;
@Primary
@Bean(name = "redis1ConnectionFactory")
public RedisConnectionFactory userRedisConnectionFactory() {
JedisConnectionFactory redisConnectionFactory = new JedisConnectionFactory();
redisConnectionFactory.setHostName(redis1Property.getHost());
redisConnectionFactory.setPort(redis1Property.getPort());
redisConnectionFactory.setDatabase(redis1Property.getDatabase());
return redisConnectionFactory;
}
@Bean(name = "redis1StringRedisTemplate")
public StringRedisTemplate userStringRedisTemplate(@Qualifier("redis1ConnectionFactory") RedisConnectionFactory cf) {
StringRedisTemplate stringRedisTemplate = new StringRedisTemplate();
stringRedisTemplate.setConnectionFactory(cf);
return stringRedisTemplate;
}
@Bean(name = "redis1RedisTemplate")
public RedisTemplate userRedisTemplate(@Qualifier("redis1ConnectionFactory") RedisConnectionFactory cf) {
StringRedisTemplate stringRedisTemplate = new StringRedisTemplate();
stringRedisTemplate.setConnectionFactory(cf);
return stringRedisTemplate;
}
}

To avoid the following error:

***************************
APPLICATION FAILED TO START
***************************
Description:
A component required a bean named 'redisTemplate' that could not be found.

We need to change Redis1Configuration to this:

src/main/java/com.bswen.sbmr/config/Redis1Configuration.java
@Configuration
public class Redis1Configuration {
@Autowired
private Redis1Property redis1Property;
@Primary
@Bean(name = "redis1ConnectionFactory")
public RedisConnectionFactory userRedisConnectionFactory() {
JedisConnectionFactory redisConnectionFactory = new JedisConnectionFactory();
redisConnectionFactory.setHostName(redis1Property.getHost());
redisConnectionFactory.setPort(redis1Property.getPort());
redisConnectionFactory.setDatabase(redis1Property.getDatabase());
return redisConnectionFactory;
}
@Bean(name = "redis1StringRedisTemplate")
public StringRedisTemplate userStringRedisTemplate(@Qualifier("redis1ConnectionFactory") RedisConnectionFactory cf) {
StringRedisTemplate stringRedisTemplate = new StringRedisTemplate();
stringRedisTemplate.setConnectionFactory(cf);
return stringRedisTemplate;
}
@Bean(name = "redis1RedisTemplate")
@Bean(name = {"redis1RedisTemplate","redisTemplate"})
public RedisTemplate userRedisTemplate(@Qualifier("redis1ConnectionFactory") RedisConnectionFactory cf) {
StringRedisTemplate stringRedisTemplate = new StringRedisTemplate();
stringRedisTemplate.setConnectionFactory(cf);
return stringRedisTemplate;
}
}

Because springboot2 needs a default bean named redisTemplate, so we reuse the redis1RedisTemplate bean, add a name redisTemplate to it.

For redis server2, we define Redis2Configuration:

src/main/java/com.bswen.sbmr/config/Redis2Configuration.java
@Configuration
public class Redis2Configuration {
@Autowired
private Redis2Property redis2Property;
@Bean(name = "redis2ConnectionFactory")
public RedisConnectionFactory roleRedisConnectionFactory() {
JedisConnectionFactory redisConnectionFactory = new JedisConnectionFactory();
redisConnectionFactory.setHostName(redis2Property.getHost());
redisConnectionFactory.setPort(redis2Property.getPort());
redisConnectionFactory.setDatabase(redis2Property.getDatabase());
return redisConnectionFactory;
}
@Bean(name = "redis2StringRedisTemplate")
public StringRedisTemplate roleStringRedisTemplate(@Qualifier("redis2ConnectionFactory") RedisConnectionFactory cf) {
StringRedisTemplate stringRedisTemplate = new StringRedisTemplate();
stringRedisTemplate.setConnectionFactory(cf);
return stringRedisTemplate;
}
@Bean(name = "redis2RedisTemplate")
public RedisTemplate roleRedisTemplate(@Qualifier("redis2ConnectionFactory") RedisConnectionFactory cf) {
StringRedisTemplate stringRedisTemplate = new StringRedisTemplate();
stringRedisTemplate.setConnectionFactory(cf);
//setSerializer(stringRedisTemplate);
return stringRedisTemplate;
}
}

Now we have setup the properties and the configurations , we have beans of RedisTemplate , we can test it as follows:

src/main/java/com.bswen.sbmr/service/MultiRedisTestRunner.java
@Component
public class MultiRedisTestRunner implements CommandLineRunner {
private final static Logger logger = LoggerFactory.getLogger(MultiRedisApplication.class);
@Autowired
@Qualifier("redis1StringRedisTemplate")
private StringRedisTemplate stringRedisTemplate;
@Autowired
@Qualifier("redis2StringRedisTemplate")
private StringRedisTemplate stringRedis2Template;
//@Override
public void run(String... strings) throws Exception {
CountDownLatch latch = new CountDownLatch(1);
try {
testRedis1();
testRedis2();
}finally {
latch.await();
}
}
private void testRedis2() {
try {
for (int i = 1; i < 2; i++) {
logger.info("=====================================================================");
logger.info("start loop redis 2:" + i);
String key = "k" + i;
String primaryKeyValue = stringRedis2Template.opsForValue().get(key);
logger.info("=====================================================================");
logger.info(String.format("read from the redis2, key %s value is %s", key, primaryKeyValue));
}
}finally {
}
}
private void testRedis1() {
try {
for (int i = 1; i < 2; i++) {
logger.info("=====================================================================");
logger.info("start loop redis 1:" + i);
String key = "k" + i;
String primaryKeyValue = stringRedisTemplate.opsForValue().get(key);
logger.info("=====================================================================");
logger.info(String.format("read from the redis1, key %s value is %s", key, primaryKeyValue));
}
}finally {
}
}
}

Run it, we get this result:

Terminal window
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v2.4.2)
2024-12-30 18:05:56.099 WARN 49528 --- [ main] o.s.boot.StartupInfoLogger : InetAddress.getLocalHost().getHostName() took 5002 milliseconds to respond. Please verify your network configuration (macOS machines may need to add entries to /etc/hosts).
2024-12-30 18:06:01.116 INFO 49528 --- [ main] com.bswen.sbr2.MultiRedisApplication2 : Starting MultiRedisApplication2 using Java 1.8.0_341 on MacBook-Pro-zzz.local with PID 49528 (/Users/zzz/....../bswen-project/spring-boot-multiredis2/target/classes started by zzz in /Users/zzz/private/bw/bswen-github/bswen-project)
2024-12-30 18:06:01.117 INFO 49528 --- [ main] com.bswen.sbr2.MultiRedisApplication2 : No active profile set, falling back to default profiles: default
2024-12-30 18:06:01.509 INFO 49528 --- [ main] .s.d.r.c.RepositoryConfigurationDelegate : Multiple Spring Data modules found, entering strict repository configuration mode!
2024-12-30 18:06:01.512 INFO 49528 --- [ main] .s.d.r.c.RepositoryConfigurationDelegate : Bootstrapping Spring Data Redis repositories in DEFAULT mode.
2024-12-30 18:06:01.533 INFO 49528 --- [ main] .s.d.r.c.RepositoryConfigurationDelegate : Finished Spring Data repository scanning in 6 ms. Found 0 Redis repository interfaces.
2024-12-30 18:06:02.058 INFO 49528 --- [ main] com.bswen.sbr2.MultiRedisApplication2 : Started MultiRedisApplication2 in 21.403 seconds (JVM running for 21.935)
2024-12-30 18:06:02.060 INFO 49528 --- [ main] c.b.sbr2.service.MultiRedisTestRunner : =====================================================================
2024-12-30 18:06:02.060 INFO 49528 --- [ main] c.b.sbr2.service.MultiRedisTestRunner : start loop redis 1:1
2024-12-30 18:06:02.187 INFO 49528 --- [ main] c.b.sbr2.service.MultiRedisTestRunner : =====================================================================
2024-12-30 18:06:02.188 INFO 49528 --- [ main] c.b.sbr2.service.MultiRedisTestRunner : read from the redis1, key k1 value is redis1
2024-12-30 18:06:02.188 INFO 49528 --- [ main] c.b.sbr2.service.MultiRedisTestRunner : =====================================================================
2024-12-30 18:06:02.188 INFO 49528 --- [ main] c.b.sbr2.service.MultiRedisTestRunner : start loop redis 2:1
2024-12-30 18:06:03.286 INFO 49528 --- [ main] c.b.sbr2.service.MultiRedisTestRunner : =====================================================================
2024-12-30 18:06:03.287 INFO 49528 --- [ main] c.b.sbr2.service.MultiRedisTestRunner : read from the redis2, key k1 value is redis2

It works!

Summary

In this post, I demonstrated how to connect to multiple redis server instances with spring boot 2, the key point is that you should setup specific configuration for each redis server connection. Thanks for your reading. All code of the project is uploaded to github.com, you can download at this link.

Final Words + More Resources

My intention with this article was to help others who might be considering using multiple redis in one spring boot application. So I hope that’s been the case here. If you still have any questions, don’t hesitate to ask me by email: [email protected].

Here are also the most important links from this article along with some further resources that will help you in this scope:

Oh, and if you found these resources useful, don’t forget to support me by starring the repo on GitHub!

⭐️ Thanks For Your Support 🙏

How to solve IllegalArgumentException: Host must not be empty when using springboot data redis and lettuce?

1. Purpose

In this post, I will demo how to solve the below exception or error when using spring boot data redis and lettuce:

Terminal window
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v2.4.2)
2021-03-21 15:27:30.705 INFO 61218 --- [ Test worker] c.bswen.app10.service.TestRedisService : Starting TestRedisService using Java 1.8.0_121 on MBP-bswen with PID 61218 (started by bswen in /Users/bswen/private/bw/bswen-github/bswen-springboot23/app10)
2021-03-21 15:27:30.706 INFO 61218 --- [ Test worker] c.bswen.app10.service.TestRedisService : The following profiles are active: test
2021-03-21 15:27:31.572 INFO 61218 --- [ Test worker] .s.d.r.c.RepositoryConfigurationDelegate : Multiple Spring Data modules found, entering strict repository configuration mode!
2021-03-21 15:27:31.576 INFO 61218 --- [ Test worker] .s.d.r.c.RepositoryConfigurationDelegate : Bootstrapping Spring Data Redis repositories in DEFAULT mode.
2021-03-21 15:27:31.599 INFO 61218 --- [ Test worker] .s.d.r.c.RepositoryConfigurationDelegate : Finished Spring Data repository scanning in 7 ms. Found 0 Redis repository interfaces.
2021-03-21 15:27:32.011 WARN 61218 --- [ Test worker] o.s.w.c.s.GenericWebApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'redisService': Unsatisfied dependency expressed through field 'redisClient'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'redisClient' defined in class path resource [com/bswen/app10/config/RedisConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [io.lettuce.core.RedisClient]: Factory method 'redisClient' threw exception; nested exception is java.lang.IllegalArgumentException: Host must not be empty
2021-03-21 15:27:32.035 INFO 61218 --- [ Test worker] ConditionEvaluationReportLoggingListener :
Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2021-03-21 15:27:32.088 ERROR 61218 --- [ Test worker] o.s.boot.SpringApplication : Application run failed
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'redisService': Unsatisfied dependency expressed through field 'redisClient'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'redisClient' defined in class path resource [com/bswen/app10/config/RedisConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [io.lettuce.core.RedisClient]: Factory method 'redisClient' threw exception; nested exception is java.lang.IllegalArgumentException: Host must not be empty
...
at java.lang.Thread.run(Thread.java:745) ~[na:1.8.0_121]
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'redisClient' defined in class path resource [com/bswen/app10/config/RedisConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [io.lettuce.core.RedisClient]: Factory method 'redisClient' threw exception; nested exception is java.lang.IllegalArgumentException: Host must not be empty
.... org.gradle.internal.concurrent.ThreadFactoryImpl$ManagedThreadRunnable.run(ThreadFactoryImpl.java:56) ~[na:na]
at java.lang.Thread.run(Thread.java:745) ~[na:1.8.0_121]
Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'redisService': Unsatisfied dependency expressed through field 'redisClient'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'redisClient' defined in class path resource [com/bswen/app10/config/RedisConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [io.lettuce.core.RedisClient]: Factory method 'redisClient' threw exception; nested exception is java.lang.IllegalArgumentException: Host must not be empty
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:643) ~[spring-beans-5.3.3.jar:5.3.3]
org.springframework.beans.factory.support.ConstructorResolver.instantiate(ConstructorResolver.java:653) ~[spring-beans-5.3.3.jar:5.3.3]
... 122 common frames omitted
Caused by: java.lang.IllegalArgumentException: Host must not be empty
at io.lettuce.core.internal.LettuceAssert.notEmpty(LettuceAssert.java:44) ~[lettuce-core-6.0.2.RELEASE.jar:6.0.2.RELEASE]
at io.lettuce.core.RedisURI$Builder.redis(RedisURI.java:1151) ~[lettuce-core-6.0.2.RELEASE.jar:6.0.2.RELEASE]
at com.bswen.app10.config.RedisConfiguration.redisClient(RedisConfiguration.java:17) ~[main/:na]
at com.bswen.app10.config.RedisConfiguration$$EnhancerBySpringCGLIB$$fd54fa1a.CGLIB$redisClient$0(<generated>) ~[main/:na]
at com.bswen.app10.config.RedisConfiguration$$EnhancerBySpringCGLIB$$fd54fa1a$$FastClassBySpringCGLIB$$5b3d5194.invoke(<generated>) ~[main/:na]
at org.springframework.cglib.proxy.MethodProxy.invokeSuper(MethodProxy.java:244) ~[spring-core-5.3.3.jar:5.3.3]
at org.springframework.context.annotation.ConfigurationClassEnhancer$BeanMethodInterceptor.intercept(ConfigurationClassEnhancer.java:331) ~[spring-context-5.3.3.jar:5.3.3]
at com.bswen.app10.config.RedisConfiguration$$EnhancerBySpringCGLIB$$fd54fa1a.redisClient(<generated>) ~[main/:na]
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_121]
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_121]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_121]
at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_121]
at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:154) ~[spring-beans-5.3.3.jar:5.3.3]
... 123 common frames omitted
Failed to load ApplicationContext
java.lang.IllegalStateException: Failed to load ApplicationContext
at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContext(DefaultCacheAwareContextLoaderDelegate.java:132)
org.junit.jupiter.engine.descriptor.ClassBasedTestDescriptor.executeAndMaskThrowable(ClassBasedTestDescriptor.java:355)
at org.junit.jupiter.engine.descriptor.ClassBasedTestDescriptor.lambda$invokeTestInstancePostProcessors$7(ClassBasedTestDescriptor.java:350)
at java.util.stream.ReferencePipeline$3$1.accept(ReferencePipeline.java:193)
at java.util.stream.ReferencePipeline$2$1.accept(ReferencePipeline.java:175)
at java.util.ArrayList$ArrayListSpliterator.forEachRemaining(ArrayList.java:1374)
at java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:481)
at java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:471)
at java.util.stream.StreamSpliterators$WrappingSpliterator.forEachRemaining(StreamSpliterators.java:312)
at java.util.stream.Streams$ConcatSpliterator.forEachRemaining(Streams.java:743)
at java.util.stream.Streams$ConcatSpliterator.forEachRemaining(Streams.java:742)
at java.util.stream.ReferencePipeline$Head.forEach(ReferencePipeline.java:580)
at org.junit.jupiter.engine.descriptor.ClassBasedTestDescriptor.invokeTestInstancePostProcessors(ClassBasedTestDescriptor.java:349)
org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$5(NodeTestTask.java:143)
... org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContext(DefaultCacheAwareContextLoaderDelegate.java:124)
... 90 more
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'redisClient' defined in class path resource [com/bswen/app10/config/RedisConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [io.lettuce.core.RedisClient]: Factory method 'redisClient' threw exception; nested exception is java.lang.IllegalArgumentException: Host must not be empty
... 122 more
Caused by: java.lang.IllegalArgumentException: Host must not be empty
at io.lettuce.core.internal.LettuceAssert.notEmpty(LettuceAssert.java:44)
at io.lettuce.core.RedisURI$Builder.redis(RedisURI.java:1151)
at com.bswen.app10.config.RedisConfiguration.redisClient(RedisConfiguration.java:17)
at com.bswen.app10.config.RedisConfiguration$$EnhancerBySpringCGLIB$$fd54fa1a.CGLIB$redisClient$0(<generated>)
at com.bswen.app10.config.RedisConfiguration$$EnhancerBySpringCGLIB$$fd54fa1a$$FastClassBySpringCGLIB$$5b3d5194.invoke(<generated>)
at org.springframework.cglib.proxy.MethodProxy.invokeSuper(MethodProxy.java:244)
at org.springframework.context.annotation.ConfigurationClassEnhancer$BeanMethodInterceptor.intercept(ConfigurationClassEnhancer.java:331)
at com.bswen.app10.config.RedisConfiguration$$EnhancerBySpringCGLIB$$fd54fa1a.redisClient(<generated>)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:154)
... 123 more
TestRedisService > testGetAndSet() FAILED
java.lang.IllegalStateException at DefaultCacheAwareContextLoaderDelegate.java:132
Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException at AutowiredAnnotationBeanPostProcessor.java:643
Caused by: org.springframework.beans.factory.BeanCreationException at ConstructorResolver.java:658
Caused by: org.springframework.beans.BeanInstantiationException at SimpleInstantiationStrategy.java:185
Caused by: java.lang.IllegalArgumentException at LettuceAssert.java:44
1 test completed, 1 failed
> Task :app10:test FAILED
FAILURE: Build failed with an exception.
* What went wrong:
Execution failed for task ':app10:test'.
> There were failing tests. See the results at: file:///Users/bswen/private/bw/bswen-github/bswen-springboot23/app10/build/test-results/test/
* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.
* Get more help at https://help.gradle.org
BUILD FAILED in 8s
6 actionable tasks: 5 executed, 1 up-to-date

The core exception is :

Terminal window
[com/bswen/app10/config/RedisConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [io.lettuce.core.RedisClient]: Factory method 'redisClient' threw exception; nested exception is java.lang.IllegalArgumentException: Host must not be empty
... 122 more
Caused by: java.lang.IllegalArgumentException: Host must not be empty
at io.lettuce.core.internal.LettuceAssert.notEmpty(LettuceAssert.java:44)
at io.lettuce.core.RedisURI$Builder.redis(RedisURI.java:1151)
at com.bswen.app10.config.RedisConfiguration.redisClient(RedisConfiguration.java:17)
at

2. The Environment

  • Jdk 8+
  • Spring boot 2+
  • Gradle 6+

3. The code and solution

3.1 The project structure

The project’s main package is com.bswen.app10, its main purpose is to connect to a redis server and do some unit tests.

└── src
├── main
│   ├── java
│   │   └── com
│   │   └── bswen
│   │   └── app10
│   │   ├── RedisApplication.java
│   │   ├── config
│   │   │   └── RedisConfiguration.java
│   │   ├── domain
│   │   │   └── SyncCommandCallback.java
│   │   └── service
│   │   └── RedisService.java
│   └── resources
│   ├── application-dev.properties
│   └── application.properties
└── test
├── java
│   └── com
│   └── bswen
│   └── app10
│   ├── package-info.java
│   └── service
│   └── TestRedisService.java
└── resources
└── application-test.properties

3.2 The project’s dependency

Let’s see the project’s gradle dependencies:

plugins {
id 'org.springframework.boot' version '2.4.2'
id 'io.spring.dependency-management' version '1.0.11.RELEASE'
id 'java'
}
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-actuator'
implementation 'org.springframework.boot:spring-boot-starter-web'
implementation('org.springframework.boot:spring-boot-starter-data-redis')
implementation 'io.lettuce:lettuce-core'
implementation 'org.apache.commons:commons-pool2'
testImplementation('org.springframework.boot:spring-boot-starter-test') {
// exclude vintage, because we only use junit5
exclude group: 'org.junit.vintage', module: 'junit-vintage-engine'
}
}
test {
// specify the test profile , spring boot would try to load application-test.properties when testing
systemProperty 'spring.profiles.active', 'test'
useJUnitPlatform()
reports {
junitXml.enabled = true
html.enabled = false
}
}

You can see that we are using springboot v2.4.2, and we are using spring-boot-starter-redis with lettuce, which is the default connector. we added lettuce-core as redis connection manager.

3.3 The configuration class

We need a configuration class to configure the redis connection.

package com.bswen.app10.config;
import io.lettuce.core.RedisClient;
import io.lettuce.core.RedisURI;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
@ConfigurationProperties("spring.redis")
public class RedisConfiguration {
private String host;
private int port;
@Bean
RedisClient redisClient() {
RedisURI uri = RedisURI.Builder.redis(this.host, this.port)
.build();
return RedisClient.create(uri);
}
}

In the above code, we define two fields(host and post) to construct the redisURI , which will be used by Lettuce RedisClient to create the redis connection.

3.4 The configuration file

I have specified the test profile when doing unit testing. So, the src/test/resources/application-test.properties file’s content is as follows:

src/test/resources/application-test.properties
spring.redis.host=2.3.4.7
spring.redis.port=6379

3.5 The test class

This is the test code:

package com.bswen.app10.service;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
@SpringBootTest
public class TestRedisService {
@Autowired
private RedisService redisService;
@Test
public void testGetAndSet() {
redisService.set("hello","world");
Assertions.assertEquals(redisService.get("hello"),"world");
}
}

I just put a key/value to redis and then get the value with the same key, the assertion should be true.

3.6 Run the code

When we run the above test with gradle, we get this exception:

Terminal window
[com/bswen/app10/config/RedisConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [io.lettuce.core.RedisClient]: Factory method 'redisClient' threw exception; nested exception is java.lang.IllegalArgumentException: Host must not be empty
... 122 more
Caused by: java.lang.IllegalArgumentException: Host must not be empty
at io.lettuce.core.internal.LettuceAssert.notEmpty(LettuceAssert.java:44)
at io.lettuce.core.RedisURI$Builder.redis(RedisURI.java:1151)

why?

3.7 The reason

The reason for this error is as follows:

  • The lettuce RedisURI$Builder can not find the host of the redis
  • The host is configured in our RedisConfiguration, but the host field does not have getter and setter to access it

3.8 The solution

So the solution is easy, here is it:

3.9 Re-run the test

Terminal window
Testing started at 15:31 ...
> Task :app10:cleanTest
> Task :app10:compileJava
> Task :app10:processResources UP-TO-DATE
> Task :app10:classes
> Task :app10:compileTestJava UP-TO-DATE
> Task :app10:processTestResources UP-TO-DATE
> Task :app10:testClasses UP-TO-DATE
> Task :app10:test
15:31:08.196 [Test worker] DEBUG org.springframework.test.context.BootstrapUtils - Instantiating CacheAwareContextLoaderDelegate from class [org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate]
15:31:08.216 [Test worker] DEBUG org.springframework.test.context.BootstrapUtils - Instantiating BootstrapContext using constructor [public org.springframework.test.context.support.DefaultBootstrapContext(java.lang.Class,org.springframework.test.context.CacheAwareContextLoaderDelegate)]
15:31:08.264 [Test worker] DEBUG org.springframework.test.context.BootstrapUtils - Instantiating TestContextBootstrapper for test class [com.bswen.app10.service.TestRedisService] from class [org.springframework.boot.test.context.SpringBootTestContextBootstrapper]
15:31:08.280 [Test worker] INFO org.springframework.boot.test.context.SpringBootTestContextBootstrapper - Neither @ContextConfiguration nor @ContextHierarchy found for test class [com.bswen.app10.service.TestRedisService], using SpringBootContextLoader
15:31:08.285 [Test worker] DEBUG org.springframework.test.context.support.AbstractContextLoader - Did not detect default resource location for test class [com.bswen.app10.service.TestRedisService]: class path resource [com/bswen/app10/service/TestRedisService-context.xml] does not exist
15:31:08.285 [Test worker] DEBUG org.springframework.test.context.support.AbstractContextLoader - Did not detect default resource location for test class [com.bswen.app10.service.TestRedisService]: class path resource [com/bswen/app10/service/TestRedisServiceContext.groovy] does not exist
15:31:08.286 [Test worker] INFO org.springframework.test.context.support.AbstractContextLoader - Could not detect default resource locations for test class [com.bswen.app10.service.TestRedisService]: no resource found for suffixes {-context.xml, Context.groovy}.
15:31:08.287 [Test worker] INFO org.springframework.test.context.support.AnnotationConfigContextLoaderUtils - Could not detect default configuration classes for test class [com.bswen.app10.service.TestRedisService]: TestRedisService does not declare any static, non-private, non-final, nested classes annotated with @Configuration.
15:31:08.349 [Test worker] DEBUG org.springframework.test.context.support.ActiveProfilesUtils - Could not find an 'annotation declaring class' for annotation type [org.springframework.test.context.ActiveProfiles] and class [com.bswen.app10.service.TestRedisService]
15:31:08.438 [Test worker] DEBUG org.springframework.context.annotation.ClassPathScanningCandidateComponentProvider - Identified candidate component class: file [/Users/bswen/private/bw/bswen-github/bswen-springboot23/app10/build/classes/java/main/com/bswen/app10/RedisApplication.class]
15:31:08.439 [Test worker] INFO org.springframework.boot.test.context.SpringBootTestContextBootstrapper - Found @SpringBootConfiguration com.bswen.app10.RedisApplication for test class com.bswen.app10.service.TestRedisService
15:31:08.577 [Test worker] DEBUG org.springframework.boot.test.context.SpringBootTestContextBootstrapper - @TestExecutionListeners is not present for class [com.bswen.app10.service.TestRedisService]: using defaults.
15:31:08.578 [Test worker] INFO org.springframework.boot.test.context.SpringBootTestContextBootstrapper - Loaded default TestExecutionListener class names from location [META-INF/spring.factories]: [org.springframework.boot.test.autoconfigure.restdocs.RestDocsTestExecutionListener, org.springframework.boot.test.autoconfigure.web.client.MockRestServiceServerResetTestExecutionListener, org.springframework.boot.test.autoconfigure.web.servlet.MockMvcPrintOnlyOnFailureTestExecutionListener, org.springframework.boot.test.autoconfigure.web.servlet.WebDriverTestExecutionListener, org.springframework.boot.test.autoconfigure.webservices.client.MockWebServiceServerTestExecutionListener, org.springframework.boot.test.mock.mockito.MockitoTestExecutionListener, org.springframework.boot.test.mock.mockito.ResetMocksTestExecutionListener, org.springframework.test.context.web.ServletTestExecutionListener, org.springframework.test.context.support.DirtiesContextBeforeModesTestExecutionListener, org.springframework.test.context.event.ApplicationEventsTestExecutionListener, org.springframework.test.context.support.DependencyInjectionTestExecutionListener, org.springframework.test.context.support.DirtiesContextTestExecutionListener, org.springframework.test.context.transaction.TransactionalTestExecutionListener, org.springframework.test.context.jdbc.SqlScriptsTestExecutionListener, org.springframework.test.context.event.EventPublishingTestExecutionListener]
15:31:08.603 [Test worker] INFO org.springframework.boot.test.context.SpringBootTestContextBootstrapper - Using TestExecutionListeners: [org.springframework.test.context.web.ServletTestExecutionListener@55b822bf, org.springframework.test.context.support.DirtiesContextBeforeModesTestExecutionListener@78d3e486, org.springframework.test.context.event.ApplicationEventsTestExecutionListener@13d65ec5, org.springframework.boot.test.mock.mockito.MockitoTestExecutionListener@3f6f6222, org.springframework.boot.test.autoconfigure.SpringBootDependencyInjectionTestExecutionListener@2b5602aa, org.springframework.test.context.support.DirtiesContextTestExecutionListener@4c462492, org.springframework.test.context.transaction.TransactionalTestExecutionListener@6d476ba9, org.springframework.test.context.jdbc.SqlScriptsTestExecutionListener@29fd7a51, org.springframework.test.context.event.EventPublishingTestExecutionListener@32256e96, org.springframework.boot.test.autoconfigure.restdocs.RestDocsTestExecutionListener@4fbaaef4, org.springframework.boot.test.autoconfigure.web.client.MockRestServiceServerResetTestExecutionListener@19769e89, org.springframework.boot.test.autoconfigure.web.servlet.MockMvcPrintOnlyOnFailureTestExecutionListener@5b9b5004, org.springframework.boot.test.autoconfigure.web.servlet.WebDriverTestExecutionListener@4253d43f, org.springframework.boot.test.autoconfigure.webservices.client.MockWebServiceServerTestExecutionListener@58040f8f, org.springframework.boot.test.mock.mockito.ResetMocksTestExecutionListener@75ab50d0]
15:31:08.607 [Test worker] DEBUG org.springframework.test.context.support.AbstractDirtiesContextTestExecutionListener - Before test class: context [DefaultTestContext@1e6a10f3 testClass = TestRedisService, testInstance = [null], testMethod = [null], testException = [null], mergedContextConfiguration = [WebMergedContextConfiguration@1802b0a0 testClass = TestRedisService, locations = '{}', classes = '{class com.bswen.app10.RedisApplication}', contextInitializerClasses = '[]', activeProfiles = '{}', propertySourceLocations = '{}', propertySourceProperties = '{org.springframework.boot.test.context.SpringBootTestContextBootstrapper=true}', contextCustomizers = set[org.springframework.boot.test.autoconfigure.actuate.metrics.MetricsExportContextCustomizerFactory$DisableMetricExportContextCustomizer@1355ba10, org.springframework.boot.test.autoconfigure.properties.PropertyMappingContextCustomizer@0, org.springframework.boot.test.autoconfigure.web.servlet.WebDriverContextCustomizerFactory$Customizer@2e7d7325, org.springframework.boot.test.context.filter.ExcludeFilterContextCustomizer@52bfcb96, org.springframework.boot.test.json.DuplicateJsonObjectContextCustomizerFactory$DuplicateJsonObjectContextCustomizer@ce7e970, org.springframework.boot.test.mock.mockito.MockitoContextCustomizer@0, org.springframework.boot.test.web.client.TestRestTemplateContextCustomizer@25135e5, org.springframework.boot.test.context.SpringBootTestArgs@1, org.springframework.boot.test.context.SpringBootTestWebEnvironment@32d0dfaf], resourceBasePath = 'src/main/webapp', contextLoader = 'org.springframework.boot.test.context.SpringBootContextLoader', parent = [null]], attributes = map['org.springframework.test.context.web.ServletTestExecutionListener.activateListener' -> true]], class annotated with @DirtiesContext [false] with mode [null].
15:31:08.641 [Test worker] DEBUG org.springframework.test.context.support.TestPropertySourceUtils - Adding inlined properties to environment: {spring.jmx.enabled=false, org.springframework.boot.test.context.SpringBootTestContextBootstrapper=true}
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v2.4.2)
2021-03-21 15:31:08.996 INFO 61274 --- [ Test worker] c.bswen.app10.service.TestRedisService : Starting TestRedisService using Java 1.8.0_121 on MBP-bswen with PID 61274 (started by bswen in /Users/bswen/private/bw/bswen-github/bswen-springboot23/app10)
2021-03-21 15:31:08.997 INFO 61274 --- [ Test worker] c.bswen.app10.service.TestRedisService : The following profiles are active: test
2021-03-21 15:31:09.953 INFO 61274 --- [ Test worker] .s.d.r.c.RepositoryConfigurationDelegate : Multiple Spring Data modules found, entering strict repository configuration mode!
2021-03-21 15:31:09.957 INFO 61274 --- [ Test worker] .s.d.r.c.RepositoryConfigurationDelegate : Bootstrapping Spring Data Redis repositories in DEFAULT mode.
2021-03-21 15:31:09.985 INFO 61274 --- [ Test worker] .s.d.r.c.RepositoryConfigurationDelegate : Finished Spring Data repository scanning in 9 ms. Found 0 Redis repository interfaces.
2021-03-21 15:31:12.034 INFO 61274 --- [ Test worker] o.s.s.concurrent.ThreadPoolTaskExecutor : Initializing ExecutorService 'applicationTaskExecutor'
2021-03-21 15:31:13.388 INFO 61274 --- [ Test worker] o.s.b.a.e.web.EndpointLinksResolver : Exposing 2 endpoint(s) beneath base path '/actuator'
2021-03-21 15:31:13.451 INFO 61274 --- [ Test worker] c.bswen.app10.service.TestRedisService : Started TestRedisService in 4.8 seconds (JVM running for 6.238)
2021-03-21 15:31:13.823 INFO 61274 --- [extShutdownHook] o.s.s.concurrent.ThreadPoolTaskExecutor : Shutting down ExecutorService 'applicationTaskExecutor'
BUILD SUCCESSFUL in 8s
6 actionable tasks: 3 executed, 3 up-to-date
15:31:13: Tasks execution finished ':app10:cleanTest :app10:test --tests "com.bswen.app10.service.TestRedisService"'.

It works!

4. Summary

In this post, we demonstrated how to solve the IllegalArgumentException: Host must not be empty problem when using lettuce with springboot data redis, you can see that we should provide appropriate configurations to lettuce to construct the connection from lettuce to redis server. Thanks for your reading. Regards.