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 no开发者_开发百科t 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.
This is happening because the smtpPort
property is ambiguous - the getSmtpPort
method returns a String
, but the setSmtpPort
method takes an int
. 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.
精彩评论