web读取spring管理的bean,基于xml

原文地址:http://dtbuluo.com/87.html

初识spring框架的时候,可能对于bean的管理,也就是IOC容器的使用,也许不是很困难的事情。

然而,我们开发过程中,往往都是将spring进行javaee的开发,这对于web开发中也许就会带来问题了,虽然所需要的bean已经实例化了在IOC容器当中,然而我们在自己的业务程序中怎么获取到自己想要的bean确是一件比较头痛的事情。

这里主要介绍两种常用的方式:

1、实现spring的ApplicationContextAware接口

首先在web.xml中配置(这里加入了struts2作为控制器):

<!-- Spring ApplicationContext配置文件的路径,可使用通配符,多个路径用,号分隔
	  此参数用于后面的Spring-Context loader -->
	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>classpath*:application*.xml</param-value>
	</context-param>
	
	<!--Spring ApplicationContext 载入 -->
	<listener>
		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
	</listener>
	
	<filter>
		<filter-name>struts2</filter-name>
		<filter-class>
			org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
	</filter>
	<filter-mapping>
		<filter-name>struts2</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>
定义需要管理的类:

package org.enson.service;

public class UserService {
	public int add(int a , int b){
		System.out.println("service"+(a+b));
		return a+b;
	}
}

定义工具类实现ApplicationContextAware,这样在程序中就可以通过工具类获取bean

package org.common;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;

//此类必须配置在spring的xml文件中
public class BeanProvider implements ApplicationContextAware {
	
	private static ApplicationContext applicationContext = null ;

	//由spring容器自动实例化ApplicationContext
	public void setApplicationContext(ApplicationContext context)
			throws BeansException {
		BeanProvider.applicationContext = context ;
	}
	
	//通过名称获取bean
	public static Object getBeans(String beanName){
		return applicationContext.getBean(beanName);
	}

}

编写applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans
	xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:p="http://www.springframework.org/schema/p"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
	
	
<bean id="beanProvider" class="org.common.BeanProvider"/>

<bean id="userService" class="org.enson.service.UserService"/>

</beans>

定义Action类:

package org.enson.action;

import org.common.BeanProvider;
import org.enson.service.UserService;

import com.opensymphony.xwork2.ActionSupport;

public class UserAction extends ActionSupport {
	private static final long serialVersionUID = 1L;
	private UserService userService = (UserService) BeanProvider
			.getBeans("userService");//这边可以直接从容器中拿到对象

	public String test() {
		System.out.println(userService.add(3, 6));
		return SUCCESS;
	}
}


继续列举struts.xml的配置:

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
	"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
	"http://struts.apache.org/dtds/struts-2.3.dtd">

<struts>

	<package name="test" extends="struts-default" namespace="/">
		<default-action-ref name="user"></default-action-ref>
		<action name="user" class="org.enson.action.UserAction" method="test">
			<result name="success">index.jsp</result>
		</action>
	</package>
</struts>

测试后台输出:

service9
9


第二种方法,其实只是BeanProvider 发生了变化,通过BeanFactory接口获得Bean,实现的是BeanFactoryAware 接口

package org.common;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.BeanFactoryAware;

public class BeanFactoryProvidor implements BeanFactoryAware {
	
	private static BeanFactory beanFactory = null;  

	public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
		BeanFactoryProvidor.beanFactory = beanFactory;
	}
	
	public static Object getBeans(String beanName){  
        return beanFactory.getBean(beanName);  
    } 

}

还需要在applicationContext.xml文件中修改:

<bean id="beanFactoryProvidor" class="org.common.BeanFactoryProvidor"/>

<bean id="userService" class="org.enson.service.UserService"/>

对于UserAction的代码,修改如下:

private UserService userService = (UserService) BeanFactoryProvidor
			.getBeans("userService");

	public String test() {
		System.out.println(userService.add(3, 6));
		return SUCCESS;
	}

测试后台结果:
service9
9

通过以上两种方法都可以在web程序中获取SpringIOC当中的bean。

此外,其实还有一种方式,可以通过WebApplicationContext 获取bean,不过这种方法的性能较差,这边就不列举了。不建议使用这种方式。


  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Mr-稻帅

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值