课程链接:

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

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. 使用Google浏览器开发者工具学习HTTP请求记录

    GET请求 1.Google浏览器开发者工具截图图示 2.General Request URL :为请求链接 Status Code :为HTTP响应状态码 3.ResponseHeaders :响 ...

  2. [SinGuLaRiTy] 图论题目复习

    [SInGuLaRiTy-1027] Copyright (c) SinGuLaRiTy 2017. All Rights Reserved. [Vijos 1423] 最佳路线 题目描述 年久失修的 ...

  3. winform按钮文字换行

    用button的Bounds属性 btn.Bounds = new Rectangle(new Point(5 + i * 143, 25), new Size(100, 60));

  4. P3978 [TJOI2015]概率论

    \(\color{#0066ff}{ 题目描述 }\) 为了提高智商,ZJY开始学习概率论.有一天,她想到了这样一个问题:对于一棵随机生成的n个结点的有根二叉树(所有互相不同构的形态等概率出现),它的 ...

  5. CF912E Prime Gift 数学

    Opposite to Grisha's nice behavior, Oleg, though he has an entire year at his disposal, didn't manag ...

  6. IDEA中jsp页面写out.println会报错?

    解决办法: (1)在WEB-INF目录下新建一个lib目录,并把Tomcat安装目录下的如下两个jar包复制过来. (2)选择jsp-api.jar,右键,Add as Library. (3)在Fi ...

  7. linux 虚拟环境问题

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

  8. springboot整合fastdfs

    首先pom.xml <?xml version="1.0" encoding="UTF-8"?> <project xmlns="h ...

  9. Hibernate上传数据到数据库,从数据库读取数据到本地模板代码

    1.Hibernate上传数据到数据库: //创建一个session对象 Session session1=HibernateTools.getSession(); //Fenciresult数据库表 ...

  10. html 一些坑。。。

    margin-left 和 margin-right 才能够使用 auto top 和 bottom 不可以 在网页设计中...通常给img 父容器 一个 宽度...同时 ,指定 img width= ...