Servelt?你可以通过工厂拿到啊?Spring 一般都有一个工厂类来进行获取服务
比如
Factory.java
import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.support.ClassPathXmlApplicationContext;public class Factory {
private static BeanFactory beanFactory;
static {
try {
beanFactory = new ClassPathXmlApplicationContext(new String[] { "applicationContext.xml" });
} catch (Exception ex) {
ex.printStackTrace();
}
} public static Object getBean(String beanName) {
if (beanName == null) {
return null;
}
try {
Object obj = beanFactory.getBean(beanName);
if (obj == null) {
System.err.println("Lookup Object[" + beanName + "] Error!");
}
return obj;
} catch (Exception ex) {
System.out.println(beanName);
ex.printStackTrace();
return null;
}
}
}
比如
Factory.java
import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.support.ClassPathXmlApplicationContext;public class Factory {
private static BeanFactory beanFactory;
static {
try {
beanFactory = new ClassPathXmlApplicationContext(new String[] { "applicationContext.xml" });
} catch (Exception ex) {
ex.printStackTrace();
}
} public static Object getBean(String beanName) {
if (beanName == null) {
return null;
}
try {
Object obj = beanFactory.getBean(beanName);
if (obj == null) {
System.err.println("Lookup Object[" + beanName + "] Error!");
}
return obj;
} catch (Exception ex) {
System.out.println(beanName);
ex.printStackTrace();
return null;
}
}
}
解决方案 »
- 数组问题求解(急求)
- 大家看看这个异常什么原因引起的Communications link failure due to underlying exception
- 请问java BS软件如何时添加注册码?
- xml与properties你会选哪个
- 请问,struts2有什么好的URL插件吗?
- struts中使用validator框架的问题: Cannot load a validator resource from
- C:\>ant java.lang.InstantiationException: org.apache.tools.ant.Main
- redhat 9.0下装过weblogic 7.0.
- java web项目备份以及恢复mysql数据库数据的问题
- jquery里.ajax({ .......... dataType: "json" }) 里面dataType: "json"的问题
- 静态Form能不能像动态Form那样用ValidatorPlugIn进行验证?
- hibernate 查询出错
throws ServletException, IOException {
ServletContext application;
WebApplicationContext wac;
application = getServletContext();
wac = WebApplicationContextUtils.getWebApplicationContext(application);//获取spring的context
Service1 service1= (Service1) wac.getBean("service1");
}
ApplicationContext ctx = WebApplicationContextUtils.getWebApplicationContext(getServletContext());
this.yourService = (YourService)ctx.getBean("yourService");