注解实现自动装配

自动装配说明

  • 自动装配是使用spring满足bean依赖的一种方法
  • spring会在应用上下文中为某个bean寻找其依赖的bean。

Spring中bean有三种装配机制

在xml中显式配置;
在java中显式配置;
隐式的bean发现机制和自动装配。

Spring的自动装配需要从两个角度来实现,或者说是两个操作:

组件扫描(component scanning):spring会自动发现应用上下文中所创建的bean;
自动装配(autowiring):spring自动满足bean之间的依赖,也就是我们说的IOC/DI;

组件扫描和自动装配组合发挥巨大威力,使得显示的配置降低到最少。

推荐不使用自动装配xml配置 , 而使用注解 .

jdk1.5开始支持注解,spring2.5开始全面支持注解。

@Autowired与@Resource异同

  • @Autowired是Spring的注解,@Resource是java的注解

  • @Autowired与@Resource都可以用来装配bean。都可以写在字段上,或写在setter方法上。

  • @Autowired默认按类型装配(属于spring规范),默认情况下必须要求依赖对象必须存在,如果要允许null 值,可以设置它的required属性为false,如:@Autowired(required=false) ,如果我们想使用名称装配可以结合@Qualifier注解进行使用

  • @Resource(属于J2EE规范),默认按照名称进行装配,名称可以通过name属性进行指定。如果没有指定name属性,当注解写在字段上时,默认取字段名进行按照名称查找,如果注解写在setter方法上默认取属性名进行装配。当找不到与名称匹配的bean时才按照类型进行装配。但是需要注意的是,如果name属性一旦指定,就只会按照名称进行装配。

    它们的作用相同都是用注解方式注入对象,但执行顺序不同。@Autowired先byType,@Resource先byName。

@Autowired

@Autowired是按类型自动转配的,不支持id匹配。

实体类

public class People {

    @Autowired
    private Cat cat;
    @Autowired
    private Dog dog;
    private String name;

    public Cat getCat() {return cat;}
    public Dog getDog() {return dog;}

}

xml文件

<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
           http://www.springframework.org/schema/context/spring-context.xsd"">
    <!--开启注解支持-->
    <context:annotation-config/>
    <bean id=""cat"" class=""com.yv.pojo.Cat""/>
    <bean id=""dog"" class=""com.yv.pojo.Dog""/>
    <bean id=""people"" class=""com.yv.pojo.People""/>
</beans>

@Autowired(required=false) 说明:false,对象可以为null;true,对象必须存对象,不能为null。

//如果允许对象为null,设置required = false,默认为true
 @Autowired(required = false)
 private Cat cat;

@Qualifier

  • @Autowired是根据类型自动装配的,加上@Qualifier则可以根据byName的方式自动装配
  • @Qualifier不能单独使用。

测试实验步骤:

1、配置文件修改内容,保证类型存在对象。且名字不为类的默认名字!

<bean id=""dog1"" class=""com.yv.pojo.Dog""/>	
<bean id=""dog2"" class=""com.yv.pojo.Dog""/>
<bean id=""cat1"" class=""com.yv.pojo.Cat""/>
<bean id=""cat2"" class=""com.yv.pojo.Cat""/>

2、没有加Qualifier测试,直接报错

3、在属性上添加Qualifier注解

@Autowired
@Qualifier(value = ""cat2"")
private Cat cat;
@Autowired
@Qualifier(value = ""dog2"")
private Dog dog;

@Resource

  • @Resource如有指定的name属性,先按该属性进行byName方式查找装配;
  • 其次再进行默认的byName方式进行装配;
  • 如果以上都不成功,则按byType的方式自动装配。
  • 都不成功,则报异常。

实体类

public class User {   
 //如果允许对象为null,设置required = false,默认为true   
 @Resource(name = ""cat2"")   
 private Cat cat;   
 @Resource  
 private Dog dog;   
 private String str;
}

xml文件

<bean id=""dog"" class=""com.kuang.pojo.Dog""/>
<bean id=""cat1"" class=""com.kuang.pojo.Cat""/>
<bean id=""cat2"" class=""com.kuang.pojo.Cat""/>
<bean id=""user"" class=""com.kuang.pojo.User""/>

配置文件2:beans.xml , 删掉cat2

<bean id=""dog"" class=""com.kuang.pojo.Dog""/>
<bean id=""cat1"" class=""com.kuang.pojo.Cat""/>

实体类上只保留注解

@Resource
private Cat cat;
@Resource
private Dog dog;

先进行byName查找,失败;再进行byType查找,成功。

Bean的自动装配

Spring IOC容器可以自动装配Bean,需要做的仅仅实在的autowire属性里指定自动装配的模式

byType(根据类型自动装配)

若IOC容器中有多个与目标Bean类型一致的Bean。在这种情况下,Spring将无法判断哪个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=""com.yv.pojo.Cat""></bean>
    <bean id=""dog"" class=""com.yv.pojo.Dog""></bean>

    <bean id=""people"" class=""com.yv.pojo.People"" autowire=""byName"">
        <property name=""name"" value=""大恐龙""/>
    </bean>

</beans>

byName(根据名称自动装配)

必须将目标Bean的名称和属性名设置的完全相同。

constructor(通过构造器自动装配):当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 class=""com.yv.pojo.Cat""></bean>
    <bean class=""com.yv.pojo.Dog""></bean>

    <bean id=""people"" class=""com.yv.pojo.People"" autowire=""byType"">
        <property name=""name"" value=""大恐龙""/>
    </bean>

</beans>

自动装配的缺点

  1. 属性要么都是用自动装配的形式,要么不用,不能部分使用,部分不使用

  2. 要么byName,要么byType,不能兼用

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在Spring Boot中,使用自动装配(Auto Configuration)可以简化配置和组件的使用,提高开发效率。自动装配是通过使用注解实现的,以下是实现Spring Boot自动装配注解方式: 1. @EnableAutoConfiguration @EnableAutoConfiguration注解是启用自动配置的注解,它会自动扫描classpath下的META-INF/spring.factories文件中的所有配置类,并将它们自动装配到Spring容器中。 2. @ConditionalOnClass @ConditionalOnClass注解可以用来指定条件,只有在classpath中存在指定的类时才会进行自动配置。例如,只有当类com.example.MyClass存在时才会进行自动配置。 3. @ConditionalOnBean @ConditionalOnBean注解可以用来指定条件,只有在Spring容器中存在指定的Bean时才会进行自动配置。例如,只有当Bean com.example.MyBean存在时才会进行自动配置。 4. @ConditionalOnProperty @ConditionalOnProperty注解可以用来指定条件,只有当指定的配置项存在且值为指定值时才会进行自动配置。例如,只有当配置项myapp.enabled=true时才会进行自动配置。 5. @ConfigurationProperties @ConfigurationProperties注解可以用来将配置文件中的属性绑定到JavaBean中。例如,可以将application.properties中的myapp.name属性绑定到JavaBean的name属性上。 6. @ComponentScan @ComponentScan注解可以用来指定要扫描的包路径,这样Spring Boot就可以自动装配指定包路径下的组件。例如,可以使用@ComponentScan(basePackages={"com.example.controller"})来指定要扫描的控制器包路径。 7. @SpringBootApplication @SpringBootApplication注解是Spring Boot的核心注解,它包含了@EnableAutoConfiguration、@ComponentScan和@Configuration三个注解,可以简化配置和组件的使用。使用@SpringBootApplication注解可以让Spring Boot自动扫描并装配所有的组件。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值