package web action;
import java util ArrayList;
import java util List;
import javax servlet HttpServlet;
import javax servlet HttpServletRequest;
import javax servlet HttpServletResponse;
import sprntext WebApplicationContext;
import sprntext support WebApplicationContextUtils;
import pojo Products;
import service IWorksService;
public class Test extends HttpServlet{
private IWorksService worksService;
private WebApplicationContext wac;
public void init(){
wac =WebApplicationContextUtils getRequiredWebApplicationContext(this getServletContext());
}
protected void doGet(HttpServletRequest req HttpServletResponse resp){
System out println( servlet );
String className=req getParameter( className );
List list=new ArrayList();
if(className!=null&&className equals( worksService )){
worksService=(IWorksService)wac getBean( worksService );
wac = (WebApplicationContext)this getServletContext() getAttribute(WebApplicationContext ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE);
try{
list=worksService getWorksList(new Products() );
System out println( list size +list size());
}catch(Exception e){
e printStackTrace();
}
}
try{
resp getWriter() append( list size| +list size());
resp getWriter() flush();
}catch(Exception e){
e printStackTrace();
}
}
} IWorksService 为spring中定义的一个服务接口:
package web action;
import java util ArrayList;
import java util List;
import javax servlet HttpServlet;
import javax servlet HttpServletRequest;
import javax servlet HttpServletResponse;
import sprntext WebApplicationContext;
import sprntext support WebApplicationContextUtils;
import pojo Products;
import service IWorksService;
public class Test extends HttpServlet{
private IWorksService worksService;
private WebApplicationContext wac;
public void init(){
wac =WebApplicationContextUtils getRequiredWebApplicationContext(this getServletContext());
}
protected void doGet(HttpServletRequest req HttpServletResponse resp){
System out println( servlet );
String className=req getParameter( className );
List list=new ArrayList();
if(className!=null&&className equals( worksService )){
worksService=(IWorksService)wac getBean( worksService );
wac = (WebApplicationContext)this getServletContext() getAttribute(WebApplicationContext ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE);
try{
list=worksService getWorksList(new Products() );
System out println( list size +list size());
}catch(Exception e){
e printStackTrace();
}
}
try{
resp getWriter() append( list size| +list size());
resp getWriter() flush();
}catch(Exception e){
e printStackTrace();
}
}
} IWorksService 为spring中定义的一个服务接口: <bean id= worksService class= service impl WorksServiceImpl > <property name= worksDao > <ref bean= worksDao /> </property> <property name= divi > <ref bean= diviPageUtil /> </property> </bean>
<bean id= worksService class= service impl WorksServiceImpl > <property name= worksDao > <ref bean= worksDao /> </property> <property name= divi > <ref bean= diviPageUtil /> </property> </bean> 可以看出访问sping容器中对象可以通过访问web容器中的固定属性而得到spring容器 进行得到spring容器中的对象
lishixinzhi/Article/program/Java/ky/201311/28787