|
package com.web.action;
import java.util.ArrayList;
import java.util.List;
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;
import com.pojo.Products;
import com.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(), 0);
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 com.web.action;
import java.util.ArrayList;
import java.util.List;
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;
import com.pojo.Products;
import com.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(), 0);
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="com.service.impl.WorksServiceImpl"> <property name="worksDao"> <ref bean="worksDao" /> </property> <property name="divi"> <ref bean="diviPageUtil" /> </property> </bean>
<bean id="worksService" class="com.service.impl.WorksServiceImpl"> <property name="worksDao"> <ref bean="worksDao" /> </property> <property name="divi"> <ref bean="diviPageUtil" /> </property> </bean> 可以看出访问sping容器中对象可以通过访问web容器中的固定属性而得到spring容器,进行得到spring容器中的对象 |