课程链接:

1    概述

2    代码演练

3    代码解析

1    概述

1.1  bean注解相关

a  context:component-scan标签使用

问:该标签的作用是什么?

答:该标签作用是支持注解,在xml配置文件中不用配置bean了

问:该标签的使用条件是什么?

答:spring3.0之后才支持该标签的使用

b  类注解相关

@Component   通用类注解(所有实体类都可以用该注解)

@Repository     注解dao类(持久层)

@Service     注解Service类(服务层)

@Controller    注解Controller类(控制层)

1.2  bean的作用域注解实现

问:该标签的使用条件是什么?

答:spring2.5 之后才支持该标签的使用

2    代码演练

2.1 注解实现(因为没有用xml bean的注入,并且没有自定义bean名称实体类component 注解后没有变量,测试类默认小驼峰)   

实体类

package com.imooc.beanannotation;

import org.springframework.stereotype.Component;

//因为不知道是什么类(dao类或者service类),所以用组件 通用的注解 @Component
@Component
public class BeanAnnotation { public void say(String word){
System.out.println("爱生活,爱老婆---"+word);
} }

配置文件:(有了context:component-scan 标签之后不再使用context:annotation-config。前者是扫描基于类的注解,后者是完成bean的注册后,处理类内的方法和成员变量的注解。前者包括后者)

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
"> <context:component-scan base-package="com.imooc.beanannotation"></context:component-scan> </beans>

测试类(获取bean的样式为小驼峰,因为实体类没有配置组件后的变量)

package com.imooc.beanannotation;

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 TestBeanAnnotation extends UnitTestBase{ public TestBeanAnnotation() {
super("classpath*:spring-beanannotation.xml");
// TODO Auto-generated constructor stub
} /**
* 注意:此处getBean 为 类的小驼峰形式
*/
@Test
public void testbeanAnnotation(){
try {
BeanAnnotation bAnnotation = super.getbean("beanAnnotation");
bAnnotation.say("褒姒");
} catch (Exception e) {
// TODO: handle exception
}
} }

打印结果:

三月 06, 2019 7:14:13 上午 org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh
信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@409c8a10: startup date [Wed Mar 06 07:14:13 CST 2019]; root of context hierarchy
三月 06, 2019 7:14:13 上午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
信息: Loading XML bean definitions from URL [file:/F:/xiangmu3/Xin/FuQiang/Spring/ddwei-dao/target/classes/spring-beanannotation.xml]
爱生活,爱老婆---褒姒
三月 06, 2019 7:14:13 上午 org.springframework.context.support.ClassPathXmlApplicationContext doClose
信息: Closing org.springframework.context.support.ClassPathXmlApplicationContext@409c8a10: startup date [Wed Mar 06 07:14:13 CST 2019]; root of context hierarchy

2.2 注解实现( 自定义bean名称实体类component 注解后有变量,测试类测试时使用bean自定义名称)   

实体类:

package com.imooc.beanannotation;

import org.springframework.stereotype.Component;

//因为不知道是什么类(dao类或者service类),所以用组件 通用的注解 @Component
@Component("beanName")
public class BeanAnnotation { public void say(String word){
System.out.println("爱生活,爱老婆---"+word);
} }

测试类:

package com.imooc.beanannotation;

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 TestBeanAnnotation extends UnitTestBase{ public TestBeanAnnotation() {
super("classpath*:spring-beanannotation.xml");
// TODO Auto-generated constructor stub
} /**
* 注意:此处getBean 为 类的小驼峰形式
*/
@Test
public void testbeanAnnotation(){
try {
BeanAnnotation bAnnotation = super.getbean("beanName");
bAnnotation.say("褒姒");
} catch (Exception e) {
// TODO: handle exception
}
} }

打印结果:(1.1打印结果相同)

2.3  bean的作用域注解实现(prototype)

实体类:

package com.imooc.beanannotation;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component; //因为不知道是什么类(dao类或者service类),所以用组件 通用的注解 @Component
@Scope("prototype")
@Component("beanName")
public class BeanAnnotation { public void say(String word){
System.out.println("爱生活,爱老婆---"+word);
} //打印出hashCode
public void getHashCodeProtype(){
System.out.println("hashCode码值为:---"+this.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"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd"> <context:component-scan base-package="com.imooc.beanannotation"></context:component-scan> </beans>

测试类:

package com.imooc.beanannotation;

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 TestBeanAnnotation extends UnitTestBase{ public TestBeanAnnotation() {
super("classpath*:spring-beanannotation.xml");
// TODO Auto-generated constructor stub
} /**
* 注意:此处getBean 为 类的小驼峰形式
*/
@Test
public void testbeanAnnotation(){
try {
BeanAnnotation bAnnotation = super.getbean("beanName");
bAnnotation.say("褒姒");
} catch (Exception e) {
// TODO: handle exception
}
} @Test
public void testbeanAnnotationGetScope(){
try {
BeanAnnotation bAnnotation = super.getbean("beanName");
bAnnotation.getHashCodeProtype(); bAnnotation = super.getbean("beanName"
);
bAnnotation.getHashCodeProtype();

} catch (Exception e) {
// TODO: handle exception
}
} }

打印结果:

三月 08, 2019 6:16:46 上午 org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh
信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@3f64fffc: startup date [Fri Mar 08 06:16:46 CST 2019]; root of context hierarchy
三月 08, 2019 6:16:46 上午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
信息: Loading XML bean definitions from URL [file:/F:/xiangmu3/Xin/FuQiang/Spring/ddwei-dao/target/classes/spring-beanannotation.xml]
hashCode码值为:---2137315958
三月 08, 2019 6:16:47 上午 org.springframework.context.support.ClassPathXmlApplicationContext doClose
信息: Closing org.springframework.context.support.ClassPathXmlApplicationContext@3f64fffc: startup date [Fri Mar 08 06:16:46 CST 2019]; root of context hierarchy
hashCode码值为:---910342737

2.4  bean的作用域注解实现(singleton)

实体类:

package com.imooc.beanannotation;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component; //因为不知道是什么类(dao类或者service类),所以用组件 通用的注解 @Component
//@Scope("prototype")
@Scope("singleton")
@Component("beanName")
public class BeanAnnotation { public void say(String word){
System.out.println("爱生活,爱老婆---"+word);
} //打印出hashCode
public void getHashCodeProtype(){
System.out.println("hashCode码值为:---"+this.hashCode());
} }

xml配置文件(同2.3)

测试类:(同2.3)

打印结果:

三月 08, 2019 6:19:15 上午 org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh
信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@3f64fffc: startup date [Fri Mar 08 06:19:15 CST 2019]; root of context hierarchy
三月 08, 2019 6:19:15 上午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
信息: Loading XML bean definitions from URL [file:/F:/xiangmu3/Xin/FuQiang/Spring/ddwei-dao/target/classes/spring-beanannotation.xml]
hashCode码值为:---1939979416
hashCode码值为:---1939979416

三月 08, 2019 6:19:15 上午 org.springframework.context.support.ClassPathXmlApplicationContext doClose
信息: Closing org.springframework.context.support.ClassPathXmlApplicationContext@3f64fffc: startup date [Fri Mar 08 06:19:15 CST 2019]; root of context hierarchy

3    代码解析

3.1  2.1的例子,注解注入默认

a  实体类:    使用@Component组件

b  xml配置文件:   <context:component-scan base-package="com.imooc.beanannotation"></context:component-scan>

            (引入xml相关声明,详细见2.1例子 spring3.0已上才支持)

c  测试类:    实体类@Component后边没有定义bean时,如何获取bean

3.2  2.2的例子,注解注入定义bean

a  实体类:    使用@Component("beanName")组件

b  xml配置文件:   <context:component-scan base-package="com.imooc.beanannotation"></context:component-scan>

             (引入xml相关声明,详细见2.1例子 spring3.0已上才支持)

c  测试类:    BeanAnnotation bAnnotation = super.getbean("beanName"); (注解@Component定义bean时,如何获取bean)

3.3  2.3的例子,作用域多例的使用

a  实体类:    @Scope("prototype")

3.4  2.4的例子,作用域单例的使用

a  实体类:    @Scope("singleton")

Spring课程 Spring入门篇 4-1 Spring bean装配(下)之bean定义及作用域注解实现的更多相关文章

  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 学习入门篇【spring boot项目的搭建以及如何加载jsp界面】

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

  6. spring boot(一):入门篇

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

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

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

  8. (转)Spring boot(一):入门篇

    https://www.cnblogs.com/ityouknow/p/5662753.html#!comments 构建微服务:Spring boot 入门篇 什么是Spring Boot Spri ...

  9. Spring Boot(一):入门篇

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

随机推荐

  1. 智能合约安全事故回顾(3)-DOS漏洞导致的KotET事件

    现实世界中的网络都是有带宽限制的,想象一下,一个访问量稳定的网站,突然有人利用某种方式爆发式的将网站的访问量提升,这个时候系统会作何反应?如果系统没有合理的防DOS攻击的方式,这种时候往往会造成服务器 ...

  2. iOS10 新特性-新功能,以及ReplayKit库

    iOS的10.0 本文总结了iOS的10,运行于目前推出iOS设备推出的主要开发者相关的功能. iOS版10引入了新的方法来增加您的应用程序通过帮助系统参与在适当的时候建议你的应用程序给用户.如果你在 ...

  3. 7、OpenCV Python 高斯模糊

    __author__ = "WSX" import cv2 as cv import numpy as np #高斯模糊 基于权重(卷积) #高斯模糊 去燥效果很好 #高斯模糊 d ...

  4. 《图解HTTP》阅读笔记--第二章 简单的HTTP协议--第三章 HTTP报文信息

     第二章.简单的HTTP协议HTTP协议:HTTP协议用于客户端(请求资源的一端)和服务器端(响应回复提供资源的一端)的通信,是一种无状态协议HTTP1.1默认TCP持久连接,管线化发送(并行发送多个 ...

  5. P1979 华容道

    题意:$n*m$棋盘上$n*m-1$颗棋子,有且只有一个格子为空白格子,每个棋子大小$1*1$ 有些棋子可以移动,而有些棋子固定,任何与空白的格子相邻(有公共的边)的格子上的棋子都可以移动到空白格子上 ...

  6. 梯度下降&随机梯度下降&批梯度下降

    梯度下降法 ​ 下面的h(x)是要拟合的函数,J(θ)损失函数,theta是参数,要迭代求解的值,theta求解出来了那最终要拟合的函数h(θ)就出来了.其中m是训练集的记录条数,j是参数的个数. 梯 ...

  7. BA 的职责

    Responsibility: 确定系统应用范围:获取实际业务情况(但是并不包含与用户完善实际业务的过程),与用户一起分析实际业务中需要通过软件应用来完成的部分: 备注:是否要与用户一同来分析软件应用 ...

  8. 使用esp32-Arduino+PubSubClient+mqtt 上传数据到中移动OneNet

    使用esp32-doit-dev-v1开发板,测试mqtt协议, 发布(publish)到onenet 平台.注意:1.使用的mqtt arduino 客户端是 pubsubclient 库.其默认是 ...

  9. DesiredCapabilities内容详解--Appium服务关键字

    上次了解了一些DesiredCapabilities的用法,有些还是不太清楚,去appium官网找了找官方文档,觉得写的很全: ## Appium 服务关键字 <expand_table> ...

  10. Oulipo (KMP出现次数)

    The French author Georges Perec (1936–1982) once wrote a book, La disparition, without the letter 'e ...