Spring - 配置 apache commons 电子邮件
我想在 Spring 应用程序中的 apache commons-email 中使用 HtmlEmail,因此我使用配置 xml,如下所示:
<bean id="commonsEmail" class="org.apache.commons.mail.HtmlEmail">
<property name="hostName" value="smtp.example.com" />
<property name="TLS" value="true"/>
<property name="smtpPort" value="587"/>
</bean>
但由于 smtpPort 属性,我无法初始化它:
Invalid property 'smtpPort' of bean class [org.apache.xml] commons.mail.HtmlEmail]:Bean 属性“smtpPort”不可写或具有无效的 setter 方法。 setter 的参数类型与 getter 的返回类型是否匹配?
请告诉我我做错了什么?谢谢。
I want to use HtmlEmail in apache commons-email in a spring app, so i use the config xml as following:
<bean id="commonsEmail" class="org.apache.commons.mail.HtmlEmail">
<property name="hostName" value="smtp.example.com" />
<property name="TLS" value="true"/>
<property name="smtpPort" value="587"/>
</bean>
But i can't initialize it because of the smtpPort property:
Invalid property 'smtpPort' of bean class [org.apache.commons.mail.HtmlEmail]: Bean property 'smtpPort' is not writable or has an invalid setter method. Does the parameter type of the setter match the return type of the getter?
Please tell me what i've done wrong ? Thank you.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
发生这种情况是因为
smtpPort
属性不明确 -getSmtpPort
方法返回String
,但setSmtpPort
方法采用一个int
。 Spring 在这一点上临阵退缩,并抛出异常,指出 bean 属性无效。我认为
HtmlEmail
和 Spring 都有问题 -HtmlEmail
的 API 设计很糟糕,Spring 则显得不必要的迂腐。我推荐的解决方案是以下之一:
创建您自己的
HtmlEmail
子类,使用新名称定义一个新的 setter 方法,该方法委托给setSmtpPort
。这既快速又简单,但其本身的设计相当糟糕。编写 Spring 的
FactoryBean
接口的实现,它负责实例化和配置HtmlEmail
实例。这比(1)需要更多工作,但设计更简洁。完全放弃 Commons Email,并使用 Spring自己的电子邮件抽象层。这将是我推荐的选项。
This is happening because the
smtpPort
property is ambiguous - thegetSmtpPort
method returns aString
, but thesetSmtpPort
method takes anint
. Spring gets cold feet at this point, and throws the exception saying that the bean property is invalid.I think both
HtmlEmail
and Spring are at fault here -HtmlEmail
for bad API design, Spring for being unnecessarily pedantic.The solution I'd recommend is one of:
Create your own subclass of
HtmlEmail
, defining a new setter method, with a new name, which delegates tosetSmtpPort
. This is quick and easy, but is rather poor design in itself.Write an implementation of Spring's
FactoryBean
interface, which gets the job of instantiating and configuring anHtmlEmail
instance. This is more work than (1), but is a cleaner design.Ditch Commons Email completely, and use Spring's own Email abstraction layer. This would be my recommended option.