java

位置:IT落伍者 >> java >> 浏览文章

servlet中如何访问spring容器


发布日期:2022年07月08日
 
servlet中如何访问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中定义的一个服务接口:

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容器中的对象

上一篇:Struts配置讲解Web应用

下一篇:spring事务配置