- 浏览: 170506 次
- 性别:
- 来自: 杭州
文章分类
- 全部博客 (159)
- java常用 (28)
- Spring Security (0)
- tomcat (2)
- orcale (4)
- rmi (6)
- jvm (5)
- html (0)
- hibernate (0)
- javascript (5)
- log4j (5)
- struts (5)
- jdbc (0)
- jquery (0)
- json (0)
- jstl (0)
- jboss (0)
- JCaptcha (0)
- jpa (0)
- oscache (0)
- 项目管理 (8)
- ExtJs (10)
- 设计模式 (3)
- jsp (3)
- 错误归档 (1)
- maven (5)
- java多线程 (10)
- quartz (4)
- spring (9)
- memcache (3)
- java笔试面试 (3)
- xml (3)
- hadoop (5)
- velocity (2)
- jetty (3)
- http (1)
- linux (2)
- hbase (2)
- mysql (12)
- mybatis (8)
- web安全 (1)
- hive (2)
- 其他 (1)
最新评论
获得spring里注册Bean的四种方法,特别是第三种方法,简单:
一:方法一(多在struts框架中)继承BaseDispatchAction
import com.mas.wawacommunity.wap.service.UserManager;
public class BaseDispatchAction extends DispatchAction {
/**
* web应用上下文环境变量
*/
protected WebApplicationContext ctx;
protected UserManager userMgr;
/**
* 获得注册Bean
* @param beanName String 注册Bean的名称
* @return
*/
protected final Object getBean(String beanName) {
return ctx.getBean(beanName);
}
protected ActionForward unspecified(ActionMapping mapping, ActionForm form,
javax.servlet.http.HttpServletRequest request,
javax.servlet.http.HttpServletResponse response) {
return mapping.findForward("index");
}
public void setServlet(ActionServlet servlet) {
this.servlet = servlet;
this.ctx = WebApplicationContextUtils.getWebApplicationContext(servlet.getServletContext());
this.userMgr = (UserManager) getBean("userManager");
}
}
二:方法二实现BeanFactoryAware
一定要在spring.xml中加上:
<bean id="serviceLocator" class="com.am.oa.commons.service.ServiceLocator" singleton="true" />
当对serviceLocator实例时就自动设置BeanFactory,以便后来可直接用beanFactory
public class ServiceLocator implements BeanFactoryAware {
private static BeanFactory beanFactory = null;
private static ServiceLocator servlocator = null;
public void setBeanFactory(BeanFactory factory) throws BeansException {
this.beanFactory = factory;
}
public BeanFactory getBeanFactory() {
return beanFactory;
}
public static ServiceLocator getInstance() {
if (servlocator == null)
servlocator = (ServiceLocator) beanFactory.getBean("serviceLocator");
return servlocator;
}
/**
* 根据提供的bean名称得到相应的服务类
* @param servName bean名称
*/
public static Object getService(String servName) {
return beanFactory.getBean(servName);
}
/**
* 根据提供的bean名称得到对应于指定类型的服务类
* @param servName bean名称
* @param clazz 返回的bean类型,若类型不匹配,将抛出异常
*/
public static Object getService(String servName, Class clazz) {
return beanFactory.getBean(servName, clazz);
}
}
action调用:
public class UserAction extends BaseAction implements Action,ModelDriven{
private Users user = new Users();
protected ServiceLocator service = ServiceLocator.getInstance();
UserService userService = (UserService)service.getService("userService");
public String execute() throws Exception {
return SUCCESS;
}
public Object getModel() {
return user;
}
public String getAllUser(){
HttpServletRequest request = ServletActionContext.getRequest();
List ls=userService.LoadAllObject( Users.class );
request.setAttribute("user",ls);
this.setUrl("/yonghu.jsp");
return SUCCESS;
}
}
三:方法三实现ApplicationContextAware
一定要在spring.xml中加上:
<bean id="SpringContextUtil " class="com.am.oa.commons.service.SpringContextUtil " singleton="true" />
当对SpringContextUtil 实例时就自动设置applicationContext,以便后来可直接用applicationContext
public class SpringContextUtil implements ApplicationContextAware {
private static ApplicationContext applicationContext; //Spring应用上下文环境
/**
* 实现ApplicationContextAware接口的回调方法,设置上下文环境
* @param applicationContext
* @throws BeansException
*/
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
SpringContextUtil.applicationContext = applicationContext;
}
/**
* @return ApplicationContext
*/
public static ApplicationContext getApplicationContext() {
return applicationContext;
}
/**
* 获取对象
* @param name
* @return Object 一个以所给名字注册的bean的实例
* @throws BeansException
*/
public static Object getBean(String name) throws BeansException {
return applicationContext.getBean(name);
}
/**
* 获取类型为requiredType的对象
* 如果bean不能被类型转换,相应的异常将会被抛出(BeanNotOfRequiredTypeException)
* @param name bean注册名
* @param requiredType 返回对象类型
* @return Object 返回requiredType类型对象
* @throws BeansException
*/
public static Object getBean(String name, Class requiredType) throws BeansException {
return applicationContext.getBean(name, requiredType);
}
/**
* 如果BeanFactory包含一个与所给名称匹配的bean定义,则返回true
* @param name
* @return boolean
*/
public static boolean containsBean(String name) {
return applicationContext.containsBean(name);
}
/**
* 判断以给定名字注册的bean定义是一个singleton还是一个prototype。
* 如果与给定名字相应的bean定义没有被找到,将会抛出一个异常(NoSuchBeanDefinitionException)
* @param name
* @return boolean
* @throws NoSuchBeanDefinitionException
*/
public static boolean isSingleton(String name) throws NoSuchBeanDefinitionException {
return applicationContext.isSingleton(name);
}
/**
* @param name
* @return Class 注册对象的类型
* @throws NoSuchBeanDefinitionException
*/
public static Class getType(String name) throws NoSuchBeanDefinitionException {
return applicationContext.getType(name);
}
/**
* 如果给定的bean名字在bean定义中有别名,则返回这些别名
* @param name
* @return
* @throws NoSuchBeanDefinitionException
*/
public static String[] getAliases(String name) throws NoSuchBeanDefinitionException {
return applicationContext.getAliases(name);
}
}
action调用:
package com.anymusic.oa.webwork;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import com.anymusic.oa.commons.service.ServiceLocator;
import com.anymusic.oa.hibernate.pojo.Role;
import com.anymusic.oa.hibernate.pojo.Users;
import com.anymusic.oa.spring.IUserService;
import com.opensymphony.webwork.ServletActionContext;
import com.opensymphony.xwork.Action;
import com.opensymphony.xwork.ActionContext;
import com.opensymphony.xwork.ModelDriven;
public class UserAction extends BaseAction implements Action,ModelDriven{
private Users user = new Users();
//不用再加载springContext.xml文件,因为在web.xml中配置了,在程序中启动是就有了.
UserService userService = (UserService) SpringContextUtil.getBean("userService");
public String execute() throws Exception {
return SUCCESS;
}
public Object getModel() {
return user;
}
public String getAllUser(){
HttpServletRequest request = ServletActionContext.getRequest();
List ls=userService.LoadAllObject( Users.class );
request.setAttribute("user",ls);
this.setUrl("/yonghu.jsp");
return SUCCESS;
}
}
四.通过servlet 或listener设置spring的ApplicationContext,以便后来引用.
注意分别extends ContextLoaderListener和ContextLoaderServlet .然后就可用SpringContext来getBean.
覆盖原来在web.xml中配置的listener或servlet.
public class SpringContext {
private static ApplicationContext applicationContext; //Spring应用上下文环境
*/
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
SpringContextUtil.applicationContext = applicationContext;
}
/**
* @return ApplicationContext
*/
public static ApplicationContext getApplicationContext() {
return applicationContext;
}
*/
public static Object getBean(String name) throws BeansException {
return applicationContext.getBean(name);
}
}
public class SpringContextLoaderListener extends ContextLoaderListener{ //
public void contextInitialized(ServletContextEvent event) {
super.contextInitialized(event);
SpringContext.setApplicationContext(
WebApplicationContextUtils.getWebApplicationContext(event.getServletContext())
);
}
}
public class SpringContextLoaderServlet extends ContextLoaderServlet {
private ContextLoader contextLoader;
public void init() throws ServletException {
this.contextLoader = createContextLoader();
SpringContext.setApplicationContext(this.contextLoader.initWebApplicationContext(getServletContext()));
}
}
发表评论
-
AnnotationSessionFactoryBean spring注解自动扫描
2012-08-31 22:35 1047第一步:修改applicationContext.xml ... -
Spring Aop 日志管理
2012-08-31 22:34 1056Spring Aop 日志管理 Spring 带给我们的另一 ... -
Spring中的Advisor,Advice,Pointcut
2012-08-31 22:34 1039Spring中的Advisor,Advice,Pointcut ... -
Spring单例Bean和线程安全
2012-08-31 22:32 3765Spring单例Bean和线程安全 Spring的bea ... -
spring配置数据源方式
2012-08-31 22:29 830[代码] JNDI方式 1、SpringJ ... -
HibernateDaoSupport中的getSession()方法
2012-08-30 20:48 745HibernateDaoSupport中的getSes ... -
spring里加入properties配置
2012-08-30 20:46 1356spring里加入properties配置 直接例子了,在l ... -
全面分析 Spring 的编程式事务管理及声明式事务管理
2012-08-30 20:45 776全面分析 Spring 的编程 ...
相关推荐
bean加入spring容器管理的方式,bean加入applicationcontext容器的方式
几种spring获取bean的方法,包含实现的代码,非常详细哦!!!!
主要给大家介绍了关于spring boot中几种注入方法的一些个人看法,文中通过示例代码介绍的非常详细,对大家学习或者使用spring boot具有一定的参考学习价值,需要的朋友们下面来一起学习学习吧
Spring 实例化Bean的三种方式
主要介绍了详解Spring中bean的几种注入方式,主要介绍了4种注入,主要有属性注入、构造函数注入、工厂方法注入,非常具有实用价值,需要的朋友可以参考下
NULL 博文链接:https://cici9898-007.iteye.com/blog/1407695
IOC注入bean有哪几种,写了几种Spring的bean注入方式demo练习例子,欢迎您观看和留言。免费共享浏览、下载。
主要给大家介绍了关于spring装配bean的3种方式,文中通过示例代码介绍的非常详细,对大家的学习或者使用Spring具有一定的参考学习价值,需要的朋友们下面来一起学习学习吧
Spring在代码中获取bean的几种方式.rar
本篇文章主要介绍了详解Spring中bean实例化的三种方式,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
介绍了Java获取Bean容器的几种常见调用方式,其中详细写出了如何实现ApplicationContextAware 接口来获取Bean容器的工具类
本篇文章主要介绍了Spring学习之Bean的装配三种方法,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
主要介绍了Spring在代码中获取bean的几种方式详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
1. Spring 介绍 1.1 Spring 的优点 ...说一下Spring基于xml注入bean的几种方式? Spring如何解决循环依赖问题? Spring的自动装配 Spring框架中都用到了哪些设计模式? Spring框架中有哪些不同类型的事件?
NULL 博文链接:https://m635674608.iteye.com/blog/2270455
在工作中有时候我们需要在非spring依赖注入或管理的类中获取service、dao等bean对象,这时候用@Autowired和@Resource显然是不行的,那么下面这篇文章就给大家了整理几种获取bean的方式,对大家的学习和工作具有一定...
主要介绍了Spring为IOC容器注入Bean的五种方式详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
依赖注入的几种实现类型 Type1 接口注入 Type2 设值注入 Type3 构造子注入 几种依赖注入模式的对比总结 Spring Bean封装机制 Bean Wrapper Bean Factory ApplicationContext Web Context Spring 高级...
25. 解释 Spring 支持的几种 bean 的作用域 26. Spring 框架中的单例 bean 是线程安全的吗? 27. 解释 Spring 框架中 bean 的生命周期 28. 哪些是重要的 bean 生命周期方法? 你能重载它们吗? 29. 什么是 Spring ...
Spring有几种配置方式 如何用基于XML配置的方式配置Spring 如何用基于Java配置的方式配置Spring 怎样用注解的方式配置Spring 请解释Spring Bean的生命周期 Spring Bean的作用域之间有什么区别 什么是Spring inner ...