zoukankan      html  css  js  c++  java
  • Spring ApplicationContextAware获取上下文

    一、ApplicationContextAware 用处

      Spring 提供了ApplicationContextAware类,通过它可以获取所有bean上下文。

    二、怎么用?

      ①、定义一个工具类,去实现 ApplicationContextAware,实现 setApplicationContext方法即可

    public class SpringContextUtil implements ApplicationContextAware {
    
        private static ApplicationContext applicationContext; // Spring应用上下文环境
    
        @Override
        public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
            SpringContextUtil.applicationContext = applicationContext;
        }
    
        /**
         * 获取对象
         * 
         * @param name
         * @return Object 一个以所给名字注册的bean的实例
         * @throws BeansException
         */
        public static Object getBean(String name) throws BeansException {
            return applicationContext.getBean(name);
        }
    
    }

      ②、业务代码

    public interface UserReadService {
    
        public UserInfo getUserInfoById(Long id);
    }
    
    @Component("userReadService")
    public class UserReadServiceImpl implements UserReadService {
    
        @Override
        public UserInfo getUserInfoById(Long id) {
            System.out.println("获取用户信息");
            return null;
        }
    
    }

      ③、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:context="http://www.springframework.org/schema/context"
        xsi:schemaLocation="http://www.springframework.org/schema/beans
                http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
                http://www.springframework.org/schema/context
                http://www.springframework.org/schema/context/spring-context-4.1.xsd">
    
    
        <context:annotation-config />
        <!--自动扫描含有@Service将其注入为bean -->
        <context:component-scan base-package="com.mycompany.yuanmeng.springdemo.aop" />
    
        <bean id="springContextUtil" class="com.mycompany.yuanmeng.springdemo.aop.SpringContextUtil" />
    
    </beans>

      ④、test

    public static void test() {
            
            new ClassPathXmlApplicationContext("spring.xml"); // 加载ApplicationContext(模拟启动web服务), 当然如果是用web工程的话,可以直接在web.xml配置,容器会去加载此文件。
            
            UserReadService userReadService = (UserReadService) SpringContextUtil.getBean("userReadService");
            
            userReadService.getUserInfoById(null);
     }
  • 相关阅读:
    2017-4-25 winform公共控件属性 菜单和工具栏属性
    2017-4-24 winform窗体基础属性 ico图片生成 不规则窗体的移动 恶搞小程序
    2017-4-23 知识补充
    C# 动态方法和静态方法的区别 (转)
    2017-4-21 Ado.net字符串攻击 防御 实体类 数据访问类 属性扩展 三层架构开发
    ToString()用法 select top 子句
    2017-4-19 ado.net 增,删,改,查,练习
    2017-4-17 类库 通用变量 is和as运算符 委托
    2017-4-16 多态 构造函数 方法重载 静态方法和静态成员
    【2017-03-12】SQL Sever 子查询、聚合函数
  • 原文地址:https://www.cnblogs.com/chenmo-xpw/p/5539596.html
Copyright © 2011-2022 走看看