英文:
Dependency Injection not working [Autowired Annotation]
问题
以下是代码的翻译:
#9 29.22 [ERROR] /src/main/java/com/nestwealth/ftpintegration/onboarding/response/OnboardingResponseRoute.java:[15,16] cannot find symbol
#9 29.22 [ERROR] symbol: class HmacGenerator
#9 29.22 [ERROR] location: class com.nestwealth.ftpintegration.onboarding.response.OnboardingResponseRoute
package com.example.ftpintegration;
import java.io.UnsupportedEncodingException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.crypto.Mac;
import javax crypto.spec SecretKeySpec;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.json JSONObject;
@Component
public class HmacGenerator {
private String secretKey;
public HmacGenerator(@Value("{secret-key}") String secretKey){
this.secretKey = secretKey;
}
public String generateHmacKey (String sftpResponseObj){
log.warn("SFTP response: (" + sftpResponseObj);
String encodingType = "HmacSHA256";
JSONObject obj = new JSONObject(sftpResponseObj);
log.warn("Stringified SFTP response: (" + obj.toString());
return "test";
}
}
package com.example.ftpintegration.onboarding.response;
import org.apache.camel Exchange;
import org.apache.camel.builder.endpoint.EndpointRouteBuilder;
import org.apache.camel.processor.idempotent.jdbc.JdbcMessageIdRepository;
import org.springframework.stereotype.Component;
import org.springframework.beans.factory.annotation.Autowired;
@Component
public class OnboardingResponseRoute extends EndpointRouteBuilder {
private JdbcMessageIdRepository idempotentRepository;
@Autowired
public HmacGenerator hmacgenerator; //fails right here
public OnboardingResponseRoute(JdbcMessageIdRepository sftpOnboardingProcessor) {
this.idempotentRepository = sftpOnboardingProcessor;
}
@Override
public void configure() throws Exception {
from(sftp("{{sftp.host}}:{{sftp.port}}/{{sftp.path}}")
.username("{{sftp.user}}")
.password("{{sftp.password}}")
.recursive(false)
.useUserKnownHostsFile(false)
.delay("{{response.sftp.delay}}")
.delete(false)
.idempotent(true)
.idempotentRepository(idempotentRepository)
.advanced()
.stepwise(false)
.localWorkDirectory("{{sftp.tmp-directory}}"))
.setHeader(Exchange.HTTP_METHOD, constant("POST"))
.setHeader(Exchange.CONTENT_TYPE, constant("application/json"))
.setHeader("x-digest")
.method(hmacgenerator, "generateHmacKey(${body})")
.log("Started copying file: ${header.CamelFileName}")
.to("{{response.http.url}}/account-opening-response")
.log("Done copying file: ${header.CamelFileName}");
}
}
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.5.3</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.nestwealth</groupId>
<artifactId>ftpintegration</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>ftpintegration</name>
<description>SFTP to S3 Integration</description>
<properties>
<java.version>11</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.apache.camel.springboot</groupId>
<artifactId>camel-spring-boot-starter</artifactId>
<version>3.11.1</version>
</dependency>
<dependency>
<groupId>org.apache.camel.springboot</groupId>
<artifactId>camel-bean-starter</artifactId>
<version>3.11.1</version>
</dependency>
<dependency>
<groupId>org.apache.camel.springboot</groupId>
<artifactId>camel-ftp-starter</artifactId>
<version>3.11.1</version>
</dependency>
<dependency>
<groupId>org.apache.camel.springboot</groupId>
<artifactId>camel-aws2-s3-starter</artifactId>
<version>3.11.1</version>
</dependency>
<dependency>
<groupId>org.apache.camel.springboot</groupId>
<artifactId>camel-http-starter</artifactId>
<version>3.11.1</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>com.vladmihalcea</groupId>
<artifactId>hibernate-types-55</artifactId>
<version>2.14.0</version>
</dependency>
<dependency>
<groupId>org.apache.camel.springboot</groupId>
<artifactId>camel-jackson-starter</artifactId>
<version>3.11.1</version>
</dependency>
<dependency>
<groupId>org.apache.camel.springboot</groupId>
<artifactId>camel-jpa-starter</artifactId>
<version>3.11.1</version>
</dependency>
<dependency>
<groupId>org.apache.camel.springboot</groupId>
<artifactId>camel-sql-starter</artifactId>
<version>3.11.1</version>
</dependency>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-endpointdsl</artifactId>
<version>3.11.1</version>
</dependency
<details>
<summary>英文:</summary>
I am not sure why i am running into the following error
```#9 29.22 [ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.1:compile (default-compile) on project ftpintegration: Compilation failure: Compilation failure:
#9 29.22 [ERROR] /src/main/java/com/nestwealth/ftpintegration/onboarding/response/OnboardingResponseRoute.java:[15,16] cannot find symbol
#9 29.22 [ERROR] symbol: class HmacGenerator
#9 29.22 [ERROR] location: class com.nestwealth.ftpintegration.onboarding.response.OnboardingResponseRoute
I have class declared that needs to used in multiple places, which is defined as following
HmacGenerator.java
package com.example.ftpintegration;
import java.io.UnsupportedEncodingException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.json.JSONObject;
@Component
public class HmacGenerator {
private String secretKey;
public HmacGenerator(@Value("{secret-key}") String secretKey){
this.secretKey = secretKey;
}
public String generateHmacKey (String sftpResponseObj){
log.warn("SFTP response: (" + sftpResponseObj);
String encodingType = "HmacSHA256";
JSONObject obj = new JSONObject(sftpResponseObj);
log.warn("Stringified SFTP response: (" + obj.toString());
return "test";
}
}
I am trying to consume the above method generateHmacKey(...)
in the following class
OnboardingResponseRoute.java
package com.example.ftpintegration.onboarding.response;
import org.apache.camel.Exchange;
import org.apache.camel.builder.endpoint.EndpointRouteBuilder;
import org.apache.camel.processor.idempotent.jdbc.JdbcMessageIdRepository;
import org.springframework.stereotype.Component;
import org.springframework.beans.factory.annotation.Autowired;
@Component
public class OnboardingResponseRoute extends EndpointRouteBuilder {
private JdbcMessageIdRepository idempotentRepository;
@Autowired
public HmacGenerator hmacgenerator; //fails right here
public OnboardingResponseRoute(JdbcMessageIdRepository sftpOnboardingProcessor) {
this.idempotentRepository = sftpOnboardingProcessor;
}
@Override
public void configure() throws Exception {
from(sftp("{{sftp.host}}:{{sftp.port}}/{{sftp.path}}")
.username("{{sftp.user}}")
.password("{{sftp.password}}")
.recursive(false)
.useUserKnownHostsFile(false)
.delay("{{response.sftp.delay}}")
.delete(false)
.idempotent(true)
.idempotentRepository(idempotentRepository)
.advanced()
.stepwise(false)
.localWorkDirectory("{{sftp.tmp-directory}}"))
.setHeader(Exchange.HTTP_METHOD, constant("POST"))
.setHeader(Exchange.CONTENT_TYPE, constant("application/json"))
.setHeader("x-digest")
.method(hmacgenerator, "generateHmacKey(${body})")
.log("Started copying file: ${header.CamelFileName}")
.to("{{response.http.url}}/account-opening-response")
.log("Done copying file: ${header.CamelFileName}");
}
}
My config file looks like the following
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.5.3</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.nestwealth</groupId>
<artifactId>ftpintegration</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>ftpintegration</name>
<description>SFTP to S3 Integration</description>
<properties>
<java.version>11</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.apache.camel.springboot</groupId>
<artifactId>camel-spring-boot-starter</artifactId>
<version>3.11.1</version>
</dependency>
<dependency>
<groupId>org.apache.camel.springboot</groupId>
<artifactId>camel-bean-starter</artifactId>
<version>3.11.1</version>
</dependency>
<dependency>
<groupId>org.apache.camel.springboot</groupId>
<artifactId>camel-ftp-starter</artifactId>
<version>3.11.1</version>
</dependency>
<dependency>
<groupId>org.apache.camel.springboot</groupId>
<artifactId>camel-aws2-s3-starter</artifactId>
<version>3.11.1</version>
</dependency>
<dependency>
<groupId>org.apache.camel.springboot</groupId>
<artifactId>camel-http-starter</artifactId>
<version>3.11.1</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>com.vladmihalcea</groupId>
<artifactId>hibernate-types-55</artifactId>
<version>2.14.0</version>
</dependency>
<dependency>
<groupId>org.apache.camel.springboot</groupId>
<artifactId>camel-jackson-starter</artifactId>
<version>3.11.1</version>
</dependency>
<dependency>
<groupId>org.apache.camel.springboot</groupId>
<artifactId>camel-jpa-starter</artifactId>
<version>3.11.1</version>
</dependency>
<dependency>
<groupId>org.apache.camel.springboot</groupId>
<artifactId>camel-sql-starter</artifactId>
<version>3.11.1</version>
</dependency>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-endpointdsl</artifactId>
<version>3.11.1</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<version>20220924</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
答案1
得分: 1
请检查是否在您的主应用程序文件中添加了@ComponentScan
:
@SpringBootApplication
@ComponentScan(basePackages = { "com.example.ftpintegration" })
// ...
并且检查是否已在OnboardingResponseRoute
中导入了HmacGenerator
:
import com.example.ftpintegration.HmacGenerator;
英文:
please check if you added @ComponentScan
in your main application file
@SpringBootApplication
@ComponentScan(basePackages = { "com.example.ftpintegration" })
// ...
and check if you have imported HmacGenerator
in OnboardingResponseRoute
:
import com.example.ftpintegration.HmacGenerator;
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论