spring学习日记-day3-bean的自动装配

一、学习目标

        Spring的装配方式多种多样,这些方式旨在帮助开发者以灵活和高效的方式管理应用中的bean(即对象)。

二、bean的自动装配

1.spring的装配方式

1. XML配置方式

XML配置方式是通过XML文件来描述Bean的配置信息。开发者可以在XML文件中定义Bean的id、class、属性以及Bean之间的依赖关系等。Spring的BeanFactory或ApplicationContext会加载和解析这些配置文件,进而创建和管理Bean的实例。

2. 注解方式

注解方式是Spring装配的另一种重要方式。通过使用注解,开发者可以在类、方法或字段上直接声明Bean的装配信息,而无需编写繁琐的XML配置文件。如@Component@Repository@Service@Controller等,这些注解用于标识类为Spring管理的组件

3. Java配置方式

Java配置方式允许开发者通过编写Java代码来定义Bean的配置信息。这种方式通常使用@Configuration注解来标识一个配置类,并使用@Bean注解来定义Bean的创建方法。通过这种方式,开发者可以摆脱XML配置的束缚,以更灵活的方式管理Bean。

4. 自动装配方式

自动装配是Spring提供的一种高级功能,它可以根据Bean之间的依赖关系自动完成Bean的装配。Spring支持多种自动装配的方式,包括根据类型(byType)、根据名称(byName)和根据构造函数参数(constructor)进行自动装配。开发者可以通过在配置文件中设置<bean>标签的autowire属性,或者在Java配置中使用@Autowired注解来启用自动装配。

2.搭建测试环境

项目结构

实体类


public class person {
    private friend friend;
    private girlFriend girlFriend;
    private  String name;

    public person() {
    }

    public person(com.lzh.pojo.friend friend, com.lzh.pojo.girlFriend girlFriend, String name) {
        this.friend = friend;
        this.girlFriend = girlFriend;
        this.name = name;
    }

    public com.lzh.pojo.friend getFriend() {
        return friend;
    }

    public void setFriend(com.lzh.pojo.friend friend) {
        this.friend = friend;
    }

    public com.lzh.pojo.girlFriend getGirlFriend() {
        return girlFriend;
    }

    public void setGirlFriend(com.lzh.pojo.girlFriend girlFriend) {
        this.girlFriend = girlFriend;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
    public void show(){
        System.out.println("my friend is "+this.friend.getName());
        System.out.println("my girlfriend is "+this.girlFriend.getName());
    }

}
public class friend {
    private  String name;

    public friend() {
    }

    public String getName() {
        return name;
    }

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

    public friend(String name) {
        this.name = name;
    }
}
public class girlFriend {
    private String name;

    public girlFriend() {
    }

    public girlFriend(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }

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

xml文件

    <bean  id="friend" class="com.lzh.pojo.friend">
       <property name="name" value="zsy"></property>
   </bean>
    <bean id="girlFriend" class="com.lzh.pojo.girlFriend">
        <property name="name" value="xxx"></property>
    </bean>
    <bean id="person" class="com.lzh.pojo.person">
        <property name="name" value="lzh"></property>
        <property name="friend" ref="friend"></property>
        <property name="girlFriend" ref="girlFriend"></property>
    </bean>

3.byname

        当使用byName自动装配时,Spring容器会查找与需要被注入的bean的属性名相匹配的bean的id或name,并将其注入到该属性中。如果找不到匹配的bean,那么自动装配将会失败。

     <bean  id="friend" class="com.lzh.pojo.friend">
       <property name="name" value="zsy"></property>
   </bean>
    <bean id="girlFriend" class="com.lzh.pojo.girlFriend">
        <property name="name" value="xxx"></property>
    </bean>
    <!--bean id="person" class="com.lzh.pojo.person">
        <property name="name" value="lzh"></property>
        <property name="friend" ref="friend"></property>
        <property name="girlFriend" ref="girlFriend"></property>
    </bean-->
    <bean id="person" class="com.lzh.pojo.person" autowire="byName">
        <property name="name" value="qinjiang"/>
    </bean>

4.bytype

        当使用byType自动装配时,Spring容器会查看需要被注入的bean的属性类型,然后在容器中找到相同类型的bean进行注入。如果容器中存在多个相同类型的bean,那么这种自动装配方式将会失败,因为它无法确定应该注入哪一个bean。

    <bean  id="friend" class="com.lzh.pojo.friend">
       <property name="name" value="zsy"></property>
   </bean>
    <bean id="girlFriend" class="com.lzh.pojo.girlFriend">
        <property name="name" value="xxx"></property>
    </bean>
    <!--bean id="person" class="com.lzh.pojo.person">
        <property name="name" value="lzh"></property>
        <property name="friend" ref="friend"></property>
        <property name="girlFriend" ref="girlFriend"></property>
    </bean-->
    <bean id="person" class="com.lzh.pojo.person" autowire="byType">
        <property name="name" value="qinjiang"/>
    </bean>

测试

@Test
    public void test1() {
        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
        person person =(person) context.getBean("person");
        person.show();
    }

5.使用注解进行装配

<?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
        https://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        https://www.springframework.org/schema/context/spring-context.xsd">

    <context:annotation-config/>
   
</beans>

1.@Autowired

        @Autowired 是 Spring 框架中的一个非常核心和常用的注解,它用于实现依赖的自动注入。当你在 Spring 管理的 bean 的字段、setter 方法或构造函数上标注 @Autowired 注解时,Spring 容器会自动查找匹配的 bean 并将其注入到被注解的字段、方法参数或构造函数的参数中。

实体类:

public class person {
    @Autowired
    private friend friend;
    @Autowired
    private girlFriend girlFriend;
    private  String name;

    public person() {
    }

    public person(com.lzh.pojo.friend friend, com.lzh.pojo.girlFriend girlFriend, String name) {
        this.friend = friend;
        this.girlFriend = girlFriend;
        this.name = name;
    }

    public void show(){
        System.out.println("my friend is "+this.friend.getName());
        System.out.println("my girlfriend is "+this.girlFriend.getName());
    }

}

    <bean  id="friend" class="com.lzh.pojo.friend">
       <property name="name" value="zsy"></property>
   </bean>
    <bean id="girlFriend" class="com.lzh.pojo.girlFriend">
        <property name="name" value="xxx"></property>
    </bean>
    <bean id="person" class="com.lzh.pojo.person"></bean>

2.@Qualifier

    @Qualifier 注解在Spring框架中用于在自动装配时提供额外的限定符,以便在存在多个相同类型的bean时,能够精确地指定应该注入哪一个bean。这个注解经常与@Autowired注解一起使用,以解决byType自动装配可能产生的歧义。

实体类

public class person {
    @Autowired
    @Qualifier( value = "friend2")
    private friend friend;
    @Autowired
    @Qualifier( value = "girlFriend2")
    private girlFriend girlFriend;
    private  String name;

    public person() {
    }

    public person(com.lzh.pojo.friend friend, com.lzh.pojo.girlFriend girlFriend, String name) {
        this.friend = friend;
        this.girlFriend = girlFriend;
        this.name = name;
    }

    public void show(){
        System.out.println("my friend is "+this.friend.getName());
        System.out.println("my girlfriend is "+this.girlFriend.getName());
    }

}

xml文件:

    <bean  id="friend1" class="com.lzh.pojo.friend">
       <property name="name" value="zsy"></property>
   </bean>
    <bean  id="friend2" class="com.lzh.pojo.friend">
        <property name="name" value="jcx"></property>
    </bean>
    <bean id="girlFriend1" class="com.lzh.pojo.girlFriend">
        <property name="name" value="xxx"></property>
    </bean>
    <bean id="girlFriend2" class="com.lzh.pojo.girlFriend">
        <property name="name" value="yyy"></property>
    </bean>

使用之前的测试方法:

3.@Resource

    @Resource 注解可以用于字段、setter 方法或构造函数上,以指定 Spring 容器应该注入哪个 bean。与 @Autowired 类似,它也可以用于自动装配 bean,但它在查找匹配的 bean 时具有不同的行为。@Autowired 默认按类型(byType)装配依赖项,而 @Resource 默认按名称(byName)装配。如果 @Resource 没有显式指定名称,并且按名称装配失败,那么它会回退到按类型装配

在pom.xml导入依赖

        <dependency>
            <groupId>jakarta.annotation</groupId>
            <artifactId>jakarta.annotation-api</artifactId>
            <version>2.1.1</version>
        </dependency>

实体类

public class person {
    @Resource
    private friend friend;
    @Resource
    private girlFriend girlFriend;

    private  String name;

    public person() {
    }

    public person(com.lzh.pojo.friend friend, com.lzh.pojo.girlFriend girlFriend, String name) {
        this.friend = friend;
        this.girlFriend = girlFriend;
        this.name = name;
    }

    public void show(){
        System.out.println("my friend is "+this.friend.getName());
        System.out.println("my girlfriend is "+this.girlFriend.getName());
    }

}

xml文件:

     <bean  id="friend" class="com.lzh.pojo.friend">
       <property name="name" value="zsy"></property>
    </bean>
    <!--bean  id="friend2" class="com.lzh.pojo.friend">
        <property name="name" value="jcx"></property>
    </bean-->
    <bean id="girlFriend" class="com.lzh.pojo.girlFriend">
        <property name="name" value="xxx"></property>
    </bean>
    <bean id="person" class="com.lzh.pojo.person"></bean>

测试:

  • 17
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
根据提供的引用内容,spring-boot-starter-data-redis是Spring Boot中用于自动装配Redis的starter包。它包含了自动装配所需的类和注解等。当我们在项目的pom.xml文件中引入spring-boot-starter-data-redis包时,Spring Boot会自动根据配置文件中的相关配置信息来完成Redis的自动装配。 具体来说,spring-boot-starter-data-redis使用了RedisAutoConfiguration类来实现自动装配。该类通过读取配置文件中的相关配置信息,例如主机名、端口号、密码等,来创建Redis连接工厂和RedisTemplate等实例。这些实例可以在应用程序中直接使用,而无需手动配置和初始化。 下面是一个示例代码,展示了如何使用spring-boot-starter-data-redis进行自动装配: ```java import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.data.redis.core.RedisTemplate; @SpringBootApplication public class RedisApplication { private final RedisTemplate<String, String> redisTemplate; public RedisApplication(RedisTemplate<String, String> redisTemplate) { this.redisTemplate = redisTemplate; } public static void main(String[] args) { SpringApplication.run(RedisApplication.class, args); } // 在需要使用Redis的地方,可以直接注入RedisTemplate实例,并进行操作 // 例如: // redisTemplate.opsForValue().set("key", "value"); // String value = redisTemplate.opsForValue().get("key"); } ``` 通过上述代码,我们可以看到,在Spring Boot应用程序中,我们只需要在需要使用Redis的地方注入RedisTemplate实例,就可以直接使用Redis的相关操作方法。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值