Skip to content

Instantly share code, notes, and snippets.

@rajasharan
Created February 19, 2015 22:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rajasharan/6bffcefdccb8c1aed382 to your computer and use it in GitHub Desktop.
Save rajasharan/6bffcefdccb8c1aed382 to your computer and use it in GitHub Desktop.
trying to make legacy non-spring servlets access spring wired services (https://stackoverflow.com/questions/28592841/nosuchbeandefinitionexception-when-trying-to-use-spring-with-jersey)
package com.sample.test
import java.io.IOException;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;
public class LegacyServlet extends HttpServlet {
public void init(ServletConfig config) throws ServletException {
super.init(config);
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
WebApplicationContext ctx = WebApplicationContextUtils.getRequiredWebApplicationContext(getServletContext());
//TestJdbc test = (TestJdbc) ctx.getBean("testJdbc");
String [] beans = ctx.getBeanDefinitionNames();
ServletOutputStream os = response.getOutputStream();
os.println("length: " + beans.length); // prints 0 length
os.flush();
}
}
<?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:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:jee="http://www.springframework.org/schema/jee"
xmlns:jdbc="http://www.springframework.org/schema/jdbc"
xmlns:lang="http://www.springframework.org/schema/lang"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.6.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.6.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.6.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.0.6.xsd
http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jee/spring-jdbc-3.0.6.xsd
http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang-3.0.6.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.6.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.6.xsd">
<context:annotation-config />
<context:component-scan base-package="com.sample.test"/>
<!--
<jee:jndi-lookup id="dataSource" jndi-name="java:/comp/evn/jdbc/localdb2"/>
<bean id="npJdbcTemplate" class="org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate">
<constructor-arg ref="dataSource"/>
</bean>
-->
<bean id="testB" class="com.sample.test.TestB"></bean>
<bean id="testJdbc" class="com.sample.test.TestJdbc">
<!--
<property name="npJdbcTemplate" ref="npJdbcTemplate" />
<property name="dataSource" ref="dataSource" />
-->
<property name="testB" ref="testB" />
</bean>
</beans>
package com.sample.test;
import org.springframework.stereotype.Component;
@Component
public class TestB {
}
package com.sample.test;
import javax.sql.DataSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Repository;
@Component
public class TestJdbc {
// @Autowired
private NamedParameterJdbcTemplate npJdbcTemplate;
// @Autowired
private DataSource dataSource;
@Autowired
private TestB testB;
// @Autowired
public void setDataSource(DataSource dataSource) {
System.out.println("setDataSource: " + dataSource);
this.dataSource = dataSource;
}
public NamedParameterJdbcTemplate getNpJdbcTemplate() {
return npJdbcTemplate;
}
// @Autowired
public void setNpJdbcTemplate(NamedParameterJdbcTemplate npJdbcTemplate) {
this.npJdbcTemplate = npJdbcTemplate;
}
public DataSource getDataSource() {
return dataSource;
}
public TestB getTestB() {
return testB;
}
@Autowired
public void setTestB(TestB testB) {
this.testB = testB;
}
}
<?xml version="1.0" encoding="UTF-8"?>
<web-app
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
id="WebApp_ID"
version="2.5" >
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:spring-test.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>LegacyServlet</servlet-name>
<servlet-class>com.sample.test.LegacyServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>LegacyServlet</servlet-name>
<url-pattern>/LegacyServlet/*</url-pattern>
</servlet-mapping>
</web-app>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment