Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiple configuration properties in spring boot @Autowired one of the properties are null

I am new to spring, I have following boot application classes. I am trying to connect to AWS SQS from Spring boot application. The code is as below:

@SpringBootApplication
@EnableConfigurationProperties ({ApplicationProperties.class, AwsProperties.class})
public class Application{
    private static final Logger logger = LoggerFactory.getLogger(Application.class);    
    public static void main(String[] args) throws IOException {
        SpringApplication.run(Application.class, args);
    }
}

ApplicationProperties.java

@Configuration
@PropertySource("classpath:application.properties")
@ConfigurationProperties(prefix="midb")
public class ApplicationProperties {

    private String keyStore;
    private String keyStorePassword;

// getter and setters
}

AwsProperties.java

@Configuration
@PropertySource("classpath:application.properties")
@ConfigurationProperties(prefix="aws")
public class AwsProperties {

    private String sqsEndpoint;
    private String accessKey;
    private String secretKey;
// getters and setters
}


@Configuration
@EnableJms
@EnableConfigurationProperties(AwsProperties.class)
public class JmsConfig {

    private static final Logger logger = LoggerFactory.getLogger(JmsConfig.class);


    @Autowired
    private AwsProperties awsProperties;

    @Autowired
    private SQSListener sqsListener;
    @PostConstruct
    public void init() {
        //System.out.println("================== " + awsProperties.toString() + "==================");// End point:"+endpoint);
    }

    @Bean
    public AmazonSQSClient createSQSClient() {

        AmazonSQSClient amazonSQSClient = new AmazonSQSClient(new BasicAWSCredentials(awsProperties.getAccessKey(), awsProperties.getSecretKey()));
        amazonSQSClient.setEndpoint(awsProperties.getSqsEndpoint());
        amazonSQSClient.createQueue(awsProperties.getSqsQueueName());
        return amazonSQSClient;
    }

    @Bean
    public DefaultMessageListenerContainer jmsListenerContainer() {
        SQSConnectionFactory sqsConnectionFactory = SQSConnectionFactory.builder()
                .withAWSCredentialsProvider(new DefaultAWSCredentialsProviderChain())
                .withEndpoint(awsProperties.getSqsEndpoint()).withAWSCredentialsProvider(awsCredentialsProvider)
                .withNumberOfMessagesToPrefetch(10).build();
        DefaultMessageListenerContainer dmlc = new DefaultMessageListenerContainer();
        dmlc.setConnectionFactory(sqsConnectionFactory);
        dmlc.setDestinationName(awsProperties.getSqsQueueName());
        dmlc.setMessageListener(sqsListener);
        return dmlc;
    }

    @Bean
    public JmsTemplate createJMSTemplate() {
        SQSConnectionFactory sqsConnectionFactory = SQSConnectionFactory.builder()
                .withAWSCredentialsProvider(awsCredentialsProvider).withEndpoint(awsProperties.getSqsEndpoint())
                .withNumberOfMessagesToPrefetch(10).build();
        JmsTemplate jmsTemplate = new JmsTemplate(sqsConnectionFactory);
        jmsTemplate.setDefaultDestinationName(awsProperties.getSqsQueueName());
        jmsTemplate.setDeliveryPersistent(false);
        return jmsTemplate;
    }

    private final AWSCredentialsProvider awsCredentialsProvider = new AWSCredentialsProvider() {
        @Override
        public AWSCredentials getCredentials() {
            return new BasicAWSCredentials(awsProperties.getAccessKey(), awsProperties.getSecretKey());
        }

        @Override
        public void refresh() {
        }
    };
}

When Maven builds, I get the following error:

Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'createSQSClient' defined in class path resource [io/bigbear/midb/sqs/JmsConfig.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.amazonaws.services.sqs.AmazonSQSClient]: Factory method 'createSQSClient' threw exception; nested exception is java.lang.IllegalArgumentException: Access key cannot be null.

like image 925
user1997772 Avatar asked Mar 16 '18 18:03

user1997772


People also ask

Why is @autowired not working?

When @Autowired doesn't work. There are several reasons @Autowired might not work. When a new instance is created not by Spring but by for example manually calling a constructor, the instance of the class will not be registered in the Spring context and thus not available for dependency injection.

Can configuration class be Autowired?

In addition to being able to reference any particular bean definition as seen above, one @Configuration class may reference the instance of any other @Configuration class using @Autowired . This works because the @Configuration classes themselves are instantiated and managed as individual Spring beans.

Why are my Autowired fields null?

The field annotated @Autowired is null because Spring doesn't know about the copy of MileageFeeCalculator that you created with new and didn't know to autowire it.

What is @configuration in Spring boot?

Spring @Configuration annotation is part of the spring core framework. Spring Configuration annotation indicates that the class has @Bean definition methods. So Spring container can process the class and generate Spring Beans to be used in the application.


Video Answer


1 Answers

I'm not sure but it seems your awsProperties.getAccessKey() returns null.

like image 67
Murat Gündeş Avatar answered Oct 27 '22 03:10

Murat Gündeş