课程链接:

本节主要介绍了以下内容:

1    aware介绍

2    代码演练

3    课程总结

1    aware介绍

1.1  为什么要使用aware?

在java类中,可以方便的获取xml配置文件中的bean的各个属性。通过不同的aware可以获取不同的bean的属性

1.2  有哪些类型的aware?

常见的有:

applicationContextAware(获取bean)

beanNameAware(获取beanname)

还有

beanFactoryAware等等

2    代码演练

2.1  applicationContextAware(验证aware获取的applicationContext 和测试获取的applicationContext 为同一个)

 实体类:

package com.imooc.aware;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware; public class MoocApplicationContext implements ApplicationContextAware{ @Override
public void setApplicationContext(ApplicationContext arg0)
throws BeansException {
System.out.println("MoocApplicationContext========》"+arg0.getBean("moocApplicationContext").hashCode());
} }

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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd"> <bean id="moocApplicationContext" class="com.imooc.aware.MoocApplicationContext"></bean>
<!-- <bean id="moocBeanName" class="com.imooc.aware.MoocBeanName"></bean> --> </beans>

测试类:

package com.imooc.test.aware;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.BlockJUnit4ClassRunner; import com.imooc.test.base.UnitTestBase; @RunWith(BlockJUnit4ClassRunner.class)
public class TestAware extends UnitTestBase{ public TestAware() {
super("classpath:spring-aware.xml");
} //测试ApplicationContext aware
@Test
public void testMoocApplicationContext(){
System.out.println("testApplicationContext======》"+super.getbean("moocApplicationContext").hashCode());
} //测试BeanNameAware
/*@Test
public void testMoocBeanName(){ }*/
}

打印结果:

二月 27, 2019 6:37:13 下午 org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh
信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@5abce07: startup date [Wed Feb 27 18:37:13 CST 2019]; root of context hierarchy
二月 27, 2019 6:37:13 下午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
信息: Loading XML bean definitions from class path resource [spring-aware.xml]
MoocApplicationContext========》282483842
testApplicationContext======》282483842
二月 27, 2019 6:37:13 下午 org.springframework.context.support.ClassPathXmlApplicationContext doClose
信息: Closing org.springframework.context.support.ClassPathXmlApplicationContext@5abce07: startup date [Wed Feb 27 18:37:13 CST 2019]; root of context hierarchy

2.2  beanNameAware(获取xml配置中的beanname)

实体类:

package com.imooc.aware;

import org.springframework.beans.factory.BeanNameAware;

public class MoocBeanName implements BeanNameAware{

    @Override
public void setBeanName(String arg0) {
// TODO Auto-generated method stub
System.out.println("beanName为"+arg0);
} }

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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd"> <bean id="moocBeanName" class="com.imooc.aware.MoocBeanName"></bean> </beans>

测试类:

package com.imooc.test.aware;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.BlockJUnit4ClassRunner; import com.imooc.test.base.UnitTestBase; @RunWith(BlockJUnit4ClassRunner.class)
public class TestAware extends UnitTestBase{ public TestAware() {
super("classpath:spring-aware.xml");
} //测试BeanNameAware
@Test
public void testMoocBeanName(){ }
}

打印结果

二月 27, 2019 7:17:55 上午 org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh
信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@2e9a6f43: startup date [Wed Feb 27 07:17:55 CST 2019]; root of context hierarchy
二月 27, 2019 7:17:55 上午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
信息: Loading XML bean definitions from class path resource [spring-aware.xml]
beanName==moocBeanName
二月 27, 2019 7:17:56 上午 org.springframework.context.support.ClassPathXmlApplicationContext doClose
信息: Closing org.springframework.context.support.ClassPathXmlApplicationContext@2e9a6f43: startup date [Wed Feb 27 07:17:55 CST 2019]; root of context hierarchy

3    课程总结

aware获取的bean和服务器调用的bean的内存地址完全一致。

Spring课程 Spring入门篇 3-3 Spring bean装配(上)之aware接口的更多相关文章

  1. Spring Boot -01- 快速入门篇(图文教程)

    Spring Boot -01- 快速入门篇(图文教程) 今天开始不断整理 Spring Boot 2.0 版本学习笔记,大家可以在博客看到我的笔记,然后大家想看视频课程也可以到[慕课网]手机 app ...

  2. Spring实践系列-入门篇(一)

    本文主要介绍了在本地搭建并运行一个Spring应用,演示了Spring依赖注入的特性 1 环境搭建 1.1 Maven依赖 目前只用到依赖注入的功能,故以下三个包已满足使用. <properti ...

  3. Spring Cloud Alibaba入门篇

    学习条件 了解web三层架构 熟练应用SSM架构 了解Maven管理工具的使用 熟练使用SpringBoot,以及了解SpringBoot基本原理. 了解部分术语:应用.工具.耦合.负载等 温馨提示: ...

  4. Spring Data JPA 入门篇

    Spring Data JPA是什么 它是Spring基于ORM框架(如hibernate,Mybatis等).JPA规范(Java Persistence API)封装的一套 JPA应用框架,可使开 ...

  5. Spring Boot源码(四):Bean装配

    为了演示Spring中对象是如何创建并放到spring容器中,这里新建一个maven项目: 其中pom.xm文件中只引入了一个依赖: <dependencies> <dependen ...

  6. spring学习六----------Bean的配置之Aware接口

    © 版权声明:本文为博主原创文章,转载请注明出处 Aware Spring提供了一些以Aware结尾的接口,实现了Aware接口的bean在被初始化后,可以获取相应的资源 通过Aware接口,可以对S ...

  7. Spring课程 Spring入门篇 2-1 IOC和bean容器

    课程链接: 本节讲了5部分内容,6为项目demo: 1 接口及面向接口编程 2 什么是IOC 3 Spring的bean配置 4 Bean的初始化 5 Demo 自己理解: 1 高层模块和底层模块都依 ...

  8. spring boot 学习入门篇【spring boot项目的搭建以及如何加载jsp界面】

    [ 前言]  Spring Boot 简介:Spring Boot是由Pivotal团队提供的全新框架,其设计目的是用来简化新Spring应用的初始搭建以及开发过程.该框架使用了特定的方式来进行配置, ...

  9. spring boot(一):入门篇

    构建微服务:Spring boot 入门篇 什么是spring boot Spring Boot是由Pivotal团队提供的全新框架,其设计目的是用来简化新Spring应用的初始搭建以及开发过程.该框 ...

  10. Spring Boot(一):入门篇+前端访问后端

    转自:Spring Boot(一):入门篇 什么是Spring Boot Spring Boot 是由 Pivotal 团队提供的全新框架,其设计目的是用来简化新 Spring 应用的初始搭建以及开发 ...

随机推荐

  1. 2019年GPLT L2-3 深入虎穴 比赛题解 中国高校计算机大赛-团体程序设计天梯赛题解

    著名的王牌间谍 007 需要执行一次任务,获取敌方的机密情报.已知情报藏在一个地下迷宫里,迷宫只有一个入口,里面有很多条通路,每条路通向一扇门.每一扇门背后或者是一个房间,或者又有很多条路,同样是每条 ...

  2. linux 虚拟环境问题

    1.python环境 python2和python3命令用来区分python版本 pip2和pip3命令用来区分pip,你的包到底安装在哪里pip3 install xxx sudo apt inst ...

  3. SoapUI Properties的使用

    Link:http://testautomationnoob.blogspot.com/2012/10/soapui-properties-and-property-related.html soap ...

  4. SprimgMVC学习笔记(四)—— @RequestMapping

    一.URL路径映射 @RequestMapping(value="item")或@RequestMapping("/item") 当value的值是数组时,可以 ...

  5. gradle 使用本地maven 仓库 和 提交代码到maven

    /* * This build file was generated by the Gradle 'init' task. * * This generated file contains a sam ...

  6. 电路中IC器件电压符号的解释

    在电子芯片.运算处理器等集成电路行业中,存在多种电压.常用的的有:VDDQ->The supply voltage to output buffers of a memory chip 存储芯片 ...

  7. DropDownList年份的添加

    http://blog.sina.com.cn/s/blog_4b9e030e01007sc3.html

  8. [CF1051F]The Shortest Statement (LCA+最短路)(给定一张n个点m条有权边的无向联通图,q次询问两点间的最短路)

    题目:给定一张n个点m条有权边的无向联通图,q次询问两点间的最短路 n≤100000,m≤100000,m-n≤20. 首先看到m-n≤20这条限制,我们可以想到是围绕这个20来做这道题. 即如果我们 ...

  9. Go语言基础之14--Waitgroup和原子操作

    一.Waitgroup介绍 1.1 背景 package main import ( "fmt" "time" ) func main() { ch := ma ...

  10. springmvc怎么重定向

    转载大神: https://blog.csdn.net/wabiaozia/article/details/50252661