方法一:

ApplicationContext ap = new ClassPathXmlApplicationContext("applicationContext.xml");

在applicationContent.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:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">
//中间部分是你自己配置的所有bean

WEB项目:

方法1:

ApplicationContext ac1 = WebApplicationContextUtils.getRequiredWebApplicationContext(ServletContext sc)

方法2:

ApplicationContext ac2 = WebApplicationContextUtils.getWebApplicationContext(ServletContext sc)

方法3:

写一个工具类类继承ApplicationObjectSupport,并将这个加入到spring的容器

方法4:

写一个工具类类继承WebApplicationObjectSupport,并将这个加入到spring的容器

 

方法1、2中使用ac.getBean("beanid");获取实例

  

Spring 获取bean的更多相关文章

  1. paip.spring 获取bean getBean 没有beanid的情况下

    paip.spring 获取bean  getBean 没有beanid的情况下 spring能自动扫描带有注解的bean文件.. 作者Attilax  艾龙,  EMAIL:1466519819@q ...

  2. spring获取bean的时候严格区分大小写

    如题:spring获取bean的时候严格区分大小写 配置文件helloservice.xml中配置: <dubbo:reference id="IInsurance" int ...

  3. Spring获取bean工具类,可用于在线程里面获取bean

    Spring获取bean工具类,可用于在线程里面获取bean import java.util.Locale; import org.springframework.beans.BeansExcept ...

  4. Spring获取bean的几种方式

    工作中需要对一个原本加载属性文件的工具类修改成对数据库的操作当然,ado层已经写好,但是需要从Spring中获取bean,然而,工具类并没有交给Spring来管理,所以需要通过方法获取所需要的bean ...

  5. Spring获取bean的一种方式

    随便一百度,网上一大把,并且还不止一种.所以这里就只记录目前用的一种好了. 实现ApplicationContextAware接口 即可: import org.springframework.bea ...

  6. spring 获取 bean

    不通过注解或者是配置文件怎么获取spring中定义的bean呢?有几个方法: 1.实现ApplicationContextAware <bean class="com.xxx.Spri ...

  7. Spring获取bean的工具类

    package com.tech.jin.util; import org.springframework.context.ApplicationContext; import org.springf ...

  8. Spring 获取bean 几种方式

    转载自: http://www.cnblogs.com/luoluoshidafu/p/5659574.html 1.读取xml文件的方式,这种在初学入门的时候比较适用 . ApplicationCo ...

  9. spring 获取bean的几种方式

    1.读取xml文件的方式,这种在初学入门的时候比较适用 . ApplicationContext applicationContext = new ClassPathXmlApplicationCon ...

随机推荐

  1. python工业互联网监控项目实战4—python opcua

    前面章节我们采用OPC作为设备到上位的信息交互的协议,本章我们介绍跨平台的OPC UA.OPC作为早期的工业通信规范,是基于COM/DCOM的技术实现的,用于设备和软件之间交换数据,最初,OPC标准仅 ...

  2. [http 1.1] M-POST w3

    5. Mandatory HTTP Requests An HTTP request is called a mandatory request if it includes at least one ...

  3. Java第一阶段作业总结

    目录 0.前言 1.作业过程总结 2.OO设计心得 3.测试的理解与实践 4.课程收获 5.对课程的建议 前言 本次博客针对第一阶段的三次作业发表总结,作业要求主要是初学者对于Java的基本语法.用法 ...

  4. liunx 之 Ubuntu 网速慢解决方法

    打开终端依次输入以下指令: sudo lshw -numeric -class network sudo ip addr show sudo ip route show sudo tracepath ...

  5. Springboot中,Tomcat启动war包的流程

    将一个SpringBoot项目,打成war包 <!-- 1. 修改POM依赖 --> <dependency> <groupId>org.springframewo ...

  6. 【linux三剑客】grep命令

    grep, egrep, fgrep - print lines matching a pattern grep 命令用于查找文件里符合条件的字符串. grep 指令用于查找内容包含指定的范本样式的文 ...

  7. 发布AI芯片昆仑和百度大脑3.0、L4自动驾驶巴士量产下线,这是百度All in AI一年后的最新答卷...

    机器之心报道,作者:李泽南. 去年的 7 月 5 日,百度在北京国际会议中心开办了首届「AI 开发者大会」.在会上,百度首次喊出了「All in AI」的口号.一年的时间过去了,今天在同样地点举行的第 ...

  8. C++编程入门题目--No.2

    题目:企业发放的奖金根据利润提成.利润(I)低于或等于10万元时,奖金可提10%:利润高 于10万元,低于20万元时,低于10万元的部分按10%提成,高于10万元的部分,可可提 成7.5%:20万到4 ...

  9. CodeForces - 1176A Divide it! (模拟+分类处理)

    You are given an integer nn. You can perform any of the following operations with this number an arb ...

  10. 学习HTML

    前端三剑客:HTML.CSS.JavaScript.现在就开始学习HTML啦. 学习资源:http://www.freecodecamp.cn/ 学习笔记: h1,head1,一级标题 <h1& ...