bpapa
Dec 7th, 2006, 05:33 PM
After reading Seth Ladd's "Expert Spring MVC and Web Flow" I realized that I have what is called an anemic domain model. So, in my quest to make my codebase less anemic, I have put a lot of key business logic functionality into my domain objects, and away from the service layer.
One example in the book that I actually have in my application is sending out an email to confirm a user registration. So, I have the following in my Member class -
private MailSender mailSender;
public setMailSender(MailSender mailSender) {
this.mailSender = mailSender;
}
public void sendRegistrationConfirmationEmail() {
SimpleMailMessage msg = new SimpleMailMessage();
// set up msg
mailSender.send(msg);
}
Obviously, I need to inject the MailSender before the sendRegistrationConfirmationEmail method is called. Since I am also using the Member class for my command object, I do this in the formBackingObject method of my Controller class -
protected Object formBackingObject(HttpServletRequest request) {
Member member = new Member();
BeanFactory beanFactory = ((ConfigurableApplicationContext)getApplicationCon text()).getBeanFactory();
((AutowireCapableBeanFactory)beanFactory).applyBea nPropertyValues(member,"memberPrototype");
return member;
}
With the following absract definition in applicationContext.xml
<!-- Mail sender implementation -->
<bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderIm pl"/>
<!-- Domain dependencies -->
<bean id="memberPrototype" abstract="true" class="com.myproject.domain.Member">
<property name="mailSender" ref="mailSender"/>
</bean>
Everything works great, but I'm a bit concerned about the code in the formBackingObject. It seems a bit verbose, and I'm wondering if there was a better approach to configuring the MailSender? Are there any pitfalls to my configuration that I might not have thought of?
One example in the book that I actually have in my application is sending out an email to confirm a user registration. So, I have the following in my Member class -
private MailSender mailSender;
public setMailSender(MailSender mailSender) {
this.mailSender = mailSender;
}
public void sendRegistrationConfirmationEmail() {
SimpleMailMessage msg = new SimpleMailMessage();
// set up msg
mailSender.send(msg);
}
Obviously, I need to inject the MailSender before the sendRegistrationConfirmationEmail method is called. Since I am also using the Member class for my command object, I do this in the formBackingObject method of my Controller class -
protected Object formBackingObject(HttpServletRequest request) {
Member member = new Member();
BeanFactory beanFactory = ((ConfigurableApplicationContext)getApplicationCon text()).getBeanFactory();
((AutowireCapableBeanFactory)beanFactory).applyBea nPropertyValues(member,"memberPrototype");
return member;
}
With the following absract definition in applicationContext.xml
<!-- Mail sender implementation -->
<bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderIm pl"/>
<!-- Domain dependencies -->
<bean id="memberPrototype" abstract="true" class="com.myproject.domain.Member">
<property name="mailSender" ref="mailSender"/>
</bean>
Everything works great, but I'm a bit concerned about the code in the formBackingObject. It seems a bit verbose, and I'm wondering if there was a better approach to configuring the MailSender? Are there any pitfalls to my configuration that I might not have thought of?