spring-获取工厂对象

获取Spring容器的工厂

 

方式一:

根据配置文件创建工厂并管理对象

这种方式与容器启动时创建的工厂是2个概念,在进行事务控制等方面会有问题!

仅测试用!

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;
	
	@Override
	public Student getStudent(String number) {
		return studentService.getStudent(number);
	}

	@Override
	public List<Student> list() {
		return studentService.list();
	}

	@Override
	public 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 {
	@Override
	public 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;
	
	
@Override
public 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
 *
 */
@Component
public class BeanFactoryUtil implements BeanFactoryAware {

	private static BeanFactory factory;
	
	@Override
	public 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;


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

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值