package com
import java
import java
import javax
import javax
import javax
import org
import org
import com
import com
public class Test extends HttpServlet{
private IWorksService worksService;
private WebApplicationContext wac;
public void init(){
wac =WebApplicationContextUtils
}
protected void doGet(HttpServletRequest req
System
String className=req
List list=new ArrayList();
if(className!=null&&className
worksService=(IWorksService)wac
wac = (WebApplicationContext)this
try{
list=worksService
System
}catch(Exception e){
e
}
}
try{
resp
resp
}catch(Exception e){
e
}
}
}
IWorksService 為spring中定義的一個服務接口:
package com
import java
import java
import javax
import javax
import javax
import org
import org
import com
import com
public class Test extends HttpServlet{
private IWorksService worksService;
private WebApplicationContext wac;
public void init(){
wac =WebApplicationContextUtils
}
protected void doGet(HttpServletRequest req
System
String className=req
List list=new ArrayList();
if(className!=null&&className
worksService=(IWorksService)wac
wac = (WebApplicationContext)this
try{
list=worksService
System
}catch(Exception e){
e
}
}
try{
resp
resp
}catch(Exception e){
e
}
}
}
IWorksService 為spring中定義的一個服務接口:
<bean id=
class=
<property name=
<ref bean=
</property>
<property name=
<ref bean=
</property>
</bean>
<bean id=
class=
<property name=
<ref bean=
</property>
<property name=
<ref bean=
</property>
</bean>
可以看出訪問sping容器中對象可以通過訪問web容器中的固定屬性而得到spring容器
From:http://tw.wingwit.com/Article/program/Java/ky/201311/28787.html