首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > 编程 >

spring-获取工场对象

2013-08-13 
spring-获取工厂对象package com.hqh.student.wsimport java.util.Listimport javax.jws.WebServiceimp

spring-获取工厂对象
package com.hqh.student.ws;import java.util.List;import javax.jws.WebService;import org.springframework.beans.factory.BeanFactory;import org.springframework.context.support.ClassPathXmlApplicationContext;import com.hqh.student.model.Reward;import com.hqh.student.model.Student;import com.hqh.student.service.StudentService;@WebService(endpointInterface="com.hqh.student.ws.IStudentWSService",serviceName="StudentWSService",portName="studentServicePort",targetNamespace="http://ws.student.hqh.com",wsdlLocation="META-INF/wsdl/student.wsdl")public class StudentWSServiceImpl implements IStudentWSService{private static final BeanFactory factory = new ClassPathXmlApplicationContext("beans.xml");public StudentWSServiceImpl() {if(studentService==null) {studentService = factory.getBean(StudentService.class);}}private StudentService studentService;@Overridepublic Student getStudent(String number) {return studentService.getStudent(number);}@Overridepublic List<Student> list() {return studentService.list();}@Overridepublic List<Reward> listReward(String number, String year) {return studentService.listReward(number, year);}}

?

?

?

方式二:

通过Servlet来获取

1、?创建一个Servlet用于在初始化servlet时,通过servlet的context获取到spring容器的工厂对象

package com.hqh.student.context;import javax.servlet.ServletException;import javax.servlet.http.HttpServlet;import org.springframework.web.context.WebApplicationContext;import org.springframework.web.context.support.WebApplicationContextUtils;/** * 根据Servlet的context对象获取spring工厂 * 再将工厂设置到自己的工具类中 * @author lenovo * */public class InitContextServlet extends HttpServlet {@Overridepublic void init() throws ServletException {WebApplicationContext ctx = WebApplicationContextUtils.getWebApplicationContext(this.getServletContext());WebUtil.setWebAppContext(ctx);super.init();}}

?

2、 获取到工厂对象后,将对象设置到自己的工具类中,以后直接从工具类中获取bean

package com.hqh.student.context;import org.springframework.web.context.WebApplicationContext;public class WebUtil {private static WebApplicationContext ctx;private WebUtil() {}public static void setWebAppContext(WebApplicationContext ctx) {System.out.println("------------------->>>>>>>>>>>WebUtil.setWebAppContext()");WebUtil.ctx = ctx;}public static WebApplicationContext getWebAppContext() {return ctx;}public static Object getBean(Class clazz) {return ctx.getBean(clazz);}}

?

3、 web.xml配置servlet

<!-- 初始化Servlet,用于设置spring工厂 --><servlet><servlet-name>InitContextServlet</servlet-name><servlet-class>com.hqh.student.context.InitContextServlet</servlet-class><load-on-startup>1</load-on-startup></servlet>

?4、从工具类中获bean【定时任务,webservice等在没有通过注入方式初始化对象时,非常有用!】

private StudentService studentService;@Overridepublic Student getStudent(String number) {studentService = (StudentService) WebUtil.getBean(StudentService.class);return studentService.getStudent(number);}

?

?

方式三:

通过BeanFactoryAware接口来获取,该方式较第二种方式更简单些!

?

package com.hqh.student.context;import org.springframework.beans.BeansException;import org.springframework.beans.factory.BeanFactory;import org.springframework.beans.factory.BeanFactoryAware;import org.springframework.stereotype.Component;/** * 一定要加注解,声明该对象由spring管理 * 否则,实现了BeanFactoryAware也不会将工厂注入 * @author lenovo * */@Componentpublic class BeanFactoryUtil implements BeanFactoryAware {private static BeanFactory factory;@Overridepublic void setBeanFactory(BeanFactory factory) throws BeansException {System.out.println("------------------->>>>>>>>>>>BeanFactoryUtil.setBeanFactory()");this.factory = factory;}public static Object getBean(Class clazz) {return factory.getBean(clazz);}}

?

?获取bean

private StudentService studentService;@Overridepublic Student getStudent(String number) {//studentService = (StudentService) WebUtil.getBean(StudentService.class);studentService = (StudentService) BeanFactoryUtil.getBean(StudentService.class);return studentService.getStudent(number);}

?

?

热点排行