package comwebaction;
import javautilArrayList;
import javautilList;
import javaxservlethttpHttpServlet;
import javaxservlethttpHttpServletRequest;
import javaxservlethttpHttpServletResponse;
import orgsprntextWebApplicationContext;
import orgsprntextsupportWebApplicationContextUtils;
import compojoProducts;
import comserviceIWorksService;
public class Test extends HttpServlet{
private IWorksService worksService;
private WebApplicationContext wac;
public void init(){
wac =WebApplicationContextUtilsgetRequiredWebApplicationContext(thisgetServletContext());
}
protected void doGet(HttpServletRequest reqHttpServletResponse resp){
Systemoutprintln(servlet );
String className=reqgetParameter(className);
List list=new ArrayList();
if(className!=null&&classNameequals(worksService)){
worksService=(IWorksService)wacgetBean(worksService);
wac = (WebApplicationContext)thisgetServletContext()getAttribute(WebApplicationContextROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE);
try{
list=worksServicegetWorksList(new Products() );
Systemoutprintln(list size+listsize());
}catch(Exception e){
eprintStackTrace();
}
}
try{
respgetWriter()append(list size|+listsize());
respgetWriter()flush();
}catch(Exception e){
eprintStackTrace();
}
}
}
IWorksService 为spring中定义的一个服务接口:
package comwebaction;
import javautilArrayList;
import javautilList;
import javaxservlethttpHttpServlet;
import javaxservlethttpHttpServletRequest;
import javaxservlethttpHttpServletResponse;
import orgsprntextWebApplicationContext;
import orgsprntextsupportWebApplicationContextUtils;
import compojoProducts;
import comserviceIWorksService;
public class Test extends HttpServlet{
private IWorksService worksService;
private WebApplicationContext wac;
public void init(){
wac =WebApplicationContextUtilsgetRequiredWebApplicationContext(thisgetServletContext());
}
protected void doGet(HttpServletRequest reqHttpServletResponse resp){
Systemoutprintln(servlet );
String className=reqgetParameter(className);
List list=new ArrayList();
if(className!=null&&classNameequals(worksService)){
worksService=(IWorksService)wacgetBean(worksService);
wac = (WebApplicationContext)thisgetServletContext()getAttribute(WebApplicationContextROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE);
try{
list=worksServicegetWorksList(new Products() );
Systemoutprintln(list size+listsize());
}catch(Exception e){
eprintStackTrace();
}
}
try{
respgetWriter()append(list size|+listsize());
respgetWriter()flush();
}catch(Exception e){
eprintStackTrace();
}
}
}
IWorksService 为spring中定义的一个服务接口:
<bean id=worksService
class=comserviceimplWorksServiceImpl>
<property name=worksDao>
<ref bean=worksDao />
</property>
<property name=divi>
<ref bean=diviPageUtil />
</property>
</bean>
<bean id=worksService
class=comserviceimplWorksServiceImpl>
<property name=worksDao>
<ref bean=worksDao />
</property>
<property name=divi>
<ref bean=diviPageUtil />
</property>
</bean>
可以看出访问sping容器中对象可以通过访问web容器中的固定属性而得到spring容器进行得到spring容器中的对象