Hi community,
I want to share my expiriences with the integration of the Spring DI into the CaptainCasa framework!
First you have to download the relevant jar libraries from SpringSource.org
Put the relevant jars into your projects wecontent/WEB-INF/lib directory and add it to your classpath (normally the lib directory is already added to the classpath).
Create the applicationContext.xml file in your sources directory (normally src).
Add Spring info to your web.xml file:
Code:
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/classes/applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
To access the beans throw your business logic is no problem, you have to configure your applicationContext.xml so the relevant object instances get injected by the framework.
To get access to the spring context in Cpt.Casa UI classes I use following work around:
Create a class to get the ApplicationContext from the FacesContext:
Code:
public class Context {
public static ApplicationContext getInstance() {
FacesContext context= FacesContext.getCurrentInstance();
ServletContext servletContext = (ServletContext)context.getExternalContext().getContext();
return WebApplicationContextUtils.getWebApplicationContext(servletContext);
}
}
Then create a layer class for my business logic services (see example below). Important is the public static factory method getFromApplicationContext(ctx).
Code:
public final class LogicLayer {
...
public static LogicLayer getFromApplicationContext(ApplicationContext ctx) {
return (LogicLayer) ctx.getBean("logicLayer");
}
}
The name of the bean "logicLayer" is defined in the applicationContext.xml:
Code:
<bean id="logicLayer" class="logic.LogicLayer">
...
</bean>
Create your own implementation of class WorkpageDispatcher, where you integrate the access methods to the beans:
Code:
/*
* The dispatcher is references in faces-config.xml. When changing the package
* of the dispatcher - please also update the faces-config.xml link!
*/
@SuppressWarnings("serial")
public class Dispatcher extends WorkpageDispatcher
{
/*
* This method needs to be implemented - is serves tools (e.g. Layout
* Editor) to find out which objects are exposed by this dispatcher.
*/
public static DispatcherInfo getStaticDispatcherInfo()
{
return new DispatcherInfo(Dispatcher.class);
}
/**
* Returns the expression under which the dispatcher can be reached.
*/
@Override
protected String getRootExpression()
{
return "#{d}";
}
protected b
public LogicLayer getLogicLayer() { return logicLayer; }
public void setLogicLayer(LogicLayer logicLayer) { this.logicLayer = logicLayer; }
}
Create your own WorkpageDispatchedBean where you cast to your Dispatcher implementation and get the reference through getOwningDispatcher():
Code:
public class MyWorkpageDispatchedBean extends WorkpageDispatchedBean
{
public MyWorkpageDispatchedBean(IWorkpageDispatcher dispatcher) {
super(dispatcher);
}
@Override
public Dispatcher getOwningDispatcher() {
return (Dispatcher) super.getOwningDispatcher();
}
// ------------------------------------------------------------------------
// logic access
// ------------------------------------------------------------------------
protected LogicLayer getLogic() {
return getOwningDispatcher().getLogicLayer();
}
}
In the managed bean you now can access the logic layer:
Code:
public class TestUI extends MyWorkpageDispatchedBean implements
Serializable {
private final LogicLayer logicLayer = getLogic();
...
}
Maybe you have your own servlets running within the servlet you can access the Spring context the following way:
Code:
ServletContext context = getServletContext();
WebApplicationContext ctx = WebApplicationContextUtils.getWebApplicationContext(context);
LogicLayer logicLayer = LogicLayer.getFromApplicationContext(context);
This works fine for me. I also tried to directly inject the instances into the Dispatcher(managed bean) with the Spring class DelegatingVariableResolver, but I didn't get it working by now.
The way I tried:
Added DelegatingVariableResolver to faces-config.xml:
Code:
<application>
<variable-resolver>
org.springframework.web.jsf.DelegatingVariableResolver
</variable-resolver>
</application>
Added dispatcher bean to applicationContext.xml:
Code:
<bean id="d" class="Dispatcher" scope="session">
<property name="logicLayer">
<ref bean="logicLayer" />
</property>
</bean>
Just mentioned if somebody has the muse to research further