enthusiastic
enthusiastic

Reputation: 1702

Passing data from servlet to spring using applciation context aware

I am trying to implement applicationContextAware in my servlet.I have the data from client side coming to my servlet.From my servlet I need to pass it to the beans which has getters and setters.I have my DAO s where I have MYSQL operations.

My applicationContext.xml has

<bean id="dataSource" destroy-method="close"
    class="org.apache.commons.dbcp.BasicDataSource">
    <property name="driverClassName" value="com.mysql.jdbc.Driver" />
    <property name="url"
        value="jdbc:mysql://localhost:3306/bazaar_admin_portal" />
    <property name="username" value="usrnm" />
    <property name="password" value="pwd" />
</bean>

<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
    <constructor-arg index="0">
        <ref bean="dataSource" />
    </constructor-arg>
</bean>

<bean
    class="org.dao.impl.TestDAOimpl"
    id="TestDAO">
    <property name="jdbcTemplate" ref="jdbcTemplate" />
</bean>

My web.xml contains

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/applicationContext.xml</param-value>
</context-param>
<servlet>
    <description></description>
    <display-name>TestServlet</display-name>
    <servlet-name>TestServlet</servlet-name>
    <servlet-class>com.controllers.TestServlet</servlet-class>
</servlet>
<servlet-mapping>
    <servlet-name>TestServlet</servlet-name>
    <url-pattern>/Test</url-pattern>
</servlet-mapping>
<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
</welcome-file-list>

And in my TestServlet under doPost method

     private static ApplicationContext applicationContext = null;

public void setApplicationContext(ApplicationContext ctx)
        throws BeansException {
    applicationContext = ctx;

I have getters and setters class Test.Also interface TestDAO and TestDAOimpl class which implements the interface.

I want to know how do I pass the data from my servlet to the spring side...i.e set the data which will enable the TestDAOimpl to insert into my DB.

Thanks

Upvotes: 0

Views: 2034

Answers (3)

kompot2k
kompot2k

Reputation: 1

in your servlet

@Autowired
private ApplicationContext ctx;

@Autowired
private MyDao myDao;

@Override
public void init() throws ServletException {
        WebApplicationContextUtils.getWebApplicationContext(super.getServletContext()).getAutowireCapableBeanFactory().autowireBean(this);
}

Upvotes: -1

Aravind A
Aravind A

Reputation: 9707

ApplicationContextAware is for beans to be aware of their application context . Read here for more info . What you could so is use WebApplicationContextUtils WebApplicationContextUtils.getRequiredWebApplicationContext(ServletContext sc) , get the application context , use getBean method and invoke the Dao.

Upvotes: 1

korifey
korifey

Reputation: 3519

Are you sure you don't want to use Spring WebMVC? It will handle your problem automatically.

Then try this in you POST Method (It's quite slow, init it lazily):

applicationContext = WebApplicationContextUtils.getWebApplicationContext(this.getServletContext());

Upvotes: 2

Related Questions