在自定义组件中获取spring底层组件

要想在自定义组件中获取spring底层的各种组件,只需让自定义组件实现一系列接口即可,这些接口都是Aware的子接口。常见的有:

1. ApplicationContextAware——用于获取IOC容器;

2. BeanNameAware——用于获取bean的名称;

3. EmbeddedValueResolverAware——用于获取字符串解析器,可以解析各种占位符,例如${}、$#{}等。

示例代码如下,自定义bean类实现了三种Aware接口

public class Candy implements ApplicationContextAware, BeanNameAware, EmbeddedValueResolverAware {
    //bean的名称
    private String name;
    //容器
    private ApplicationContext applicationContext;
    //配置文件中的值
    private String nickname;

    @Override
    public void setBeanName(String name) {
        this.name = name;
    }
    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        this.applicationContext = applicationContext;
    }
    @Override
    public void setEmbeddedValueResolver(StringValueResolver resolver) {
        this.nickname = resolver.resolveStringValue("${bottle.nickname}");
    }
}

原文地址:https://www.cnblogs.com/dubhlinn/p/10708211.html

时间: 2024-08-25 16:22:12

在自定义组件中获取spring底层组件的相关文章

angular2的ElementRef在组件中获取不到

angular2的ElementRef在组件中获取不到 angular2不推荐操作dom,但是实际应用中不可避免的需要使用到dom操作,怎么操作,官方文档提供了一系列api(ElementRef,ViewContainerRef ,TemplateRef)配合 @ViewChild或@ViewChildren就可以获取到dom元素,但是这个过程中有些文档未提及的坑,本人不小心踩进去,半天才爬出来,因此分享一下. 首先,需要在ng2的模板中使用 #banners 定义一个模板局部变量,如: <di

vue父组件中获取子组件中的数据

<FormItem label="上传头像" prop="image"> <uploadImg :width="150" :height="150" :name="'avatar'" size="150px*150px" ref="avatar"></uploadImg> </FormItem> <FormItem

unigui组件中client javascript delphi组件之间的操作

UniLabel组件: function OnClick(sender, e){ MainForm.UniLabel1.setText('Click!');} function Onmousemove(sender, x, y){ MainForm.UniLabel1.setPosition(x, y);} unibutton组件: function OnMouseout(sender, e){ sender.setText('Out');} uniedit组件 function form.On

在普通类中获取Spring管理的bean

1.在项目中添加下面的类: import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContextAware; /** * 以静态变量保存Spring ApplicationContext, 可在任何代码任何地方任何时候中取出ApplicaitonContext. * */ public class SpringContextHolder implem

vue组件中的样式属性:scoped,解决在父组件中无法修改子组件样式问题

Scoped CSS规范是Web组件产生不污染其他组件,也不被其他组件污染的CSS规范. vue组件中的style标签标有scoped属性时表明style里的css样式只适用于当前组件元素,它是通过使用PostCSS来改变以下内容实现的: <style scoped> .example { color: red; } </style> <template> <div class="example">hi</div> </

vue中父子组件主动获取值 父组件向子件间的传值

父组件主动获取子组件的数据和方法: 1.调用子组件的时候定义一个ref <v-header ref='header'></header> 2.在父组件里面通过 this.$refs.header.属性 this.$refs.header.方法 子组件主动获取父组件的数据和方法 this.$parent.数据 this.$parent.方法 父组件向子件间的传值 1.父组件调用子组件的时候 绑定动态属性 2.在子组件里通过props接受父组件传过来的数据 原文地址:https://w

delphi 从 TWebbrowse组件中获取图片

在 delphi 中使用 TWebbrowse 组件,虽然效率不如用(idhttp之类)模拟操作效率高.但其难度低,上手快,简单粗暴有效. 从网上搜到的处理此问题的文章大多是 ctrl + c 复制到剪贴板的方法,但在 win7 64中,此法几乎没法使用,随时报剪贴板错误. 本方法利用 IHTMLElementRender 接口,完美地解决了问题.同时,也可以加深对接口的进一步理解. delphi7源码下载 unit Unit1; interface uses Windows, Messages

在非spring组件中注入spring bean

1.在spring中配置如下<context:spring-configured/>     <context:load-time-weaver aspectj-weaving="autodetect"/> 2.spring bean如下 用@configurable进行注解,这样我们可以直接new RealTimeStatisticTask,那么RealTimeStaticDao也能被正常注入了. 3.将spring-instrument-tomcat-4.1

在Vue组件中获取全局的点击事件

// 定义全局点击函数 Vue.prototype.globalClick = function (callback) { document.getElementById('main').onclick = function () { callback(); }; }; mounted: function () { this.globalClick(this.moreSetupMenuRemove); } // 移除操作 moreSetupMenuRemove () { this.$refs.m