samedi 14 mars 2015

Spring:How to access custom FactoryBean properties in another bean

I am trying to send an activation email to the customer after the registration process. I have created a FactoryBean implementation to retrieve the SMTP Server's emailId and password properties from DB as I dont want to hard-code the email address and password in config files or expose them as properties file.



public class MailPropertiesFactoryBeanRepository extends
AbstractFactoryBean<Properties> {

private JdbcTemplate jdbcTemplate;
private String tableName;
private String emailId;
private String password;

public void setDataSource(DataSource dataSource) {
this.jdbcTemplate = new JdbcTemplate(dataSource) ;
}

public String getTableName() {
return tableName;
}

public void setTableName(String tableName) {
this.tableName = tableName;
}

public String getEmailId() {
return emailId;
}

public void setEmailId(String emailId) {
this.emailId = emailId;
}

public String getPassword() {
return password;
}

public void setPassword(String password) {
this.password = password;
}

public JdbcTemplate getJdbcTemplate() {
return jdbcTemplate;
}

// Create an Instance of Bean Properties where we query
// emailId and password details from the database to send
// emails to the customer.
@Override
protected Properties createInstance() throws Exception {
final Properties props = new Properties();
String sql = "Select " + emailId + "," + password + " from "
+ tableName;
jdbcTemplate.query(sql, new RowCallbackHandler() {

@Override
public void processRow(ResultSet rs) throws SQLException {
props.put(rs.getString(1), rs.getString(2));
}
});
return props;
}

@Override
public Class<?> getObjectType() {
return Properties.class;
}

}


Declaration in spring config file



<!-- Declare Factory Bean to externalize the Mail Properties from Database ->
<beans:bean id="mailProps"
class="com.spring.shopping.repository.MailPropertiesFactoryBeanRepository">
<beans:property name="dataSource" ref="dataSource" />
<beans:property name="tableName" value="eshopper.emaildetails" />
<beans:property name="emailId" value="UserName" />
<beans:property name="password" value="Password" />
</beans:bean>


I am trying to access the emailId and password bean properties in the javaMailSender bean as shown below



<beans:bean id="javaMailSender"
class="org.springframework.mail.javamail.JavaMailSenderImpl">
<beans:property name="host" value="smtp.gmail.com" />
<beans:property name="port" value="985" />
<beans:property name="username"
value="#{mailProps.emailId}" />
<beans:property name="password"
value="#{mailProps.password}" />
<beans:property name="javaMailProperties">
<beans:props>
<beans:prop key="mail.smtp.ssl.trust">
smtp.gmail.com
</beans:prop>
<beans:prop key="mail.smtp.starttls.enable">
true
</beans:prop>
<beans:prop key="mail.smtp.auth">
true
</beans:prop>
</beans:props>
</beans:property>
</beans:bean>


The Service class to send the mail



@Service
public class MailSenderService {

private final String FROM_ID = "info.demoproduct@gmail.com";
@Autowired
private JavaMailSenderImpl javaMailSender;

public void setJavaMailSender(JavaMailSenderImpl javaMailSender) {
this.javaMailSender = javaMailSender;
}

public void sendRegistrationEmail(String emailId,String userName,String text){
SimpleMailMessage simpleMailMessage = new SimpleMailMessage();
simpleMailMessage.setFrom(FROM_ID);
simpleMailMessage.setTo(emailId);
simpleMailMessage.setSubject("Activation Mail for "+userName);
simpleMailMessage.setText(text);
System.out.println(javaMailSender.getUsername());
System.out.println(javaMailSender.getPassword());
javaMailSender.send(simpleMailMessage);
}
}

o/p:
null
null


My problem is I am getting the following exception



org.springframework.mail.MailAuthenticationException: Authentication failed; nested exception is javax.mail.AuthenticationFailedException: failed to connect, no password specified?



The emailId and password are printed as null in the console.


What am I doing wrong? What is the right way to access the FactoryBean properties emailId and password in javaMailSender bean.


P.S I have checked this answer for question Spring: Access bean property from another bean .By debugging I came to know that I am dealing with a HashTable as java.util.Properties extends HashTable,and I dont have any idea how to access hashtable values through SpringEL


Thanks


Aucun commentaire:

Enregistrer un commentaire