2019-03-08/11:10:17

演示:使用注解的方式完成注入对象中的效果

注解参考链接:https://www.cnblogs.com/szlbm/p/5512931.html

Spring中id与name的区别:https://blog.csdn.net/qq_22063697/article/details/51912386

1.修改applicationContext.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:aop="http://www.springframework.org/schema/aop"
     xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context"
     xsi:schemaLocation="
    http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
    http://www.springframework.org/schema/aop
    http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
    http://www.springframework.org/schema/tx
    http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-3.0.xsd">

     <context:annotation-config/>   //告诉Spring用注解的方式进行配置
     <bean name="c" class="pojo.Category">
         <property name="name" value="category 1" />
     </bean>
     <bean name="p" class="pojo.Product">
         <property name="name" value="product1" />
 <!--         <property name="category" ref="c" /> -->   //用注解的方式替代
     </bean>

 </beans>

2.在Product.java的category属性前加上@Autowired注解

  @Autowired:顾名思义,就是自动装配,其作用是为了消除代码Java代码里面的getter/setter与bean属性中的property。当然,getter看个人需求,如果私有属性需要对外提供的话,应当予以保留。例子中统一不去掉get和set.

 package pojo;

 import org.springframework.beans.factory.annotation.Autowired;

 public class Product {

     private int id;
     private String name;
     @Autowired
     private Category category;

     public int getId() {
         return id;
     }

     public void setId(int id) {
         this.id = id;
     }

     public String getName() {
         return name;
     }

     public void setName(String name) {
         this.name = name;
     }

     public Category getCategory() {
         return category;
     }

     public void setCategory(Category category) {
         this.category = category;
     }
 }

Product.java

3.测试代码

 package test;

 import org.springframework.context.ApplicationContext;
 import org.springframework.context.support.ClassPathXmlApplicationContext;

 import pojo.Product;

 public class TestSpring {

     public static void main(String[] args) {
         ApplicationContext context = new ClassPathXmlApplicationContext(new String[] { "applicationContext.xml" });
         Product p = (Product) context.getBean("p");
         System.out.println(p.getName());
         System.out.println(p.getCategory().getName());
     }
 }

TestSpring

4.@Autowired的位置

除了前面的在属性前加上@Autowired这种方式外,也可以在setCategory方法前加上@Autowired,这样来达到相同的效果

 package pojo;

 import org.springframework.beans.factory.annotation.Autowired;

 public class Product {

     private int id;
     private String name;

     private Category category;

     public int getId() {
         return id;
     }

     public void setId(int id) {
         this.id = id;
     }

     public String getName() {
         return name;
     }

     public void setName(String name) {
         this.name = name;
     }

     public Category getCategory() {
         return category;
     }
     @Autowired
     public void setCategory(Category category) {
         this.category = category;
     }
 }

5.@Resource

@Resource注解与@Autowired它们作用非常相似,这个就简单说了,例子过后点明一下@Resource和@Autowired的区别。先看一下@Resource,直接写Product.java了:

 package pojo;

 import javax.annotation.Resource;

 public class Product {

     private int id;
     private String name;
     @Resource(name="c")
     private Category category;

     public int getId() {
         return id;
     }

     public void setId(int id) {
         this.id = id;
     }

     public String getName() {
         return name;
     }

     public void setName(String name) {
         this.name = name;
     }

     public Category getCategory() {
         return category;
     }

     public void setCategory(Category category) {
         this.category = category;
     }
 }

Product.java

6.@Resource与@Autowired 

@Resource的装配顺序:

1、@Resource后面没有任何内容,默认通过name属性去匹配bean,找不到再按type去匹配

2、指定了name或者type则根据指定的类型去匹配bean

3、指定了name和type则根据指定的name和type去匹配bean,任何一个不匹配都将报错

然后,区分一下@Autowired和@Resource两个注解的区别:

1、@Autowired默认按照byType方式进行bean匹配,@Resource默认按照byName方式进行bean匹配

2、@Autowired是Spring的注解,@Resource是J2EE的注解,这个看一下导入注解的时候这两个注解的包名就一清二楚了

Spring属于第三方的,J2EE是Java自己的东西,因此,建议使用@Resource注解,以减少代码和Spring之间的耦合。

7.对Bean的注解  @Component

上面这个例子,还可以继续简化,因为spring的配置文件里面还有16行~20行两个个bean,下一步的简化是把这两个bean也给去掉,使得spring配置文件里面只有一个自动扫描的标签,增强Java代码的内聚性并进一步减少配置文件。

要继续简化,可以使用@Componet。先看一下配置文件,当然是全部删除了:

<context:component-scan base-package="pojo"/>        其作用时告诉Spring,bean都放在pojo这个包下面

 <?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:aop="http://www.springframework.org/schema/aop"
     xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context"
     xsi:schemaLocation="
    http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
    http://www.springframework.org/schema/aop
    http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
    http://www.springframework.org/schema/tx
    http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-3.0.xsd">

     <context:component-scan base-package="pojo"/>

 </beans>

8.@Component

  Product类加上@Component注解,即表明此类是bean       @Autowired注入Category对象不能取消

  Category类加上@Component注解,即表明此类是bean

  另外,因为配置从applicationContext.xml中移出来了,所以属性初始化放在属性声明上进行了。

  

 package pojo;

 import javax.annotation.Resource;

 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Component;

 @Component("p")
 public class Product {

     private int id;
     private String name="product 1";

     @Autowired
     private Category category;

     public int getId() {
         return id;
     }

     public void setId(int id) {
         this.id = id;
     }

     public String getName() {
         return name;
     }

     public void setName(String name) {
         this.name = name;
     }

     public Category getCategory() {
         return category;
     }

     public void setCategory(Category category) {
         this.category = category;
     }
 }

Product.java

 package pojo;

 import org.springframework.stereotype.Component;

 @Component("c")
 public class Category {

     private int id;
     private String name="category 1";

     public int getId() {
         return id;
     }
     public void setId(int id) {
         this.id = id;
     }
     public String getName() {
         return name;
     }
     public void setName(String name) {
         this.name = name;
     }

 }

Category.java

 package test;

 import org.springframework.context.ApplicationContext;
 import org.springframework.context.support.ClassPathXmlApplicationContext;

 import pojo.Product;

 public class TestSpring {

     public static void main(String[] args) {
         ApplicationContext context = new ClassPathXmlApplicationContext(new String[] { "applicationContext.xml" });
         Product p = (Product) context.getBean("p");
         System.out.println(p.getName());
         System.out.println(p.getCategory().getName());
     }
 }

TestSpring.java

Spring注解IOC/DI(4)的更多相关文章

  1. Spring框架-IOC/DI详细学习

    一.IOC/DI概念 参考博客:https://www.cnblogs.com/xdp-gacl/p/4249939.html IOC(inversion of control, 控制反转)是一种设计 ...

  2. spring的IOC/DI功能实践

    一.写在前面: 做这个Demo主要是为了能够更好的理解Spring的原理,看再多的文章,听再多的讲解最终都最好自己去实现一遍,可以将Spring的功能分块实现,最终自然比较容易将各个功能组合起来. 这 ...

  3. Spring的IOC/DI使用到的技术

    一.了解Spring IOC/DI 1:Spring有两大核心技术,控制反转(Inversion of Control, IOC)/依赖注入(Dependency Injection,DI)和面向切面 ...

  4. Spring之IOC/DI(反转控制/依赖注入)_入门Demo

    在平时的java应用开发中,我们要实现某一个功能或者说是完成某个业务逻辑时至少需要两个或以上的对象来协作完成,在没有使用Spring的时候,每个对象在需要使用他的合作对象时,自己均要使用像new ob ...

  5. Spring框架——IOC&DI

    Spring Spring 目标 内容 Spring与web整合的原理 Spring 中包含的关键特性 Spring架构图 企业级框架 企业级系统 IOCDI IOC DI IOC和DI 为什么使用依 ...

  6. 个人对spring的IOC+DI的封装

    暂时支持8种基本数据类型,String类型,引用类型,List的注入. 核心代码 package day01; import java.lang.reflect.Field;import java.l ...

  7. Spring基础[IOC/DI、AOP]

    一.Spring作用:管理项目中各种业务Bean(service类.Dao类.Action类),实例化类,属性赋值 二.Spring IOC(Inversion of Control )控制反转,也被 ...

  8. Spring理解IOC,DI,AOP作用,概念,理解。

    IOC控制反转:创建实例对象的控制权从代码转换到Spring容器.实际就是在xml中配置.配置对象 实例化对象时,进行强转为自定义类型.默认返回类型是Object强类型. ApplicationCon ...

  9. 解释Spring中IOC, DI, AOP

    oc就是控制翻转或是依赖注入.通俗的讲就是如果在什么地方需要一个对象,你自己不用去通过new 生成你需要的对象,而是通过spring的bean工厂为你长生这样一个对象.aop就是面向切面的编程.比如说 ...

随机推荐

  1. React + TypeScript:元素引用的传递

    React 中需要操作元素时,可通过 findDOMNode() 或通过 createRef() 创建对元素的引用来实现.前者官方不推荐,所以这里讨论后者及其与 TypeScript 结合时如何工作. ...

  2. 《HelloGitHub》第 33 期

    公告 欢迎 点击分享 自荐或发现的开源项目,也可安装 分享插件 更便捷地推荐有趣的开源项目. 小伙伴们,新的一年就要来了,今年的 Bug 改完了吗?先看看<HelloGitHub>最新一期 ...

  3. 一文带你认识Spring事务

    前言 只有光头才能变强. 文本已收录至我的GitHub仓库,欢迎Star:https://github.com/ZhongFuCheng3y/3y Spring事务管理我相信大家都用得很多,但可能仅仅 ...

  4. 从零单排学Redis【铂金一】

    前言 只有光头才能变强 好的,今天我们要上铂金段位了,如果还没经历过青铜和白银和黄金阶段的,可以先去蹭蹭经验再回来: 从零单排学Redis[青铜] 从零单排学Redis[白银] 从零单排学Redis[ ...

  5. Dubbo Mesh 在闲鱼生产环境中的落地实践

    本文作者至简曾在 2018 QCon 上海站以<Service Mesh 的本质.价值和应用探索>为题做了一次分享,其中谈到了 Dubbo Mesh 的整体发展思路是“借力开源.反哺开源” ...

  6. C#如何根据类的名词创建类的实例

    这个大概分为两种情况:1-在同一程序集访问该类:2-在不同的程序集访问 A:同一程序集,使用微软的创建对象的类:System.Activator: 先通过类名,获取到类型,在使用用于创建本地或远程对象 ...

  7. C# 打印PDF文档的10种方法

    操作PDF文档时,打印是常见的需求之一.针对不同的打印需求,可分多种情况来进行,如设置静默打印.指定打印页码范围和打印纸张大小.双面打印.黑白打印等等.经过测试,下面将对常见的几种PDF打印需求做一些 ...

  8. 设计模式之结构类模式PK

    结构类模式包括: 适配器模式 桥梁模式 组合模式 装饰模式 门面模式 享元模式 代理模式 结构类模式着重于如何建立一个软件结构 为什么叫结构类模式呢? 因为他们都是通过组合类或对象产生更大结构以适应更 ...

  9. 重庆3Shape TRIOS都有哪些功能

    1.高质量的临床结果高准确度的数字化印模确保高质量的修复体密合度即时制备和印模确认就位修复体时无需多次调整和研磨2.轻松取模无需喷粉扫描无需印模材料,可保持操作台整洁无需重复取模3.更稳健的业务联系加 ...

  10. Liunx-cd命令

    1. 如何进入上级目录cd .. 2. 如何进入当前用户主目录cd ~3. 如何进入上两级目录cd ../.. 4. 进入当前目录命令cd .5. 如何进入目录  /lym/b 6.切换跟目录