Reputation: 1717
I have an application made up of 2 projects - UI and data. In the Data project, I have added a spring bean to the xml application context:
<bean id="mail-notification-service" class="com.test.DefaultEmailNotificationManager">
</bean>
This manager sends out notifications on request, and the parameters use a simple enum and a parameters object (both of which use classes only in the data project) to select an IEmailGenerator and use it to send the emails.
The manager is defined something like:
public class DefaultEmailNotificationManager implements IEmailNotificationManager {
public MailResult sendEmail( EmailType type ) { .. }
public void register( IEmailGenerator generator ) { .. }
}
public interface IEmailGenerator {
public EmailType getType();
}
Trouble is, the generators are defined in the UI project, so they can do things like get hold of wicket page classes, the request cycle, and application resources. Thus I can't add them to the bean in the data projects' applicationContext so that other modules in both the data and UI projects can use them.
Is there any way in the applicationContext of the UI project to do something like:
<bean id="exclusionNotifier" class="com.test.ui.ExclusionEmailNotifier"/>
<bean id="modificationNotifier" class="com.test.ui.ModificationEmailNotifier"/>
<call-method bean-ref="mail-notification-service" method="register">
<param name="generatorImplementation", ref="exclusionNotifier"/>
</call-method>
<call-method bean-ref="mail-notification-service" method="register">
<param name="generatorImplementation", ref="modificationNotifier"/>
</call-method>
I can manually tie the beans together in the WicketApplication.init method but would prefer something more elegant. Has anyone done anything like this?
Using Spring 4.1.4
Thanks in advance.
Upvotes: 0
Views: 685
Reputation: 159
Inject generators into mail-notification-service
bean (e.g. using autowire="byType"
) and register them right after bean construction using init-method
(see Initialization callbacks in Spring docs)
public class DefaultEmailNotificationManager implements IEmailNotificationManager {
private Collection<IEmailGenerator> generators;
public void init() {
for( IEmailGenerator g : generators ) {
register(g);
}
}
public void setGenerators( Collection<IEmailGenerator> generators ) {
this.generators = generators;
}
public MailResult sendEmail( EmailType type ) { .. }
private void register( IEmailGenerator generator ) { .. }
}
data's applicationContext:
<bean id="mail-notification-service"
class="com.test.DefaultEmailNotificationManager"
init-method="init"
autowire="byType" />
UI's applicationContext:
<bean id="exclusionNotifier" class="com.test.ui.ExclusionEmailNotifier"/>
<bean id="modificationNotifier" class="com.test.ui.ModificationEmailNotifier"/>
Upvotes: 1