Spring课程 Spring入门篇 3-3 Spring bean装配(上)之aware接口
本节主要介绍了以下内容:
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接口的更多相关文章
- Spring Boot -01- 快速入门篇(图文教程)
Spring Boot -01- 快速入门篇(图文教程) 今天开始不断整理 Spring Boot 2.0 版本学习笔记,大家可以在博客看到我的笔记,然后大家想看视频课程也可以到[慕课网]手机 app ...
- Spring实践系列-入门篇(一)
本文主要介绍了在本地搭建并运行一个Spring应用,演示了Spring依赖注入的特性 1 环境搭建 1.1 Maven依赖 目前只用到依赖注入的功能,故以下三个包已满足使用. <properti ...
- Spring Cloud Alibaba入门篇
学习条件 了解web三层架构 熟练应用SSM架构 了解Maven管理工具的使用 熟练使用SpringBoot,以及了解SpringBoot基本原理. 了解部分术语:应用.工具.耦合.负载等 温馨提示: ...
- Spring Data JPA 入门篇
Spring Data JPA是什么 它是Spring基于ORM框架(如hibernate,Mybatis等).JPA规范(Java Persistence API)封装的一套 JPA应用框架,可使开 ...
- Spring Boot源码(四):Bean装配
为了演示Spring中对象是如何创建并放到spring容器中,这里新建一个maven项目: 其中pom.xm文件中只引入了一个依赖: <dependencies> <dependen ...
- spring学习六----------Bean的配置之Aware接口
© 版权声明:本文为博主原创文章,转载请注明出处 Aware Spring提供了一些以Aware结尾的接口,实现了Aware接口的bean在被初始化后,可以获取相应的资源 通过Aware接口,可以对S ...
- Spring课程 Spring入门篇 2-1 IOC和bean容器
课程链接: 本节讲了5部分内容,6为项目demo: 1 接口及面向接口编程 2 什么是IOC 3 Spring的bean配置 4 Bean的初始化 5 Demo 自己理解: 1 高层模块和底层模块都依 ...
- spring boot 学习入门篇【spring boot项目的搭建以及如何加载jsp界面】
[ 前言] Spring Boot 简介:Spring Boot是由Pivotal团队提供的全新框架,其设计目的是用来简化新Spring应用的初始搭建以及开发过程.该框架使用了特定的方式来进行配置, ...
- spring boot(一):入门篇
构建微服务:Spring boot 入门篇 什么是spring boot Spring Boot是由Pivotal团队提供的全新框架,其设计目的是用来简化新Spring应用的初始搭建以及开发过程.该框 ...
- Spring Boot(一):入门篇+前端访问后端
转自:Spring Boot(一):入门篇 什么是Spring Boot Spring Boot 是由 Pivotal 团队提供的全新框架,其设计目的是用来简化新 Spring 应用的初始搭建以及开发 ...
随机推荐
- 读《JavaScript权威指南》笔记(五)
1.getComputedStyle()方法的返回值是一个CSSStyleDeclaration对象,它代表了应用在指定元素(或伪对象)上的所有样式. 2.clip style="clip: ...
- P4014 分配问题
\(\color{#0066ff}{题目描述}\) 有 \(n\) 件工作要分配给 \(n\) 个人做.第 \(i\) 个人做第 \(j\) 件工作产生的效益为 \(c_{ij}\) .试设计一个将 ...
- Codeforces Round #533 (Div. 2)题解
link orz olinr AK Codeforces Round #533 (Div. 2) 中文水平和英文水平都太渣..翻译不准确见谅 T1.给定n<=1000个整数,你需要钦定一个值t, ...
- SprimgMVC学习笔记(八)—— SpringMVC与前台json数据交互
一.两种交互形式 可以看出,前台传过来的方式有两种,一种是传json格式的数据过来,另一种就是在url的末尾传普通的key/value串过来,针对这两种方式,在Controller类中会有不同的解析, ...
- Jenkins自动化CI CD流水线之5--pipeline
一.概览 二.安装 在对jenkins进行初始化安装时,默认已经安装了jenkins的相关插件,如下图所示: 三.实操 新建任务: 编写pipeline脚本: 我们可以借助流水线语法去做. test流 ...
- getResourceAsStream properties
try (InputStream is = getClass().getResourceAsStream("/test.properties")) { Properties pro ...
- case when null then 'xx' else 'yy' end 无效
Sql Server 中使用case when then 判断某字段是否为null,和判断是否为字符或数字时的写法不一样,而且语法能正常执行, 如果不注意数据内容,很容易搞错. 错误方法: CASE ...
- 通过JS,按照原比例控制图片尺寸
<html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Con ...
- mybatis用法
转载:https://www.cnblogs.com/xdp-gacl/p/4261895.html 一.Mybatis介绍 MyBatis是一个支持普通SQL查询,存储过程和高级映射的优秀持久层框架 ...
- java线程类图
Executors创建不同类型的Executor, executor执行不同的runnable task Executor: Runnable: