课程链接:

1    解析

2    代码演练

1    解析

1.1  @Required注解

该注解适用于bean属性的set方法

1.2  @Autowired

作用:

是为了把依赖的对象,自动的注入到bean里

使用方式:

1  @autowired required的默认值为false

2  每个类中只有一个构造器被标记为required=true

3  @autowired(required=false):如果找不到实体类,不报错。

4  可以添加到成员变量或者方法上边

2    代码演练

2.1  autowired作为成员变量注解

实体类:

  1. package com.imooc.beanannotation.injection.service;
  2.  
  3. import org.springframework.beans.factory.annotation.Autowired;
  4. import org.springframework.stereotype.Service;
  5.  
  6. import com.imooc.beanannotation.injection.dao.InjectionDao;
  7.  
  8. @Service
  9. public class InjectionServiceImpl implements InjectionService {
  10.  
  11. @Autowired
  12. private InjectionDao injectionDao;
  13.  
  14. @Override
  15. public void save(String sArgs) {
  16. injectionDao.save(sArgs);
  17. // TODO Auto-generated method stub
  18. System.out.println("开始保存!!!"+sArgs);
  19. }
  20.  
  21. }

dao类:

  1. package com.imooc.beanannotation.injection.dao;
  2.  
  3. import org.springframework.stereotype.Repository;
  4.  
  5. @Repository
  6. public class InjectionDaoImpl implements InjectionDao{
  7.  
  8. @Override
  9. public void save(String args) {
  10. // TODO Auto-generated method stub
  11. System.out.println("保存======="+args);
  12. }
  13.  
  14. }

测试类:

  1. package com.imooc.beanannotation.injection2;
  2.  
  3. import org.junit.Test;
  4. import org.junit.runner.RunWith;
  5. import org.junit.runners.BlockJUnit4ClassRunner;
  6.  
  7. import com.imooc.beanannotation.injection.service.InjectionService;
  8. import com.imooc.test.base.UnitTestBase;
  9.  
  10. @RunWith(BlockJUnit4ClassRunner.class)
  11. public class TestInjectionService extends UnitTestBase{
  12.  
  13. public TestInjectionService() {
  14. super("classpath*:spring-beanannotation-injection.xml");
  15. // TODO Auto-generated constructor stub
  16. }
  17.  
  18. @Test
  19. public void testInjectionServiceConstruct(){
  20. try {
  21. InjectionService iService = super.getbean("injectionServiceImpl");
  22. iService.save("12346");
  23. } catch (Exception e) {
  24. // TODO: handle exception
  25. e.printStackTrace();
  26. }
  27. }
  28.  
  29. }

xml:

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xmlns:context="http://www.springframework.org/schema/context"
  5. xsi:schemaLocation="http://www.springframework.org/schema/beans
  6. http://www.springframework.org/schema/beans/spring-beans.xsd
  7. http://www.springframework.org/schema/context
  8. http://www.springframework.org/schema/context/spring-context.xsd">
  9.  
  10. <context:component-scan base-package="com.imooc.beanannotation"></context:component-scan>
  11.  
  12. </beans>

打印结果:

  1. 三月 10, 2019 11:16:29 上午 org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh
  2. 信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@52633079: startup date [Sun Mar 10 11:16:29 CST 2019]; root of context hierarchy
  3. 三月 10, 2019 11:16:29 上午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
  4. 信息: Loading XML bean definitions from URL [file:/F:/xiangmu3/Xin/FuQiang/Spring/ddwei-dao/target/classes/spring-beanannotation-injection.xml]
  5. 保存=======12346
  6. 三月 10, 2019 11:16:30 上午 org.springframework.context.support.ClassPathXmlApplicationContext doClose
  7. 信息: Closing org.springframework.context.support.ClassPathXmlApplicationContext@52633079: startup date [Sun Mar 10 11:16:29 CST 2019]; root of context hierarchy
  8. 开始保存!!!

2.2  autowired作为set方法注解

实体类:

  1. package com.imooc.beanannotation.injection.service;
  2.  
  3. import org.springframework.beans.factory.annotation.Autowired;
  4. import org.springframework.stereotype.Service;
  5.  
  6. import com.imooc.beanannotation.injection.dao.InjectionDao;
  7.  
  8. @Service
  9. public class InjectionServiceImpl implements InjectionService {
  10.  
  11. private InjectionDao injectionDao;
  12.  
  13. @Autowired
  14. public void setInjectionDao(InjectionDao injectionDao) {
  15. this.injectionDao = injectionDao;
  16. }
  17.  
  18. @Override
  19. public void save(String sArgs) {
  20. injectionDao.save(sArgs);
  21. // TODO Auto-generated method stub
  22. System.out.println("开始保存!!!"+sArgs);
  23. }
  24.  
  25. }

打印结果:

  1. 三月 10, 2019 11:21:25 上午 org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh
  2. 信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@5abce07: startup date [Sun Mar 10 11:21:25 CST 2019]; root of context hierarchy
  3. 三月 10, 2019 11:21:25 上午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
  4. 信息: Loading XML bean definitions from URL [file:/F:/xiangmu3/Xin/FuQiang/Spring/ddwei-dao/target/classes/spring-beanannotation-injection.xml]
  5. 保存=======12346
  6. 开始保存!!!12346
  7. 三月 10, 2019 11:21:26 上午 org.springframework.context.support.ClassPathXmlApplicationContext doClose
  8. 信息: Closing org.springframework.context.support.ClassPathXmlApplicationContext@5abce07: startup date [Sun Mar 10 11:21:25 CST 2019]; root of context hierarchy

2.3  autowired作为构造方法注解

实体类:

  1. package com.imooc.beanannotation.injection.service;
  2.  
  3. import org.springframework.beans.factory.annotation.Autowired;
  4. import org.springframework.stereotype.Service;
  5.  
  6. import com.imooc.beanannotation.injection.dao.InjectionDao;
  7.  
  8. @Service
  9. public class InjectionServiceImpl implements InjectionService {
  10.  
  11. private InjectionDao injectionDao;
  12.  
  13. public void setInjectionDao(InjectionDao injectionDao) {
  14. this.injectionDao = injectionDao;
  15. }
  16.  
  17. @Autowired
  18. public InjectionServiceImpl(InjectionDao injectionDao) {
  19. super();
  20. this.injectionDao = injectionDao;
  21. }
  22.  
  23. @Override
  24. public void save(String sArgs) {
  25. injectionDao.save(sArgs);
  26. // TODO Auto-generated method stub
  27. System.out.println("开始保存!!!"+sArgs);
  28. }
  29.  
  30. }

打印结果:

  1. 三月 10, 2019 11:24:33 上午 org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh
  2. 信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@5abce07: startup date [Sun Mar 10 11:24:33 CST 2019]; root of context hierarchy
  3. 三月 10, 2019 11:24:34 上午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
  4. 信息: Loading XML bean definitions from URL [file:/F:/xiangmu3/Xin/FuQiang/Spring/ddwei-dao/target/classes/spring-beanannotation-injection.xml]
  5. 保存=======12346
  6. 三月 10, 2019 11:24:34 上午 org.springframework.context.support.ClassPathXmlApplicationContext doClose
  7. 信息: Closing org.springframework.context.support.ClassPathXmlApplicationContext@5abce07: startup date [Sun Mar 10 11:24:33 CST 2019]; root of context hierarchy
  8. 开始保存!!!

Spring课程 Spring入门篇 4-2 Spring bean装配(下)之Autowired注解说明1的更多相关文章

  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的配置之Autowired注解实现

    © 版权声明:本文为博主原创文章,转载请注明出处 @Required -@Required注解适用于bean属性的setter方法 -这个注解仅仅表示,受影响的bean属性必须在配置时被填充,通过在b ...

  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添加配置类@Configuration并初始化@Bean,@Resource和@Autowired都为null

    大写加黑,找了好久@Resource和@Autowired都依赖不到创建的bean的原因:@Bean的方法名即是创建的Bean名称 import org.activiti.engine.Process ...

  10. spring boot(一):入门篇

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

随机推荐

  1. b,u,i,s,这些被删除的标签以及用来替换他们的标签

    加粗文字 给文字加下划线 倾斜文字 给文字加删除线 这些是以前的HTML语言修饰文字用的,没有语义,所以被删除,不到万不得已 不能使用,HTML给了新的标签替换他们,并且有语义 定义重要性强调的文字 ...

  2. 【转】在Windows64位环境下.net访问Oracle解决方案

    源地址:http://www.cnblogs.com/asingna/archive/2012/05/27/2519950.html

  3. 最长双回文串——manacehr

    题目 [题目描述] 顺序和逆序读起来完全一样的串叫做回文串.比如 acbca 是回文串,而 abc 不是(abc 的顺序为 “abc”,逆序为 “cba”,不相同).输入长度为 n 的串 S,求 S  ...

  4. 三大文本处理工具grep、sed及awk

    一.   用grep在文件中搜索文本 grep能够接受正则表达式,生成各种格式的输出.除此之外,它还有大量有趣的选项. 1.  搜索包含特定模式的文本行: 2.  从stdin中读取: 3.  单个g ...

  5. UVA11270 Tiling Dominoes

    \(\color{#0066ff}{ 题目描述 }\) 给定一个m×n的矩形网格,用1×2多米诺骨牌完全平铺. 请注意,即使一个平铺的旋转与另一个平铺相匹配,它们仍算作不同的平铺. 下面显示了一个平铺 ...

  6. pacman命令用法

    Pacman 是一个命令行工具,这意味着当你执行下面的命令时,必须在终端或控制台中进行. 1.更新系统 在 Arch Linux 中,使用一条命令即可对整个系统进行更新: pacman -Syu 如果 ...

  7. 杭电ACM hdu 2079 选课时间 (模板)

    Problem Description 又到了选课的时间了,xhd看着选课表发呆,为了想让下一学期好过点,他想知道学n个学分共有多少组合.你来帮帮他吧.(xhd认为一样学分的课没区别) Input输入 ...

  8. 阿里Java开发规约(2)

    本文是对阿里插件中规约的详细解释二,关于插件使用,请参考这里 及时清理不再使用的代码段或配置信息. 说明:对于垃圾代码或过时配置,坚决清理干净,避免程序过度臃肿,代码冗余 Positive examp ...

  9. jenkins出现的问题

    1.增加服务器时 要修改时,,需要设置 2:出现这个问题是 执行了npm install node-sass

  10. 使用 Fetch完成AJAX请求

    使用 Fetch完成AJAX请求 写在前面 无论用JavaScript发送或获取信息,我们都会用到Ajax.Ajax不需要刷新页面就能发送和获取信息,能使网页实现异步更新. 几年前,初始化Ajax一般 ...