1

I'm trying to fetch some properties from application.properties file ad my code is the following:

Main Application class:

package it.mysite;

@SpringBootApplication
@EnableTransactionManagement
public class MailSenderApplication {

    public static void main(String[] args){
        ConfigurableApplicationContext context = SpringApplication.run(MailSenderApplication.class, args);

        System.out.println("*****");
        for (String name : context.getBeanDefinitionNames()) {
            System.out.println(name);
        }
        System.out.println("*****");
        new MySendService().sendReport();
    }
}

My service class:

package it.mysite.service;

@Service
public class MySendService {

    @Value("${mail.fc.to}")
    private String[] to;

    @Value("${mail.fc.subject}")
    private String subject;

    @Autowired ReportService reportEmailService;

    @Autowired MailProperties mailProperties;

    public void sendReport(){
        if(mailProperties.getTo().length > 0) {
    }
}

Class where I fetch the properties:

package it.mysite.properties;

@Component
@ConfigurationProperties("mail.fc")
public class MailProperties {

    private String[] to;

    public String[] getTo(){
        return to;
    }
}

Config file:

# Email config
mail.fc.to=my@mail.com
mail.fc.subject=My subject

All of the @Autowired properties are null, and also the @Value properties (I tried to get them in that way also). When I print my context I can see these classes in the bean list, and for what I know my packages hierarchy is correct, so what can be the problem?

EDIT Ok, I got the suggestion from the duplicate question and I changed my main class code as follows: Main Application class:

package it.mysite;

@SpringBootApplication
@EnableTransactionManagement
public class MailSenderApplication {

    @Autowired MySendService mySendService;

    public static void main(String[] args){
        ConfigurableApplicationContext context = SpringApplication.run(MailSenderApplication.class, args);

        System.out.println("*****");
        for (String name : context.getBeanDefinitionNames()) {
            System.out.println(name);
        }
        System.out.println("*****");
        new MailSenderApplication().boot();
    }

    private void boot(){
        mySendService.sendReport();
    }
}

But I got the same error. Wasn't that the suggestion?

esseara
  • 834
  • 5
  • 27
  • 47
  • 1
    You're still doing the same mistake: using new to create Spring beans. Don't. Get the Spring bean from the Spring `context`. Or better, use a runner: https://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#boot-features-command-line-runner – JB Nizet Nov 23 '18 at 11:17
  • OMG thank you. This pointed me in the right direction. If you write your comment as an answer I'll chose that as the correct one. – esseara Nov 23 '18 at 11:33
  • 1
    @esseara You can't write it as an answer because the question has been closed as a duplicate. Essentially it's the same problem (meaning you can't use the `new` keyword that way). You can go to the other question though and vote for the most useful answer over there. – g00glen00b Nov 23 '18 at 12:00

0 Answers0