I create a class to manage sending emails and i want to inject smtp config via properties file. But i keep got null on my field properties. This is my code:
public class EmailUtils { 
    @Inject
    @PropertiesFromFile("smtp.properties")
    Properties properties;
    public void sendEmail(String destinator, String subject, String body) {
        final String username = properties.getProperty("smtp.email");
        final String password = properties.getProperty("smtp.password");
        Session session = Session.getInstance(properties,
          new javax.mail.Authenticator() {
            protected PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication(username, password);
            }
          });
        try {
            Message message = new MimeMessage(session);
            message.setFrom(new InternetAddress(properties.getProperty("smtp.from")));
            message.setRecipients(Message.RecipientType.TO,
                InternetAddress.parse(destinator));
            message.setSubject(subject);
            message.setText(body);
            Transport.send(message);
            System.out.println("Done");
        } catch (MessagingException e) {
            throw new RuntimeException(e);
        }
   }
}
public class PropertyReader {
    @Produces
    @PropertiesFromFile
    public Properties provideServerProperties(InjectionPoint ip) {
    //get filename from annotation
    String filename = ip.getAnnotated().getAnnotation(PropertiesFromFile.class).value();
    return readProperties(filename);
}
    private Properties readProperties(String fileInClasspath) {
        InputStream is = this.getClass().getClassLoader().getResourceAsStream(fileInClasspath);
        try {
            Properties properties = new Properties();
            properties.load(is);
            return properties;
       } catch (IOException e) {
           System.err.println("Could not read properties from file " + fileInClasspath + " in classpath. " + e);
       } catch (Exception e) {
           System.err.println("Exception catched:"+ e.getMessage());
       } 
       return null;
   }
}
@Qualifier
@Target({METHOD, FIELD})
@Retention(RUNTIME)
public @interface PropertiesFromFile {
    @Nonbinding
    String value() default "application.properties";
}
I tested the code with a simple Main, but it doesn't work. I tested it with tomcat and still got NPE on Properties object. I missed something ? Please help :)
 
     
    