`
wiselyman
  • 浏览: 2082721 次
  • 性别: Icon_minigender_1
  • 来自: 合肥
博客专栏
Group-logo
点睛Spring4.1
浏览量:81272
74ae1471-94c5-3ae2-b227-779326b57435
点睛Spring MVC4...
浏览量:130211
社区版块
存档分类
最新评论

12点睛Spring4.1-Spring Aware

 
阅读更多

12.1 Aware

  • 我们设计的准则是解耦,这就意味着我们不能对Spring的IoC容器有直接的依赖,但是我们还是想我们的bean能识别容器的资源;
  • 使用aware能让我们在应用的任意位置获得spring容器的资源;
  • 我们通过实现aware接口来识别spring容器的资源;
  • Spring包含的aware有:
    • BeanNameAware
    • BeanFactoryAware
    • ApplicationContextAware
    • MessageSourceAware
    • ApplicationEventPublisherAware
    • ResourceLoaderAware
  • 实现ApplicationContextAware接口,可识别所有的资源,但最好是你用到什么就使用什么;
  • 这也就意味着我们就耦合到了spring框架上了,没有spring框架你的代码将无法执行;

12.2 示例

12.2.1 新建演示bean

package com.wisely.aware;

import org.springframework.beans.factory.BeanNameAware;
import org.springframework.context.ResourceLoaderAware;
import org.springframework.core.io.ResourceLoader;
import org.springframework.stereotype.Component;
@Component
public class DemoBean implements BeanNameAware,ResourceLoaderAware{
    private String name;
    private ResourceLoader loader;

    //BeanNameAware接口的方法
    public void setBeanName(String beanName) {
        this.name = beanName;

    }

    //ResourceLoaderAware接口的的方法
    public void setResourceLoader(ResourceLoader resourceLoader) {
        this.loader = resourceLoader;

    }

    public String getName() {
        return name;
    }


    public ResourceLoader getLoader() {
        return loader;
    }

}

12.2.2 新建演示用文件info.txt

jhkljhlkjhlkj
111111111111

12.2.3 测试

package com.wisely.aware;

import java.io.IOException;

import org.apache.commons.io.IOUtils;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.core.io.Resource;
import org.springframework.core.io.ResourceLoader;

public class Main {

    public static void main(String[] args) throws IOException {
        AnnotationConfigApplicationContext context =
                new AnnotationConfigApplicationContext("com.wisely.aware");
        DemoBean db = context.getBean(DemoBean.class);
        System.out.println(db.getName());
        ResourceLoader rl = db.getLoader();
        Resource r = rl.getResource("classpath:com/wisely/aware/info.txt");
        System.out.println(IOUtils.toString(r.getInputStream()));
        context.close();
    }

}

输出结果

demoBean
jhkljhlkjhlkj
111111111111

新书推荐《JavaEE开发的颠覆者: Spring Boot实战》,涵盖Spring 4.x、Spring MVC 4.x、Spring Boot企业开发实战。

 

京东地址:http://item.jd.com/11894632.html

当当地址:http://product.dangdang.com/23926195.html

亚马逊地址:http://www.amazon.cn/图书/dp/B01D5ZBFUK/ref=zg_bsnr_663834051_6 

淘宝地址:https://item.taobao.com/item.htm?id=528426235744&ns=1&abbucket=8#detail

 

 

 

或自己在京东、淘宝、亚马逊、当当、互动出版社搜索自选。

 


0
0
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics