@Inject 属性 returns 在 Tomcat 上为空
@Inject properties returns null on Tomcat
我创建了一个 class 来管理发送电子邮件,我想通过属性文件注入 smtp 配置。但是我的字段属性一直为空。
这是我的代码:
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";
}
我用一个简单的 Main 测试了代码,但它不起作用。
我用 tomcat 对其进行了测试,但在 Properties 对象上仍然出现 NPE。
我错过了什么?
请帮助:)
请考虑这样做Java EE 方式:
如果使用 Tomcat,请确保您已按照 How to install and use CDI on Tomcat? or Application servers and environments supported by Weld.
中所述使用 CDI 实施对其进行设置
将 JavaMail 实现 jar 从应用程序的 WEB-INF/lib
移动到 Tomcat lib
目录。
通过将以下内容添加到 config/Context.xml
文件中,在 Tomcat 中配置您的电子邮件会话:
<Context>
...
<Resource name="mail/Session" auth="Container"
type="javax.mail.Session"
mail.smtp.host="your mail host here"
mail.smtp.user="your user name here"
password="your password"
mail.from="noreply@yourdomain.com" />
...
</Context>
还有其他地方可以放置此配置,但这里是最简单的解释。请参阅 Tomcat JNDI documentation 了解更多信息。
简化您的代码:
@Resource(name="mail/Session")
private Session session;
public void sendEmail(String destinator, String subject, String body) {
try {
Message message = new MimeMessage(session);
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);
}
}
您的服务器(在本例中为 Tomcat)负责为您配置邮件会话和身份验证。
类似的机制适用于设置 JDBC DataSource 对象 FWIW。
用 @Named
和 @ApplicationScoped
注释您的 PropertyReader
class 以使 CDI 容器知道它以便对其进行管理。在你的 class 路径中有一个空白 beans.xml
。
我创建了一个 class 来管理发送电子邮件,我想通过属性文件注入 smtp 配置。但是我的字段属性一直为空。 这是我的代码:
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";
}
我用一个简单的 Main 测试了代码,但它不起作用。 我用 tomcat 对其进行了测试,但在 Properties 对象上仍然出现 NPE。 我错过了什么? 请帮助:)
请考虑这样做Java EE 方式:
如果使用 Tomcat,请确保您已按照 How to install and use CDI on Tomcat? or Application servers and environments supported by Weld.
中所述使用 CDI 实施对其进行设置
将 JavaMail 实现 jar 从应用程序的
WEB-INF/lib
移动到 Tomcatlib
目录。通过将以下内容添加到
config/Context.xml
文件中,在 Tomcat 中配置您的电子邮件会话:<Context> ... <Resource name="mail/Session" auth="Container" type="javax.mail.Session" mail.smtp.host="your mail host here" mail.smtp.user="your user name here" password="your password" mail.from="noreply@yourdomain.com" /> ... </Context>
还有其他地方可以放置此配置,但这里是最简单的解释。请参阅 Tomcat JNDI documentation 了解更多信息。
简化您的代码:
@Resource(name="mail/Session") private Session session; public void sendEmail(String destinator, String subject, String body) { try { Message message = new MimeMessage(session); 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); } }
您的服务器(在本例中为 Tomcat)负责为您配置邮件会话和身份验证。
类似的机制适用于设置 JDBC DataSource 对象 FWIW。
用 @Named
和 @ApplicationScoped
注释您的 PropertyReader
class 以使 CDI 容器知道它以便对其进行管理。在你的 class 路径中有一个空白 beans.xml
。