课程链接:

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

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. vue使用过程常见的一些问题

    Vue目前的的开发模式主要有两种:1.直接页面级的开发,script直接引入Vue2.工程性开发,webpack+loader或者直接使用脚手架工具Vue-cli,里面的文件都配置好了 webpack ...

  2. ASP 注释

    ASP.NET中使用HTML标准注释<!-- -->回导致程序崩溃,必须使用ASP标准注释<%-- --%>

  3. Liunx配置静态IP

    刚开始Linux默认的是动态获取,而我们需要设置静态IP(我是为了xshell的连接) 1.  执行dhclient命令自动获取到一个IP,NETMASK, 2.  执行route命令,获取defau ...

  4. kuangbin专题十六 KMP&&扩展KMP HDU3294 Girls' research

    One day, sailormoon girls are so delighted that they intend to research about palindromic strings. O ...

  5. pytorch contiguous的使用

    contiguous一般与transpose,permute,view搭配使用 即使用transpose或permute进行维度变换后,调用contiguous,然后方可使用view对维度进行变形. ...

  6. maven 打包 包含xml

    <build> <finalName>dc-exam</finalName> <!-- 包含xml文件 --> <resources> &l ...

  7. 读经典——《CLR via C#》(Jeffrey Richter著) 笔记_发布者策略控制

    在 读经典——<CLR via C#>(Jeffrey Richter著) 笔记_高级管理控制(配置)中,是由程序集的发布者将程序集的一个新版本发送给管理员,后者安装程序集,并手动编辑应用 ...

  8. 06-图2 Saving James Bond - Easy Version (25 分)

    This time let us consider the situation in the movie "Live and Let Die" in which James Bon ...

  9. 查询多列得到map与查询得到po对象

    import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; i ...

  10. GM TECH2 Scanner Clone

    Professional Diagnostic Tools gm tech 2 scanner china with multi-languages, TIS2000 Programming CD, ...