PDA

View Full Version : [Noob] FrameworkServlet and basic DI


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?

Marten Deinum
May 22nd, 2007, 05:29 AM
I'm not sure how the injection goes but I guess it is injecting it by NAME. Your bean is named 'commonDAOBean' and you have a setter for 'commonDAO' these names do not match. Either try renaming your bean to 'commonDAO' or create a setter for 'commonDAOBean'.

cadiolis
May 22nd, 2007, 12:16 PM
medeinum - thanks for the tip. I changed it to:

<!-- Define DAO beans-->
<bean id="commonDAO" class="intouch.core.dao.impl.CommonDAOImpl">
<property name="sqlMapClient" ref="commonSqlMapConfig"/>
</bean>
<bean id="reportDAO" class="intouch.reporting.dao.impl.ReportDAOImpl">
<property name="sqlMapClient" ref="reportSqlMapConfig"/>
</bean>

And added:
<bean id="reportServlet" class="intouch.reporting.ReportServlet">
<property name="commonDAO" ref="commonDAO"/>
<property name="reportDAO" ref="reportDAO"/>
</bean>

Now if I put debug code in the setter I can see it being called. However, the instance is still null when the servlet is instantiated via an http call. I have a a feeling I am missing something small.

Marten Deinum
May 22nd, 2007, 12:19 PM
Well the reportServlet in your applicationContext.xml file is another one than the one in your web.xml. You are settting the properties on the one instantiated inside the Spring container. However the on inside your web.xml doesn't get injected.

cadiolis
May 22nd, 2007, 12:41 PM
I just gave that ID randomly, not to match against anything. Are you suggesting that it has to be something specific? If so, do you know what is?

Marten Deinum
May 22nd, 2007, 02:07 PM
No.. I just wanted/tried to make clear that the instance you added to your applicationcontext is another instance than the one in your web.xml.

sreelatha
Jun 4th, 2007, 02:01 AM
Hi I am Sreelatha and I am struck up in retriving data from MySql data base using springs ans hibernate and I am getting an exception as follows:

org.springframework.web.util.NestedServletExceptio n: Request processing failed; nested exception is java.lang.ClassCastException: blogger.Blogger

karldmoore
Jun 4th, 2007, 07:45 AM
It would be better to post another thread for this problem. It would be really helpful however, if you could post the code that's causing this problem the configuration and also the stacktrace. If you put this in [code] [ /code] tags, it's sooo much easier to read!