zoukankan      html  css  js  c++  java
  • Spring 获取bean

    方法一:

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

    在applicationContent.xml中配置

    1 <?xml version="1.0" encoding="UTF-8"?>
    2 <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    3     xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context"
    4     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">
    5         //中间部分是你自己配置的所有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");获取实例

      

  • 相关阅读:
    python排序
    (转载)C++中的sort函数(一)
    谨慎求证,小心思考
    梯度下降算法之方程求解
    单链表基本操作
    为什么会有补码
    tensorflow中的padding方式SAME和VALID的区别
    洛谷P2765 魔术球问题
    洛谷P2754 [CTSC1999]家园
    洛谷P1251 餐巾计划问题
  • 原文地址:https://www.cnblogs.com/lxn0216/p/9524512.html
Copyright © 2011-2022 走看看