Я создаю весеннее приложение с базой данных MySQL. Когда я бегаю нормально, все работает нормально. Но я хочу использовать Docker для приложения.
Когда я запускаю приложение Spring-Boot, я получаю эту ошибку:
09:53:56.898 [main] WARN org.springframework.context.annotation.AnnotationConfigApplicationContext - Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'webConfigurationInterceptor' defined in URL [jar:file:/app.jar!/com/example/favouritePlaceInTheWorld/config/WebConfigurationInterceptor.class]: Unsatisfied dependency expressed through constructor parameter 1; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'ipBlackListInterceptor' defined in URL [jar:file:/app.jar!/com/example/favouritePlaceInTheWorld/web/interceptors/IpBlackListInterceptor.class]: Unsatisfied dependency expressed through constructor parameter 0; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'ipBlockedAddressesServiceImpl' defined in URL [jar:file:/app.jar!/com/example/favouritePlaceInTheWorld/service/impl/IpBlockedAddressesServiceImpl.class]: Unsatisfied dependency expressed through constructor parameter 0; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.example.favouritePlaceInTheWorld.repository.IpBlockedAddressesRepository' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {}
09:53:56.931 [main] ERROR org.springframework.boot.SpringApplication - Application run failed
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'webConfigurationInterceptor' defined in URL [jar:file:/app.jar!/com/example/favouritePlaceInTheWorld/config/WebConfigurationInterceptor.class]: Unsatisfied dependency expressed through constructor parameter 1; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'ipBlackListInterceptor' defined in URL [jar:file:/app.jar!/com/example/favouritePlaceInTheWorld/web/interceptors/IpBlackListInterceptor.class]: Unsatisfied dependency expressed through constructor parameter 0; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'ipBlockedAddressesServiceImpl' defined in URL [jar:file:/app.jar!/com/example/favouritePlaceInTheWorld/service/impl/IpBlockedAddressesServiceImpl.class]: Unsatisfied dependency expressed through constructor parameter 0; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.example.favouritePlaceInTheWorld.repository.IpBlockedAddressesRepository' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {}
at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:800)
at org.springframework.beans.factory.support.ConstructorResolver.autowireConstructor(ConstructorResolver.java:229)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireConstructor(AbstractAutowireCapableBeanFactory.java:1372)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1222)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:582)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:542)
at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:335)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:234)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:333)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:208)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:953)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:918)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:583)
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:740)
at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:415)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:303)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1312)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1301)
at com.example.favouritePlaceInTheWorld.FavouritePlaceApplication.main(FavouritePlaceApplication.java:12)
Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'ipBlackListInterceptor' defined in URL [jar:file:/app.jar!/com/example/favouritePlaceInTheWorld/web/interceptors/IpBlackListInterceptor.class]: Unsatisfied dependency expressed through constructor parameter 0; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'ipBlockedAddressesServiceImpl' defined in URL [jar:file:/app.jar!/com/example/favouritePlaceInTheWorld/service/impl/IpBlockedAddressesServiceImpl.class]: Unsatisfied dependency expressed through constructor parameter 0; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.example.favouritePlaceInTheWorld.repository.IpBlockedAddressesRepository' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {}
at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:800)
at org.springframework.beans.factory.support.ConstructorResolver.autowireConstructor(ConstructorResolver.java:229)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireConstructor(AbstractAutowireCapableBeanFactory.java:1372)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1222)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:582)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:542)
at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:335)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:234)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:333)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:208)
at org.springframework.beans.factory.config.DependencyDescriptor.resolveCandidate(DependencyDescriptor.java:276)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1389)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1309)
at org.springframework.beans.factory.support.ConstructorResolver.resolveAutowiredArgument(ConstructorResolver.java:887)
at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:791)
... 18 common frames omitted
Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'ipBlockedAddressesServiceImpl' defined in URL [jar:file:/app.jar!/com/example/favouritePlaceInTheWorld/service/impl/IpBlockedAddressesServiceImpl.class]: Unsatisfied dependency expressed through constructor parameter 0; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.example.favouritePlaceInTheWorld.repository.IpBlockedAddressesRepository' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {}
at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:800)
at org.springframework.beans.factory.support.ConstructorResolver.autowireConstructor(ConstructorResolver.java:229)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireConstructor(AbstractAutowireCapableBeanFactory.java:1372)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1222)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:582)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:542)
at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:335)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:234)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:333)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:208)
at org.springframework.beans.factory.config.DependencyDescriptor.resolveCandidate(DependencyDescriptor.java:276)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1389)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1309)
at org.springframework.beans.factory.support.ConstructorResolver.resolveAutowiredArgument(ConstructorResolver.java:887)
at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:791)
... 32 common frames omitted
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.example.favouritePlaceInTheWorld.repository.IpBlockedAddressesRepository' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {}
at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoMatchingBeanFound(DefaultListableBeanFactory.java:1799)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1355)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1309)
at org.springframework.beans.factory.support.ConstructorResolver.resolveAutowiredArgument(ConstructorResolver.java:887)
at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:791)
... 46 common frames omitted
Это мой файл Docker:
FROM openjdk:17
ADD target/School.jar app.jar
ENTRYPOINT ["java", "-jar", "app.jar"]
Мое приложение.yml
spring:
datasource:
driverClassName: com.mysql.cj.jdbc.Driver
username: root
password: 12345678
url: jdbc:mysql://localhost:3306/favourite_place_in_the_world?useSSL=false&createDatabaseIfNotExist=true
jpa:
hibernate:
ddl-auto: update
open-in-view: false
properties:
hibernate:
dialect: org.hibernate.dialect.MySQL8Dialect
format_sql: true
mvc:
hiddenmethod:
filter:
enabled: true
#this is for mailHog SMTP server test
mail:
host: "localhost"
port: 1025
username: ${mailHog_username}
password: ${mailHog_password}
#this is for check the time AOP SLO in MilliSeconds 1000 ms = 1s
#ex -> 60 for local. 500 for remote
slos-config:
slos:
-
id: local_operation
threshold: 60
-
id: remote_operation
threshold: 5
#for actuator endpoint
management:
server:
port: 8081
metrics:
enabled: true
export:
prometheus:
enabled: true
endpoint:
web:
base-path: /actuator
prometheus:
enabled: true
endpoints:
jmx:
exposure:
include: "*"
web:
exposure:
include: "*"
info:
env:
enabled: true
#for grafana and prometheus
global:
scrape_interval: 5s
evaluation_interval: 15s
scrape_configs:
- job_name: 'prometheus'
honor_labels: true
metric_path: /actuator/prometheus
scheme: http
static_configs:
- targets: ['localhost:8080']
# data_source=jdbc:mysql://localhost:3306/favourite_place_in_the_world?useSSL\=false&createDatabaseIfNotExist\=true
# mailHog_username=favourite-place@favourite-place.com
Мой файл pom.xml
4.0.0
org.springframework.boot
spring-boot-starter-parent
2.6.7
com.example
School
0.0.1-SNAPSHOT
School
School
17
org.springframework.boot
spring-boot-starter-data-jpa
org.springframework.boot
spring-boot-starter-thymeleaf
org.springframework.boot
spring-boot-starter-validation
org.springframework.boot
spring-boot-starter-web
org.springframework.boot
spring-boot-devtools
runtime
true
mysql
mysql-connector-java
8.0.30
runtime
org.springframework.boot
spring-boot-starter-test
test
org.modelmapper
modelmapper
3.1.0
org.springframework.security
spring-security-core
5.7.1
org.springframework.boot
spring-boot-starter-security
2.7.0
org.thymeleaf.extras
thymeleaf-extras-springsecurity5
org.springframework.boot
spring-boot-starter-mail
2.5.4
org.springframework
spring-aop
5.3.20
org.springframework.boot
spring-boot-configuration-processor
2.7.0
true
org.springframework.security
spring-security-test
5.5.2
test
org.hsqldb
hsqldb
2.4.1
test
org.springframework.boot
spring-boot-starter-actuator
2.6.3
io.micrometer
micrometer-registry-prometheus
1.8.2
org.springframework.boot
spring-boot-maven-plugin
Also i connect docker container with mysql with
create network
docker network create spring-net
docker network connect spring-net mysqldb
I check is work find with - docker container inspect mysqldb
But when i start with -
docker run -p 9000:8080 --name app --net spring-net -e MYSQL_HOST=mysqldb -e MYSQL_USER=root -e MYSQL_PASSWORD=12345678 -e MYSQL_PORT=3306 app
is generate this issue.
This is my configuration file:
@Configuration
public class WebConfigurationInterceptor implements WebMvcConfigurer {
private final StatsInterceptor statsInterceptor;
private final IpBlackListInterceptor ipBlackListInterceptor;
private final MaintenanceInterceptor maintenanceInterceptor;
public WebConfigurationInterceptor(StatsInterceptor statsInterceptor,
IpBlackListInterceptor ipBlackListInterceptor,
MaintenanceInterceptor maintenanceInterceptor) {
this.statsInterceptor = statsInterceptor;
this.ipBlackListInterceptor = ipBlackListInterceptor;
this.maintenanceInterceptor = maintenanceInterceptor;
}
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(statsInterceptor);
registry.addInterceptor(ipBlackListInterceptor);
registry.addInterceptor(maintenanceInterceptor);
}
}
Entity for class
@Entity
@Table(name = "ip_blocked_address")
public class IpBlockedAddress extends BaseEntity{
private String name;
public IpBlockedAddress() {
}
public String getName() {
return name;
}
public IpBlockedAddress setName(String name) {
this.name = name;
return this;
}
}
Service for model entity
public class IpBlockedAddressServiceModel {
@NotBlank
@Size(min = 10)
private String textContent;
public IpBlockedAddressServiceModel() {
}
public String getTextContent() {
return textContent;
}
public IpBlockedAddressServiceModel setTextContent(String textContent) {
this.textContent = textContent;
return this;
}
}
Repository
@Repository
public interface IpBlockedAddressesRepository extends JpaRepository {
Optional findByName(String name);
}
Interface:
public interface IpBlockedAddressesService {
IpBlockedAddress findByName(String name);
void addBlockedIpToDB(IpBlockedAddressServiceModel ipBlockedAddressServiceModel);
}
Implementation of Interface
package com.example.favouritePlaceInTheWorld.service.impl;
import com.example.favouritePlaceInTheWorld.model.entity.IpBlockedAddress;
import com.example.favouritePlaceInTheWorld.model.service.IpBlockedAddressServiceModel;
import com.example.favouritePlaceInTheWorld.repository.IpBlockedAddressesRepository;
import com.example.favouritePlaceInTheWorld.service.IpBlockedAddressesService;
import org.springframework.stereotype.Service;
@Service
public class IpBlockedAddressesServiceImpl implements IpBlockedAddressesService {
private final IpBlockedAddressesRepository ipBlockedAddressesRepository;
public IpBlockedAddressesServiceImpl(IpBlockedAddressesRepository ipBlockedAddressesRepository) {
this.ipBlockedAddressesRepository = ipBlockedAddressesRepository;
}
@Override
public IpBlockedAddress findByName(String name) {
return ipBlockedAddressesRepository
.findByName(name)
.orElse(null);
}
@Override
public void addBlockedIpToDB(IpBlockedAddressServiceModel ipBlockedAddressServiceModel) {
IpBlockedAddress ipBlockedAddress = new IpBlockedAddress();
ipBlockedAddress.setName(ipBlockedAddressServiceModel.getTextContent());
ipBlockedAddressesRepository.save(ipBlockedAddress);
}
}
And Interceptor
package com.example.favouritePlaceInTheWorld.web.interceptors;
import com.example.favouritePlaceInTheWorld.model.entity.IpBlockedAddress;
import com.example.favouritePlaceInTheWorld.service.IpBlockedAddressesService;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@Component
public class IpBlackListInterceptor implements HandlerInterceptor {
private final IpBlockedAddressesService ipBlockedAddressesService;
public IpBlackListInterceptor(IpBlockedAddressesService ipBlockedAddressesService) {
this.ipBlockedAddressesService = ipBlockedAddressesService;
}
/**
* this is method from HandlerInterceptor interface
* request is have method getRemoteAddr()
*/
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
String ipAddress = request.getRemoteAddr();
IpBlockedAddress blockedIp = ipBlockedAddressesService.findByName(ipAddress);
return blockedIp == null;
}
}
Подробнее здесь: https://stackoverflow.com/questions/767 ... -and-mysql
Проблема с запуском Docker с помощью Spring-boot и MySQL. ⇐ JAVA
Программисты JAVA общаются здесь
-
Anonymous
1719245929
Anonymous
Я создаю весеннее приложение с базой данных MySQL. Когда я бегаю нормально, все работает нормально. Но я хочу использовать Docker для приложения.
Когда я запускаю приложение Spring-Boot, я получаю эту ошибку:
09:53:56.898 [main] WARN org.springframework.context.annotation.AnnotationConfigApplicationContext - Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'webConfigurationInterceptor' defined in URL [jar:file:/app.jar!/com/example/favouritePlaceInTheWorld/config/WebConfigurationInterceptor.class]: Unsatisfied dependency expressed through constructor parameter 1; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'ipBlackListInterceptor' defined in URL [jar:file:/app.jar!/com/example/favouritePlaceInTheWorld/web/interceptors/IpBlackListInterceptor.class]: Unsatisfied dependency expressed through constructor parameter 0; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'ipBlockedAddressesServiceImpl' defined in URL [jar:file:/app.jar!/com/example/favouritePlaceInTheWorld/service/impl/IpBlockedAddressesServiceImpl.class]: Unsatisfied dependency expressed through constructor parameter 0; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.example.favouritePlaceInTheWorld.repository.IpBlockedAddressesRepository' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {}
09:53:56.931 [main] ERROR org.springframework.boot.SpringApplication - Application run failed
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'webConfigurationInterceptor' defined in URL [jar:file:/app.jar!/com/example/favouritePlaceInTheWorld/config/WebConfigurationInterceptor.class]: Unsatisfied dependency expressed through constructor parameter 1; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'ipBlackListInterceptor' defined in URL [jar:file:/app.jar!/com/example/favouritePlaceInTheWorld/web/interceptors/IpBlackListInterceptor.class]: Unsatisfied dependency expressed through constructor parameter 0; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'ipBlockedAddressesServiceImpl' defined in URL [jar:file:/app.jar!/com/example/favouritePlaceInTheWorld/service/impl/IpBlockedAddressesServiceImpl.class]: Unsatisfied dependency expressed through constructor parameter 0; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.example.favouritePlaceInTheWorld.repository.IpBlockedAddressesRepository' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {}
at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:800)
at org.springframework.beans.factory.support.ConstructorResolver.autowireConstructor(ConstructorResolver.java:229)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireConstructor(AbstractAutowireCapableBeanFactory.java:1372)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1222)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:582)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:542)
at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:335)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:234)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:333)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:208)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:953)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:918)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:583)
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:740)
at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:415)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:303)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1312)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1301)
at com.example.favouritePlaceInTheWorld.FavouritePlaceApplication.main(FavouritePlaceApplication.java:12)
Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'ipBlackListInterceptor' defined in URL [jar:file:/app.jar!/com/example/favouritePlaceInTheWorld/web/interceptors/IpBlackListInterceptor.class]: Unsatisfied dependency expressed through constructor parameter 0; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'ipBlockedAddressesServiceImpl' defined in URL [jar:file:/app.jar!/com/example/favouritePlaceInTheWorld/service/impl/IpBlockedAddressesServiceImpl.class]: Unsatisfied dependency expressed through constructor parameter 0; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.example.favouritePlaceInTheWorld.repository.IpBlockedAddressesRepository' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {}
at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:800)
at org.springframework.beans.factory.support.ConstructorResolver.autowireConstructor(ConstructorResolver.java:229)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireConstructor(AbstractAutowireCapableBeanFactory.java:1372)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1222)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:582)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:542)
at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:335)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:234)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:333)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:208)
at org.springframework.beans.factory.config.DependencyDescriptor.resolveCandidate(DependencyDescriptor.java:276)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1389)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1309)
at org.springframework.beans.factory.support.ConstructorResolver.resolveAutowiredArgument(ConstructorResolver.java:887)
at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:791)
... 18 common frames omitted
Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'ipBlockedAddressesServiceImpl' defined in URL [jar:file:/app.jar!/com/example/favouritePlaceInTheWorld/service/impl/IpBlockedAddressesServiceImpl.class]: Unsatisfied dependency expressed through constructor parameter 0; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.example.favouritePlaceInTheWorld.repository.IpBlockedAddressesRepository' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {}
at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:800)
at org.springframework.beans.factory.support.ConstructorResolver.autowireConstructor(ConstructorResolver.java:229)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireConstructor(AbstractAutowireCapableBeanFactory.java:1372)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1222)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:582)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:542)
at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:335)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:234)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:333)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:208)
at org.springframework.beans.factory.config.DependencyDescriptor.resolveCandidate(DependencyDescriptor.java:276)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1389)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1309)
at org.springframework.beans.factory.support.ConstructorResolver.resolveAutowiredArgument(ConstructorResolver.java:887)
at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:791)
... 32 common frames omitted
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.example.favouritePlaceInTheWorld.repository.IpBlockedAddressesRepository' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {}
at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoMatchingBeanFound(DefaultListableBeanFactory.java:1799)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1355)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1309)
at org.springframework.beans.factory.support.ConstructorResolver.resolveAutowiredArgument(ConstructorResolver.java:887)
at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:791)
... 46 common frames omitted
Это мой файл Docker:
FROM openjdk:17
ADD target/School.jar app.jar
ENTRYPOINT ["java", "-jar", "app.jar"]
Мое приложение.yml
spring:
datasource:
driverClassName: com.mysql.cj.jdbc.Driver
username: root
password: 12345678
url: jdbc:mysql://localhost:3306/favourite_place_in_the_world?useSSL=false&createDatabaseIfNotExist=true
jpa:
hibernate:
ddl-auto: update
open-in-view: false
properties:
hibernate:
dialect: org.hibernate.dialect.MySQL8Dialect
format_sql: true
mvc:
hiddenmethod:
filter:
enabled: true
#this is for mailHog SMTP server test
mail:
host: "localhost"
port: 1025
username: ${mailHog_username}
password: ${mailHog_password}
#this is for check the time AOP SLO in MilliSeconds 1000 ms = 1s
#ex -> 60 for local. 500 for remote
slos-config:
slos:
-
id: local_operation
threshold: 60
-
id: remote_operation
threshold: 5
#for actuator endpoint
management:
server:
port: 8081
metrics:
enabled: true
export:
prometheus:
enabled: true
endpoint:
web:
base-path: /actuator
prometheus:
enabled: true
endpoints:
jmx:
exposure:
include: "*"
web:
exposure:
include: "*"
info:
env:
enabled: true
#for grafana and prometheus
global:
scrape_interval: 5s
evaluation_interval: 15s
scrape_configs:
- job_name: 'prometheus'
honor_labels: true
metric_path: /actuator/prometheus
scheme: http
static_configs:
- targets: ['localhost:8080']
# data_source=jdbc:mysql://localhost:3306/favourite_place_in_the_world?useSSL\=false&createDatabaseIfNotExist\=true
# mailHog_username=favourite-place@favourite-place.com
Мой файл pom.xml
4.0.0
org.springframework.boot
spring-boot-starter-parent
2.6.7
com.example
School
0.0.1-SNAPSHOT
School
School
17
org.springframework.boot
spring-boot-starter-data-jpa
org.springframework.boot
spring-boot-starter-thymeleaf
org.springframework.boot
spring-boot-starter-validation
org.springframework.boot
spring-boot-starter-web
org.springframework.boot
spring-boot-devtools
runtime
true
mysql
mysql-connector-java
8.0.30
runtime
org.springframework.boot
spring-boot-starter-test
test
org.modelmapper
modelmapper
3.1.0
org.springframework.security
spring-security-core
5.7.1
org.springframework.boot
spring-boot-starter-security
2.7.0
org.thymeleaf.extras
thymeleaf-extras-springsecurity5
org.springframework.boot
spring-boot-starter-mail
2.5.4
org.springframework
spring-aop
5.3.20
org.springframework.boot
spring-boot-configuration-processor
2.7.0
true
org.springframework.security
spring-security-test
5.5.2
test
org.hsqldb
hsqldb
2.4.1
test
org.springframework.boot
spring-boot-starter-actuator
2.6.3
io.micrometer
micrometer-registry-prometheus
1.8.2
org.springframework.boot
spring-boot-maven-plugin
Also i connect docker container with mysql with
create network
docker network create spring-net
docker network connect spring-net mysqldb
I check is work find with - docker container inspect mysqldb
But when i start with -
docker run -p 9000:8080 --name app --net spring-net -e MYSQL_HOST=mysqldb -e MYSQL_USER=root -e MYSQL_PASSWORD=12345678 -e MYSQL_PORT=3306 app
is generate this issue.
This is my configuration file:
@Configuration
public class WebConfigurationInterceptor implements WebMvcConfigurer {
private final StatsInterceptor statsInterceptor;
private final IpBlackListInterceptor ipBlackListInterceptor;
private final MaintenanceInterceptor maintenanceInterceptor;
public WebConfigurationInterceptor(StatsInterceptor statsInterceptor,
IpBlackListInterceptor ipBlackListInterceptor,
MaintenanceInterceptor maintenanceInterceptor) {
this.statsInterceptor = statsInterceptor;
this.ipBlackListInterceptor = ipBlackListInterceptor;
this.maintenanceInterceptor = maintenanceInterceptor;
}
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(statsInterceptor);
registry.addInterceptor(ipBlackListInterceptor);
registry.addInterceptor(maintenanceInterceptor);
}
}
Entity for class
@Entity
@Table(name = "ip_blocked_address")
public class IpBlockedAddress extends BaseEntity{
private String name;
public IpBlockedAddress() {
}
public String getName() {
return name;
}
public IpBlockedAddress setName(String name) {
this.name = name;
return this;
}
}
Service for model entity
public class IpBlockedAddressServiceModel {
@NotBlank
@Size(min = 10)
private String textContent;
public IpBlockedAddressServiceModel() {
}
public String getTextContent() {
return textContent;
}
public IpBlockedAddressServiceModel setTextContent(String textContent) {
this.textContent = textContent;
return this;
}
}
Repository
@Repository
public interface IpBlockedAddressesRepository extends JpaRepository {
Optional findByName(String name);
}
Interface:
public interface IpBlockedAddressesService {
IpBlockedAddress findByName(String name);
void addBlockedIpToDB(IpBlockedAddressServiceModel ipBlockedAddressServiceModel);
}
Implementation of Interface
package com.example.favouritePlaceInTheWorld.service.impl;
import com.example.favouritePlaceInTheWorld.model.entity.IpBlockedAddress;
import com.example.favouritePlaceInTheWorld.model.service.IpBlockedAddressServiceModel;
import com.example.favouritePlaceInTheWorld.repository.IpBlockedAddressesRepository;
import com.example.favouritePlaceInTheWorld.service.IpBlockedAddressesService;
import org.springframework.stereotype.Service;
@Service
public class IpBlockedAddressesServiceImpl implements IpBlockedAddressesService {
private final IpBlockedAddressesRepository ipBlockedAddressesRepository;
public IpBlockedAddressesServiceImpl(IpBlockedAddressesRepository ipBlockedAddressesRepository) {
this.ipBlockedAddressesRepository = ipBlockedAddressesRepository;
}
@Override
public IpBlockedAddress findByName(String name) {
return ipBlockedAddressesRepository
.findByName(name)
.orElse(null);
}
@Override
public void addBlockedIpToDB(IpBlockedAddressServiceModel ipBlockedAddressServiceModel) {
IpBlockedAddress ipBlockedAddress = new IpBlockedAddress();
ipBlockedAddress.setName(ipBlockedAddressServiceModel.getTextContent());
ipBlockedAddressesRepository.save(ipBlockedAddress);
}
}
And Interceptor
package com.example.favouritePlaceInTheWorld.web.interceptors;
import com.example.favouritePlaceInTheWorld.model.entity.IpBlockedAddress;
import com.example.favouritePlaceInTheWorld.service.IpBlockedAddressesService;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@Component
public class IpBlackListInterceptor implements HandlerInterceptor {
private final IpBlockedAddressesService ipBlockedAddressesService;
public IpBlackListInterceptor(IpBlockedAddressesService ipBlockedAddressesService) {
this.ipBlockedAddressesService = ipBlockedAddressesService;
}
/**
* this is method from HandlerInterceptor interface
* request is have method getRemoteAddr()
*/
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
String ipAddress = request.getRemoteAddr();
IpBlockedAddress blockedIp = ipBlockedAddressesService.findByName(ipAddress);
return blockedIp == null;
}
}
Подробнее здесь: [url]https://stackoverflow.com/questions/76747674/issue-launching-the-docker-with-spring-boot-and-mysql[/url]
Ответить
1 сообщение
• Страница 1 из 1
Перейти
- Кемерово-IT
- ↳ Javascript
- ↳ C#
- ↳ JAVA
- ↳ Elasticsearch aggregation
- ↳ Python
- ↳ Php
- ↳ Android
- ↳ Html
- ↳ Jquery
- ↳ C++
- ↳ IOS
- ↳ CSS
- ↳ Excel
- ↳ Linux
- ↳ Apache
- ↳ MySql
- Детский мир
- Для души
- ↳ Музыкальные инструменты даром
- ↳ Печатная продукция даром
- Внешняя красота и здоровье
- ↳ Одежда и обувь для взрослых даром
- ↳ Товары для здоровья
- ↳ Физкультура и спорт
- Техника - даром!
- ↳ Автомобилистам
- ↳ Компьютерная техника
- ↳ Плиты: газовые и электрические
- ↳ Холодильники
- ↳ Стиральные машины
- ↳ Телевизоры
- ↳ Телефоны, смартфоны, плашеты
- ↳ Швейные машинки
- ↳ Прочая электроника и техника
- ↳ Фототехника
- Ремонт и интерьер
- ↳ Стройматериалы, инструмент
- ↳ Мебель и предметы интерьера даром
- ↳ Cантехника
- Другие темы
- ↳ Разное даром
- ↳ Давай меняться!
- ↳ Отдам\возьму за копеечку
- ↳ Работа и подработка в Кемерове
- ↳ Давай с тобой поговорим...
Мобильная версия