cadiolis
May 21st, 2007, 04:35 PM
I am trying to create a basic servlet that does not require a MVC implementation. From what I can tell I should be able to do this using FrameworkServlet. I am converting an advanced Jasper report server based on straight servlets to Spring so no MVC is needed.
As a side note: The examples I have seen in the Spring docs for Jasper are not sufficient as I need to get my hands on the actual JasperReport object to do some manipulation.
My question is how do I perform basic dependency injection into the Framework Servlet? Here are some code snippets of what I have right now.
[ReportServlet.java]
...
public class ReportServlet extends FrameworkServlet {
private CommonDAO commonDAO;
public void setCommonDAO(CommonDAO commonDAO)
{
this.commonDAO = commonDAO;
}
...
@Override
protected void doService(HttpServletRequest arg0, HttpServletResponse arg1) throws Exception {
logger.info("commonDAO: " + commonDAO);
logger.info("commonDAO.config: " + commonDAO.loadConfig(1, 1));
}
[web.xml]
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd">
<web-app>
<display-name>InTouch Jasper Reporting</display-name>
<description>The Jasper Reports Web Application</description>
<!-- Reports servlet -->
<servlet>
<servlet-name>reports</servlet-name>
<servlet-class>intouch.reporting.ReportServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>reports</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>
index.jsp
</welcome-file>
</welcome-file-list>
</web-app>
<?xml version="1.0" encoding="UTF-8"?>
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:jee="http://www.springframework.org/schema/jee"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
http://www.springframework.org/schema/jee
http://www.springframework.org/schema/jee/spring-jee-2.0.xsd">
<jee:jndi-lookup id="dataSource"
jndi-name="/jdbc/devel"
resource-ref="true" />
<!-- Web SqlMap setup for iBATIS Database Layer -->
<bean id="commonSqlMapConfig" class="org.springframework.orm.ibatis.SqlMapClientFactory Bean">
<property name="dataSource" ref="dataSource" />
<property name="configLocation">
<value>classpath:commonSqlMapConfig.xml</value>
</property>
</bean>
<!-- Define DAO beans-->
<bean id="commonDAOBean" class="intouch.dao.impl.CommonDAOImpl">
<property name="sqlMapClient" ref="commonSqlMapConfig"/>
</bean>
</beans>
When the application starts up the commonDAO setter IS called. If I put some output in there I can see the value and can make database calls. However, when I try to access via the browser, commonDAO is always null. If I add the following code it will work:
ApplicationContext ctx = getWebApplicationContext();
commonDAO = (CommonDAO)ctx.getBean("commonDAOBean");
However, this doesn't seem right. How can I get dependency injection to work using FrameworkServlet? Is there something I need to add to my XML config file?
As a side note: The examples I have seen in the Spring docs for Jasper are not sufficient as I need to get my hands on the actual JasperReport object to do some manipulation.
My question is how do I perform basic dependency injection into the Framework Servlet? Here are some code snippets of what I have right now.
[ReportServlet.java]
...
public class ReportServlet extends FrameworkServlet {
private CommonDAO commonDAO;
public void setCommonDAO(CommonDAO commonDAO)
{
this.commonDAO = commonDAO;
}
...
@Override
protected void doService(HttpServletRequest arg0, HttpServletResponse arg1) throws Exception {
logger.info("commonDAO: " + commonDAO);
logger.info("commonDAO.config: " + commonDAO.loadConfig(1, 1));
}
[web.xml]
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd">
<web-app>
<display-name>InTouch Jasper Reporting</display-name>
<description>The Jasper Reports Web Application</description>
<!-- Reports servlet -->
<servlet>
<servlet-name>reports</servlet-name>
<servlet-class>intouch.reporting.ReportServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>reports</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>
index.jsp
</welcome-file>
</welcome-file-list>
</web-app>
<?xml version="1.0" encoding="UTF-8"?>
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:jee="http://www.springframework.org/schema/jee"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
http://www.springframework.org/schema/jee
http://www.springframework.org/schema/jee/spring-jee-2.0.xsd">
<jee:jndi-lookup id="dataSource"
jndi-name="/jdbc/devel"
resource-ref="true" />
<!-- Web SqlMap setup for iBATIS Database Layer -->
<bean id="commonSqlMapConfig" class="org.springframework.orm.ibatis.SqlMapClientFactory Bean">
<property name="dataSource" ref="dataSource" />
<property name="configLocation">
<value>classpath:commonSqlMapConfig.xml</value>
</property>
</bean>
<!-- Define DAO beans-->
<bean id="commonDAOBean" class="intouch.dao.impl.CommonDAOImpl">
<property name="sqlMapClient" ref="commonSqlMapConfig"/>
</bean>
</beans>
When the application starts up the commonDAO setter IS called. If I put some output in there I can see the value and can make database calls. However, when I try to access via the browser, commonDAO is always null. If I add the following code it will work:
ApplicationContext ctx = getWebApplicationContext();
commonDAO = (CommonDAO)ctx.getBean("commonDAOBean");
However, this doesn't seem right. How can I get dependency injection to work using FrameworkServlet? Is there something I need to add to my XML config file?