课程链接:

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

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. LAMP之Apache

    Apache是世界使用排名第一的Web服务器软件.它可以运行在几乎所有广泛使用的计算机平台上,由于其跨平台和安全性被广泛使用,是最流行的Web服务器端软件之一.快速.可靠并且可通过简单的API扩充,将 ...

  2. [PowerShell]template engine

    今天讨论的是如何在Powershell里实现一个简单的Template Engine的功能. 假设模板文件的内容如下:template.tt hello $name welcome $company ...

  3. 使用nexus 搭建本地 maven 服务器

    1.下载安装nexus oos https://www.sonatype.com/download-oss-sonatype ,选择2.x 版本. 2. 配置maven <server> ...

  4. 优先队列priority_queue的简单应用

    优先队列 引入 优先队列是一种特殊以及强大的队列. 那么优先队列是什么呢? 说白了,就是一种功能强大的队列. 它的功能强大在哪里呢? 四个字:自动排序. 优先队列的头文件&&声明 头文 ...

  5. day_04 列表

    1. 列表list 能装对象的对象,有序的(按照我们存放的顺序) 以[]表示,里面可以存放大量各种元素,各个元组用逗号隔开 列表也具有索引和切片 2. 列表的增改删查 1. 增 1.append() ...

  6. 关于Django的视图层

    视图函数 通俗来讲:视图函数是在url配置是所匹配好了将要调用的对应函数(逻辑代码)        是一个简单的Python 函数,它接受Web请求并且返回Web响应.响应可以是一张网页的HTML内容 ...

  7. Ansible故障

    常见问题一: [root@m01 ~]# ansible  -k 172.16.1.51 -m ping SSH password: [WARNING]: No hosts matched, noth ...

  8. scanf()函数的注意事项

    /* 2 time:2018年5月23日18:57:52 3 author:Howie Tang 4 title:scanf()函数的总结 5 */ #include <stdio.h> ...

  9. Unable to verify your data submission.加入了_csrf也报400错误的解决

    <input type="hidden" name="_csrf" value="<?=Yii::$app->request-> ...

  10. Subarray Sum K

    Given an nonnegative integer array, find a subarray where the sum of numbers is k. Your code should ...