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");获取实例

      

  • 相关阅读:
    Java运行时内存
    java --对象流与对象的序列化
    Java 文件操作
    爬虫
    eclipse项目放到github
    越来越玄的JAVA
    map和set的遍历
    集合总览
    unsafe类
    狡诈的java并发容器
  • 原文地址:https://www.cnblogs.com/lxn0216/p/9524512.html
Copyright © 2011-2022 走看看