Spring 获取bean
方法一:
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的更多相关文章
- paip.spring 获取bean getBean 没有beanid的情况下
paip.spring 获取bean getBean 没有beanid的情况下 spring能自动扫描带有注解的bean文件.. 作者Attilax 艾龙, EMAIL:1466519819@q ...
- spring获取bean的时候严格区分大小写
如题:spring获取bean的时候严格区分大小写 配置文件helloservice.xml中配置: <dubbo:reference id="IInsurance" int ...
- Spring获取bean工具类,可用于在线程里面获取bean
Spring获取bean工具类,可用于在线程里面获取bean import java.util.Locale; import org.springframework.beans.BeansExcept ...
- Spring获取bean的几种方式
工作中需要对一个原本加载属性文件的工具类修改成对数据库的操作当然,ado层已经写好,但是需要从Spring中获取bean,然而,工具类并没有交给Spring来管理,所以需要通过方法获取所需要的bean ...
- Spring获取bean的一种方式
随便一百度,网上一大把,并且还不止一种.所以这里就只记录目前用的一种好了. 实现ApplicationContextAware接口 即可: import org.springframework.bea ...
- spring 获取 bean
不通过注解或者是配置文件怎么获取spring中定义的bean呢?有几个方法: 1.实现ApplicationContextAware <bean class="com.xxx.Spri ...
- Spring获取bean的工具类
package com.tech.jin.util; import org.springframework.context.ApplicationContext; import org.springf ...
- Spring 获取bean 几种方式
转载自: http://www.cnblogs.com/luoluoshidafu/p/5659574.html 1.读取xml文件的方式,这种在初学入门的时候比较适用 . ApplicationCo ...
- spring 获取bean的几种方式
1.读取xml文件的方式,这种在初学入门的时候比较适用 . ApplicationContext applicationContext = new ClassPathXmlApplicationCon ...
随机推荐
- 打印js对象内容
function writeObj(obj){ var description = ""; for(var i in obj){ var property=obj[i]; desc ...
- 闲置安卓设备搭建Linux服务器实现外网访问
title: 闲置安卓设备搭建Linux服务器实现外网访问 这是我搭过的第一个博客系统,写贴纪念一下 待博主整理好思路,将今天所用到的全部分享! 好吧,我就是穷.富人靠科技,穷人靠变异.我这种穷人是真 ...
- Android xUtils3.0使用手册(二) - 数据库操作
步骤: (1). 创建数据表: (2). DaoConfig 获取数据库的配置信息: (3). 获取数据库实例: x.getDb(daoConfig); (4). 数据库的增删改查. 1. 创建 ...
- asp.net下载大文件代码
public void Down(string filepath, HttpResponse aResponse) { System.IO.Stream iStream = null; // Buff ...
- mac OS 安装配置Nginx服务器
系统环境 安装工具 Homebrew软件包管理器 :<mac OS 安装 Homebrew软件包管理器>https://blog.csdn.net/weixin_41791279/arti ...
- oracle查询当前系统时间前10天的数据
select * from eo_c_order t where t.create_time>systimestamp-interval'1'day; 转载于:https://www.cnblo ...
- Intellij IDEA 使用Spring-boot-devTools
转载地址:https://blog.csdn.net/u013938484/article/details/77541050 转载于:https://blog.51cto.com/881206524/ ...
- vSphere可用性之三准备实验环境
第三章 准备实验环境 在上篇内容中,讲述了进行VMware HA实验所必需的软硬件条件.接下来将使用这些来搭建实验环境.主要内容为依据拓扑图安装ESX主机系统.ISCSI存储系统. 此次实验环境的建置 ...
- CF思维联系--CodeForces - 218C E - Ice Skating (并查集)
题目地址:24道CF的DIv2 CD题有兴趣可以做一下. ACM思维题训练集合 Bajtek is learning to skate on ice. He's a beginner, so his ...
- P2480 [SDOI2010]古代猪文
P2480 [SDOI2010]古代猪文 比较综合的一题 前置:Lucas 定理,crt 求的是: \[g^x\bmod 999911659,\text{其中}x=\sum_{d\mid n}\tbi ...