Dependency Injection not working [Autowired Annotation]

huangapple go评论66阅读模式
英文:

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(&quot;{secret-key}&quot;) String secretKey){
this.secretKey = secretKey;
}
public String generateHmacKey (String sftpResponseObj){
log.warn(&quot;SFTP response: (&quot; + sftpResponseObj);
String encodingType = &quot;HmacSHA256&quot;;
JSONObject obj = new JSONObject(sftpResponseObj);
log.warn(&quot;Stringified SFTP response: (&quot; + obj.toString());
return &quot;test&quot;;
}
}

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(&quot;{{sftp.host}}:{{sftp.port}}/{{sftp.path}}&quot;)
.username(&quot;{{sftp.user}}&quot;)
.password(&quot;{{sftp.password}}&quot;)
.recursive(false)
.useUserKnownHostsFile(false)
.delay(&quot;{{response.sftp.delay}}&quot;)
.delete(false)
.idempotent(true)
.idempotentRepository(idempotentRepository)
.advanced()
.stepwise(false)
.localWorkDirectory(&quot;{{sftp.tmp-directory}}&quot;))
.setHeader(Exchange.HTTP_METHOD, constant(&quot;POST&quot;))
.setHeader(Exchange.CONTENT_TYPE, constant(&quot;application/json&quot;))
.setHeader(&quot;x-digest&quot;)
.method(hmacgenerator, &quot;generateHmacKey(${body})&quot;)
.log(&quot;Started copying file: ${header.CamelFileName}&quot;)
.to(&quot;{{response.http.url}}/account-opening-response&quot;)
.log(&quot;Done copying file: ${header.CamelFileName}&quot;);
}
}

My config file looks like the following

pom.xml

&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
&lt;project xmlns=&quot;http://maven.apache.org/POM/4.0.0&quot; xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot;
xsi:schemaLocation=&quot;http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd&quot;&gt;
&lt;modelVersion&gt;4.0.0&lt;/modelVersion&gt;
&lt;parent&gt;
&lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
&lt;artifactId&gt;spring-boot-starter-parent&lt;/artifactId&gt;
&lt;version&gt;2.5.3&lt;/version&gt;
&lt;relativePath/&gt; &lt;!-- lookup parent from repository --&gt;
&lt;/parent&gt;
&lt;groupId&gt;com.nestwealth&lt;/groupId&gt;
&lt;artifactId&gt;ftpintegration&lt;/artifactId&gt;
&lt;version&gt;0.0.1-SNAPSHOT&lt;/version&gt;
&lt;name&gt;ftpintegration&lt;/name&gt;
&lt;description&gt;SFTP to S3 Integration&lt;/description&gt;
&lt;properties&gt;
&lt;java.version&gt;11&lt;/java.version&gt;
&lt;/properties&gt;
&lt;dependencies&gt;
&lt;dependency&gt;
&lt;groupId&gt;org.apache.camel.springboot&lt;/groupId&gt;
&lt;artifactId&gt;camel-spring-boot-starter&lt;/artifactId&gt;
&lt;version&gt;3.11.1&lt;/version&gt;
&lt;/dependency&gt;
&lt;dependency&gt;
&lt;groupId&gt;org.apache.camel.springboot&lt;/groupId&gt;
&lt;artifactId&gt;camel-bean-starter&lt;/artifactId&gt;
&lt;version&gt;3.11.1&lt;/version&gt;
&lt;/dependency&gt;
&lt;dependency&gt;
&lt;groupId&gt;org.apache.camel.springboot&lt;/groupId&gt;
&lt;artifactId&gt;camel-ftp-starter&lt;/artifactId&gt;
&lt;version&gt;3.11.1&lt;/version&gt;
&lt;/dependency&gt;
&lt;dependency&gt;
&lt;groupId&gt;org.apache.camel.springboot&lt;/groupId&gt;
&lt;artifactId&gt;camel-aws2-s3-starter&lt;/artifactId&gt;
&lt;version&gt;3.11.1&lt;/version&gt;
&lt;/dependency&gt;
&lt;dependency&gt;
&lt;groupId&gt;org.apache.camel.springboot&lt;/groupId&gt;
&lt;artifactId&gt;camel-http-starter&lt;/artifactId&gt;
&lt;version&gt;3.11.1&lt;/version&gt;
&lt;/dependency&gt;
&lt;dependency&gt;
&lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
&lt;artifactId&gt;spring-boot-starter-jdbc&lt;/artifactId&gt;
&lt;/dependency&gt;
&lt;dependency&gt;
&lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
&lt;artifactId&gt;spring-boot-starter-data-jpa&lt;/artifactId&gt;
&lt;/dependency&gt;
&lt;dependency&gt;
&lt;groupId&gt;com.vladmihalcea&lt;/groupId&gt;
&lt;artifactId&gt;hibernate-types-55&lt;/artifactId&gt;
&lt;version&gt;2.14.0&lt;/version&gt;
&lt;/dependency&gt;
&lt;dependency&gt;
&lt;groupId&gt;org.apache.camel.springboot&lt;/groupId&gt;
&lt;artifactId&gt;camel-jackson-starter&lt;/artifactId&gt;
&lt;version&gt;3.11.1&lt;/version&gt;
&lt;/dependency&gt;
&lt;dependency&gt;
&lt;groupId&gt;org.apache.camel.springboot&lt;/groupId&gt;
&lt;artifactId&gt;camel-jpa-starter&lt;/artifactId&gt;
&lt;version&gt;3.11.1&lt;/version&gt;
&lt;/dependency&gt;
&lt;dependency&gt;
&lt;groupId&gt;org.apache.camel.springboot&lt;/groupId&gt;
&lt;artifactId&gt;camel-sql-starter&lt;/artifactId&gt;
&lt;version&gt;3.11.1&lt;/version&gt;
&lt;/dependency&gt;
&lt;dependency&gt;
&lt;groupId&gt;org.postgresql&lt;/groupId&gt;
&lt;artifactId&gt;postgresql&lt;/artifactId&gt;
&lt;scope&gt;runtime&lt;/scope&gt;
&lt;/dependency&gt;
&lt;dependency&gt;
&lt;groupId&gt;org.apache.camel&lt;/groupId&gt;
&lt;artifactId&gt;camel-endpointdsl&lt;/artifactId&gt;
&lt;version&gt;3.11.1&lt;/version&gt;
&lt;/dependency&gt;
&lt;dependency&gt;
&lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
&lt;artifactId&gt;spring-boot-starter-test&lt;/artifactId&gt;
&lt;scope&gt;test&lt;/scope&gt;
&lt;/dependency&gt;
&lt;dependency&gt;
&lt;groupId&gt;org.json&lt;/groupId&gt;
&lt;artifactId&gt;json&lt;/artifactId&gt;
&lt;version&gt;20220924&lt;/version&gt;
&lt;/dependency&gt;
&lt;/dependencies&gt;
&lt;build&gt;
&lt;plugins&gt;
&lt;plugin&gt;
&lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
&lt;artifactId&gt;spring-boot-maven-plugin&lt;/artifactId&gt;
&lt;/plugin&gt;
&lt;/plugins&gt;
&lt;/build&gt;
&lt;/project&gt;

答案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 = { &quot;com.example.ftpintegration&quot; })
// ...

and check if you have imported HmacGenerator in OnboardingResponseRoute:

import com.example.ftpintegration.HmacGenerator;

huangapple
  • 本文由 发表于 2023年2月24日 11:26:55
  • 转载请务必保留本文链接:https://go.coder-hub.com/75552358.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定