Spring 普通的注解

44 篇文章 0 订阅
37 篇文章 0 订阅

样式类:

package com.yyf.pojo;

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

import javax.annotation.Resource;

public class People {
    @Resource(name="dog11")
//    @Autowired  自动分配  通过byType实现
//    @Qualifier(value="dog111") ==  @Resource(name="dog11")   resource用byName实现
  private Dog dog;
//    @Autowired
//    @Qualifier(value="cat111")  //当类有多个bean对象时,用Qualifier 指定bean
    @Resource(name="cat111")
    private Cat cat;
  private String name;

    public People() {
    }

    public People(Dog dog, Cat cat, String name) {
        this.dog = dog;
        this.cat = cat;
        this.name = name;
    }

    public Dog getDog() {
        return dog;
    }



    public Cat getCat() {
        return cat;
    }



    public String getName() {
        return name;
    }

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

    @Override
    public String toString() {
        return "People{" +
                "dog=" + dog +
                ", cat=" + cat +
                ", name='" + name + '\'' +
                '}';
    }
}

spring 配置文件 

   byName:会自动寻找容器上下文和自己对象set后面值相对应的beanID  必须保证beanId唯一
  byType:
当我们要往一个bean的某个属性里注入另外一个bean,我们会使用<property> + <ref/>标签的形式。但是对于大型项目,假设有一个bean A被多个bean引用注入,如果A的id因为某种原因修改了,那么所有引用了A的bean的<ref/>标签内容都得修改,这时候如果使用autowire="byType",那么引用了A的bean就完全不用修改了。
  <bean id="people" class="com.yyf.pojo.People" autowire="byName"/>
   <bean id="people" class="com.yyf.pojo.People" autowire="byType">
     <property name="name" value="杨小法"/>

<?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"
       xmlns:aop="http://www.springframework.org/schema/aop"
       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
        http://www.springframework.org/schema/aop
        https://www.springframework.org/schema/context/spring-aop.xsd">
<!--开启注解的支持-->
   <context:annotation-config/>
<bean id="cat" class="com.yyf.pojo.Cat"/>
<bean id="cat111" class="com.yyf.pojo.Cat"/>
<bean id="dog111" class="com.yyf.pojo.Dog"/>
<bean id="dog11" class="com.yyf.pojo.Dog"/>
<bean id="people" class="com.yyf.pojo.People"/>
</beans>



<!--   byName会自动寻找容器上下文和自己对象set后面值相对应的beanID  必须保证beanid唯一-->
<!--  byType会自动寻找容器上下文和自己对象set后面类型相对应的beanID   但使用byType时,一个类不能有多个bean 不声明id也行 与类类型所对应的类型就行
    必须保证class唯一-->
<!--   <bean id="people" class="com.yyf.pojo.People" autowire="byName"/>-->
<!--   <bean id="people" class="com.yyf.pojo.People" autowire="byType">-->
<!--      <property name="name" value="杨小法"/>-->

<!--   </bean>-->

   @Autowired  自动分配  通过byType实现@Qualifier(value="dog111")  指定bean

 //当类有多个bean对象时,用Qualifier 指定bean==@Resource(name="dog11")   

<!--开启注解的支持-->
   <context:annotation-config/>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值