web.xml
<?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">
<display-name>testWebApp</display-name>
<listener>
<listener-class>com.test.MyFirstListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/testWebApp.xml</param-value>
</context-param>
</web-app>
package com.test;
import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import org.springframework.web.context.ContextLoaderListener;
import org.springframework.web.context.WebApplicationContext;
public class MyFirstListener extends ContextLoaderListener
{
private static ServletContext servletContext;
@Override
public void contextInitialized(ServletContextEvent event)
{
System.out.println("MyFirstListener.contextInitialized(...)");
final ServletContext servletContext = event.getServletContext();
MyFirstListener.servletContext = servletContext;
super.contextInitialized(event);
WebApplicationContext springContext = null;
try
{
springContext = (WebApplicationContext) servletContext.getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE);
} catch (RuntimeException ex)
{
String msg = "Problem getting Spring context: " + ex.getMessage();
throw ex;
}
try
{
MyFirstSpringClass objMyFirstSpringClass = (MyFirstSpringClass)springContext.getBean("MyFirstSpringClass");
System.out.println(objMyFirstSpringClass.SayHello("koma"));
} catch (Exception e)
{
System.out.println(e.toString());
}
}
}
MyFirstSpringClass.java
package com.test;
public class MyFirstSpringClass
{
public String SayHello(String name)
{
return "helo" + name;
}
/**
* Factory Method Test
* @return
*/
public static synchronized String getResourceConfig()
{
return "mystring";
}
}
0 件のコメント:
コメントを投稿