广告
返回顶部
首页 > 资讯 > 后端开发 > Python >一篇文章教带你了解Java Spring之自动装配
  • 891
分享到

一篇文章教带你了解Java Spring之自动装配

2024-04-02 19:04:59 891人浏览 安东尼

Python 官方文档:入门教程 => 点击学习

摘要

目录在spring中有三种装配的方式:1.Bean的自动装配1.1 autowire="byName" 实现自动装配1.2 autowire="byType" 实现自动装配2.注解实

在Spring中有三种装配的方式:

  • 在xml中显示的配置
  • 在java中显示配置
  • 隐式的自动装配bean

1.Bean的自动装配

自动装配是Spring满足bean依赖的一种方式,Spring会在上下文中自动寻找,并自动给bean装配属性。

1.1 autowire="byName" 实现自动装配

byname会自动在容器上下文中查找,和自己对象set方法后面的值对应的bean id。

需要保证所有bean的id唯一,并且这个bean需要和自动注入的属性的set方法的值一致。

People.java


package org.example;
public class People {
    private Cat cat;
    private Dog dog;
    private String name;
    public Cat getCat() {
        return cat;
    }
    public void setCat(Cat cat) {
        this.cat = cat;
    }
    public Dog getDog() {
        return dog;
    }
    public void setDog(Dog dog) {
        this.dog = dog;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    @Override
    public String toString() {
        return "People{" +
                "cat=" + cat +
                ", dog=" + dog +
                ", name='" + name + '\'' +
                '}';
    }
}

cat.java


package org.example;
public class Cat {
    public void shut(){
        System.out.println("喵喵喵……");
    }
}

Dog.java


package org.example;
public class Dog {
    public void shut(){
        System.out.println("汪汪汪……");
    }
}

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"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
    <bean id="cat" class="org.example.Cat"></bean>
    <bean id="dog" class="org.example.Dog"></bean>
    <bean id="people" class="org.example.People" autowire="byName">
        <property name="name" value="小狂神"></property>
    </bean>
</beans>

测试


package org.example;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class App {
    public static void main( String[] args ) {
        //获取ApplicationContext对象
        ApplicationContext application=new ClassPathXmlApplicationContext("ApplicationContext.xml");
        //通过ApplicationContext获得TestHello对象
        //getBean()方法中的参数即为配置文件中Bean的id的值
        People people=(People) application.getBean("people");
        people.getCat().shut();
        people.getDog().shut();
    }
}

1.2 autowire="byType" 实现自动装配

byType:会自动在容器上下文中查找,和自己对象属性类型相同的bean。

需要保证所有bean的class唯一,并且这个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"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
    <bean id="cat" class="org.example.Cat"></bean>
    <bean id="dog" class="org.example.Dog"></bean>
    <bean id="people" class="org.example.People" autowire="byType">
        <property name="name" value="小狂神"></property>
    </bean>
</beans>

2.注解实现自动装配

jdk1.5支持的注解,Spring2.5就支持注解了。

2.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:util="http://www.springframework.org/schema/util"
       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/util https://www.springframework.org/schema/util/spring-util.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">
<!--开启注解的支持-->
    <context:component-scan base-package="org.example"/>
</beans>

2.2 @Autowired注解

直接在属性上使用即可,也可以在set方式上使用。使用@Autowired 可以不用编写set方法了,前提是你这个自动装配的属性在ioc(Spring)容器中存在,且符合byname。

People.java


package org.example;
import org.springframework.beans.factory.annotation.Autowired;
public class People {
    @Autowired
    private Cat cat;
    @Autowired
    private Dog dog;
    private String name;
    public Cat getCat() {
        return cat;
    }
//set方法可以省略
    public void setCat(Cat cat) {
        this.cat = cat;
    }
    public Dog getDog() {
        return dog;
    }
    public void setDog(Dog dog) {
        this.dog = dog;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    @Override
    public String toString() {
        return "People{" +
                "cat=" + cat +
                ", dog=" + dog +
                ", name='" + name + '\'' +
                '}';
    }
}

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: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 https://www.springframework.org/schema/context/spring-context.xsd">
    <bean id="cat" class="org.example.Cat"></bean>
    <bean id="dog" class="org.example.Dog"></bean>
    <bean id="people" class="org.example.People" ></bean>
    <context:component-scan base-package="org.example"/>
</beans>

2.3 @Resource注解

People.java


package org.example;
import org.springframework.beans.factory.annotation.Autowired;
import javax.annotation.Resource;
public class People {
//如果没有(name="cat")那么就会找不到
    @Resource(name = "cat2")
    private Cat cat;
    @Resource
    private Dog dog;
    private String name;
    public Cat getCat() {
        return cat;
    }
    public void setCat(Cat cat) {
        this.cat = cat;
    }
    public Dog getDog() {
        return dog;
    }
    public void setDog(Dog dog) {
        this.dog = dog;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    @Override
    public String toString() {
        return "People{" +
                "cat=" + cat +
                ", dog=" + dog +
                ", name='" + name + '\'' +
                '}';
    }
}

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: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 https://www.springframework.org/schema/context/spring-context.xsd">
    <bean id="cat1" class="org.example.Cat"></bean>
    <bean id="cat2" class="org.example.Cat"></bean>
    <bean id="dog" class="org.example.Dog"></bean>
    <bean id="people" class="org.example.People" ></bean>
    <context:component-scan base-package="org.example"/>
</beans>

2.4小结

@Autowired和@Resource的区别:

  • 都是用来自动装配的,都可以放在属性字段上
  • @Autowired通过byType的方式实现,而且必须要求这个对象存在
  • @Resource默认通过byname的方式实现,如果找不到名字,则通过byType实现。如果两个都找不到的情况就会报错。
  • 执行顺序不同:@Autowired通过byType;@Resource默认通过byname的方式实现。

3.介绍一个idea中做笔记的小技巧

总结

本篇文章就到这里了,希望能够给你带来帮助,也希望您能够多多关注编程网的更多内容!

--结束END--

本文标题: 一篇文章教带你了解Java Spring之自动装配

本文链接: https://www.lsjlt.com/news/136115.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
  • 一篇文章教带你了解Java Spring之自动装配
    目录在Spring中有三种装配的方式:1.Bean的自动装配1.1 autowire="byName" 实现自动装配1.2 autowire="byType" 实现自动装配2.注解实...
    99+
    2022-11-12
  • 一篇文章带你了解初始Spring
    目录为什么要使用SpringSpring概述Spring容器使用流程1.启动容器2.完成bean的初始化3.注册bean到容器中4.装配bean的属性bean的注册bean属性注入总...
    99+
    2022-11-12
  • 一篇文章带你了解Python之Selenium自动化爬虫
    目录Python之Selenium自动化爬虫0.介绍1.安装2.下载浏览器驱动3.实例4.开启无头模式5.保存页面截图6.模拟输入和点击a.根据文本值查找节点b.获取当前节点的文本c...
    99+
    2022-11-13
  • 一篇文章带你了解Java Spring基础与IOC
    目录About SpringAbout IOCHello SpringHello.javaBeans.xmlTest.javaIOC创建对象的几种方式Spring import se...
    99+
    2022-11-12
  • 一篇文章带你深入了解Java封装
    目录如何实现封装代码展示构造方法注意点:代码展示总结如何实现封装 可以分为两步: 第一步:将类的变量声明为private。 第二步:提供公共set和get方法来修改和获取变量的值。 ...
    99+
    2022-11-12
  • 一篇文章带你了解Spring AOP 的注解
    目录1、xml 的方式实现 AOP①、接口 UserService②、实现类 UserServiceImpl③、切面类,也就是通知类 MyAspect④、AOP配置文件 applic...
    99+
    2022-11-13
  • 一篇文章带你了解jQuery动画
    目录1.控制元素的显示与隐藏 show() hide()2.控制元素的透明度 fadeIn() fadeOut()3:控制元素的高度 slideUp() slideDown()总结 ...
    99+
    2022-11-12
  • 一篇文章带你了解Java SpringBoot Nacos
    目录1、什么是Nacos 1.1与eureka对比1.2与zookeeper对比1.3与springcloud config 对比 2、Spring Cloud Alibaba 套件...
    99+
    2022-11-12
  • 一篇文章带你了解Java Stream流
    目录一、Stream流引入现有一个需求:1.用常规方法解决需求2.用Stream流操作集合,获取流,过滤操作,打印输出二、Stream流的格式三、获取流四、Stream流的常用方法方...
    99+
    2022-11-12
  • 一篇文章带你了解mybatis的动态SQL
    目录1、动态SQL:if 语句3、动态SQL:if+set 语句4、动态SQL:choose(when,otherwise) 语句5、动态SQL:trim 语句6、动态SQL: SQ...
    99+
    2022-11-13
  • 一篇文章带你深入了解Java异常
    目录一.初识异常1.常见的异常类型<1>除以0<2>数组下标越界<3>访问null对象2.防御式编程<1>LBYL<2>E...
    99+
    2022-11-12
  • 一篇文章带你深入了解Java基础
    目录1、String类1.1两种对象实例化方式1.2字符串比较1.3字符串常量是String的匿名对象1.4String两种实例化方式区别1、分析直接赋值方式2、构造方法赋值1.5字...
    99+
    2022-11-12
  • 一篇文章带你了解Java基础-抽象
    目录Java基础知识(抽象)抽象抽象定义abstract的使用定义抽象类抽象类的一些注意点总结Java基础知识(抽象) 抽象 抽象是从众多的事物中抽取出共同的、本质性的特征,而舍弃...
    99+
    2022-11-12
  • 一篇文章带你了解Java基础-接口
    目录Java基础知识(接口)接口接口的定义接口和抽象的区别接口的格式接口中的主要成分接口的案例接口与接口的关系JDK 8之后的接口新增方法总结Java基础知识(接口) 接口 Jav...
    99+
    2022-11-12
  • 一篇文章带你了解Java基础-多态
    目录Java基础知识(多态)多态多态的定义和存在的必要条件多态的案例多态的弊端引用类型转换总结Java基础知识(多态) 多态 多态就是指程序中定义的引用变量所指向的具体类型和通过该...
    99+
    2022-11-12
  • 一篇文章带你了解Java SpringMVC返回null
    目录1、回顾一下2、思考一个问题3、springmvc 的处理流程4、使用场景5、总结1、回顾一下 大家有没有注意到,目前讲到的所有 controller 中的方法接收到请求之后,都...
    99+
    2022-11-12
  • 一篇文章带你了解JAVA面对对象三大特征之封装
    目录面对对象的三大特征: 封装、继承、多态封装继承多态类和对象关于类关于对象三大特征第一封装关于封装思想 :private关键字this关键字构造方法构造方法注意事项总结面对对象的三...
    99+
    2022-11-12
  • 一篇文章带你了解JavaScript的包装类型
    目录1、简介2、String1、创建语法2、常用方法3、更多方法3、Number1、语法2、属性3、常用方法4、Boolean总结1、简介 【解释】: 在 JavaScri...
    99+
    2022-11-13
  • 一篇文章带你了解Python中的装饰器
    目录前言Python 中的装饰器是什么语法糖使用 Python 装饰器修改函数行为使用 Python 装饰器对函数进行计时使用 Python 装饰器将有用信息记录到终端Web app...
    99+
    2022-11-13
  • 一篇文章带你了解Java之关键字和保留字
    目录引言概念关键字分类1.访问控制2.类、方法和变量修饰符3.程序控制语句4.错误处理5包相关6.基本类型7.变量引用8.保留关键字9.其他(个人认为不是关键字、也不是保留字,但是,...
    99+
    2022-11-12
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作