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

 更新时间:2021年09月18日 09:24:18   作者:可爱多一点@  
今天小编就为大家分享一篇关于Spring中的自动装配,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看吧

在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中做笔记的小技巧

总结

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

相关文章

  • Java 自定义线程池和线程总数控制操作

    Java 自定义线程池和线程总数控制操作

    这篇文章主要介绍了Java 自定义线程池和线程总数控制操作,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2021-02-02
  • Java单线程ThreadLocal串值问题解决方案

    Java单线程ThreadLocal串值问题解决方案

    这篇文章主要介绍了Java单线程ThreadLocal串值问题解决方案,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2020-04-04
  • java:程序包org.apache.ibatis.annotations不存在报错解决

    java:程序包org.apache.ibatis.annotations不存在报错解决

    这篇文章主要给大家介绍了关于java:程序包org.apache.ibatis.annotations不存在报错的解决方法,这个错误是我在直接导入springboot项目的时候报错的,文中通过图文介绍的非常详细,需要的朋友可以参考下
    2023-04-04
  • java分布式事务seata的使用方式

    java分布式事务seata的使用方式

    这篇文章主要介绍了java分布式事务seata的使用方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教
    2024-04-04
  • springboot一个自定义注解如何搞定多线程事务

    springboot一个自定义注解如何搞定多线程事务

    文章介绍了Spring Boot中使用`@Async`注解进行声明式多线程编程的方法,以及如何通过自定义注解和AOP实现多线程事务控制,同时,还解释了`CountDownLatch`的使用场景及其工作原理
    2024-12-12
  • Java享元设计模式优化对象创建提高性能和效率

    Java享元设计模式优化对象创建提高性能和效率

    Java享元设计模式通过共享可重用的对象,减少了系统中对象的数量,优化了对象的创建和管理,提高了性能和效率。它是一种经典的设计模式,适用于需要处理大量相似对象的应用程序
    2023-04-04
  • Springboot Caffeine本地缓存使用示例

    Springboot Caffeine本地缓存使用示例

    这篇文章主要介绍了Springboot Caffeine本地缓存使用示例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2020-11-11
  • Java8如何通过Lambda处理List集合

    Java8如何通过Lambda处理List集合

    这篇文章主要介绍了java8如何通过Lambda处理List集合,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2020-01-01
  • 关于SpingMVC的<context:component-scan>包扫描踩坑记录

    关于SpingMVC的<context:component-scan>包扫描踩坑记录

    这篇文章主要介绍了关于SpingMVC的<context:component-scan>包扫描踩坑记录,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2022-03-03
  • Java中构造、生成XML简明教程

    Java中构造、生成XML简明教程

    这篇文章主要介绍了Java中构造、生成XML简明教程,本文通过dom4j包来完成,需要的朋友可以参考下
    2014-08-08

最新评论