spring的依赖注入(DI)

依赖注入

  • 依赖注入。

  • 依赖:指Bean对象的创建依赖于容器,Bean对象的依赖于资源

  • 注入:指Bean对象所依赖的资源,由容器来设置和装配

  • 构造器注入,之前的写过

  • Set注入

要求被注入的属性,必须有set方法,set方法的方法名由set+属性首字母大写。如果属性是boolean类型,没有set方法,是is

public class Address {
    private String address;

    public Address() {
    }

    public Address(String address) {
        this.address = address;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }
	@Override
    public String toString() {
        return "Address{" +
                "address='" + address + '\'' +
                '}';
    }
}
public class Student {
    private String name;
    private Address address;
    private String[] books;
    private List<String> hobbys;
    private Map<String,String> card;
    private Set<String> games;
    private Properties info;
    private String wife;

    public Student() {
    }

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", address=" + address +
                ", books=" + Arrays.toString(books) +
                ", hobbys=" + hobbys +
                ", card=" + card +
                ", games=" + games +
                ", info=" + info +
                ", wife='" + wife + '\'' +
                '}';
    }

    public String getName() {
        return name;
    }

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

    public Address getAddress() {
        return address;
    }

    public void setAddress(Address address) {
        this.address = address;
    }

    public String[] getBooks() {
        return books;
    }

    public void setBooks(String[] books) {
        this.books = books;
    }

    public List<String> getHobbys() {
        return hobbys;
    }

    public void setHobbys(List<String> hobbys) {
        this.hobbys = hobbys;
    }

    public Map<String, String> getCard() {
        return card;
    }

    public void setCard(Map<String, String> card) {
        this.card = card;
    }

    public Set<String> getGames() {
        return games;
    }

    public void setGames(Set<String> games) {
        this.games = games;
    }

    public Properties getInfo() {
        return info;
    }

    public void setInfo(Properties info) {
        this.info = info;
    }

    public String getWife() {
        return wife;
    }

    public void setWife(String wife) {
        this.wife = wife;
    }

    public Student(String name, Address address, String[] books, List<String> hobbys, Map<String, String> card, Set<String> games, Properties info, String wife) {
        this.name = name;
        this.address = address;
        this.books = books;
        this.hobbys = hobbys;
        this.card = card;
        this.games = games;
        this.info = info;
        this.wife = wife;
    }

spring的配置

<?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="address" class="com.zhou.pojo.Address">
        <property name="address" value="桂林"/>
    </bean>
    <bean id="student" class="com.zhou.pojo.Student">
        <property name="name" value="狂神"/>
    </bean>
</beans>

测试

 @Test
    public void test(){
        ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
        Student student = (Student) context.getBean("student");
        System.out.println(student);
        System.out.println(student.getName());
    }

注入方式

<bean id="address" class="com.zhou.pojo.Address">
        <property name="address" value="桂林"/>
    </bean>

    <bean id="student" class="com.zhou.pojo.Student">
        <property name="name" value="狂神"/>

        <!--第二种注入,Bean注入,ref-->
        <property name="address" ref="address"/>

        <!--数组注入-->
        <property name="books">
            <array>
                <value>红楼梦</value>
                <value>西游记</value>
                <value>水浒传</value>
                <value>西游记</value>
            </array>
        </property>

        <!--List注入-->
        <property name="hobbys">
            <list>
                <value>听歌</value>
                <value>敲代码</value>
                <value>看电影</value>
            </list>
        </property>

        <!--Map是有键值对类型的,所以是有key和value-->
        <property name="card">
            <map>
                <entry key="身份证" value="11111"/>
                <entry key="银行卡" value="2141414"/>
            </map>
        </property>

        <property name="games">
            <set>
                <value>LOL</value>
                <value>KPL</value>
                <value>BOB</value>
            </set>
        </property>

        <!--配置的信息使用prop-->
        <property name="info">
            <props>
                <prop key="driver">20193124</prop>
                <prop key="url"></prop>
                <prop key="username">root</prop>
                <prop key="password">123456</prop>
            </props>
        </property>
        <!--Null注入-->
        <property name="wife"><null/></property>
    </bean>
Student{name='狂神', address=Address{address='桂林'}, books=[红楼梦, 西游记, 水浒传, 西游记], hobbys=[听歌, 敲代码, 看电影], card={身份证=11111, 银行卡=2141414}, games=[LOL, KPL, BOB], info={password=123456, url=, driver=20193124, username=root}, wife='null'}
p命名和c命名注入
public class User {
    private String name;
    private int age;

    public User() {
    }

    public User(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }

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

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return "User{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}    

导入约束: xmlns:p=“http://www.springframework.org/schema/p”
c 命名空间注入 : 需要在头文件中加入约束文件
xmlns:c=“http://www.springframework.org/schema/c”

<?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:c="http://www.springframework.org/schema/c"
       xmlns:p="http://www.springframework.org/schema/p"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd">

        <bean id="user" class="com.zhou.pojo.User" p:age="18" p:name="狂神">
            <!--使用p命名空间进行注入,先要在头文件中注入约束条件-->
        </bean>

        <bean id="user1" class="com.zhou.pojo.User" c:age="19" c:name="狂神说spring"/>
</beans>

测试

ApplicationContext context1 = new ClassPathXmlApplicationContext("userbeans.xml");
        User user = (User) context1.getBean("user");
        User user1 = (User) context1.getBean("user1");
        System.out.println(user);
        System.out.println(user1);
        System.out.println(user1 == user);

设置属性后的对象是不一样的

User{name='狂神', age=18}
User{name='狂神说spring', age=19}
false
Bean的作用域

在Spring中,那些组成应用程序的主体及由Spring IoC容器所管理的对象,被称之为bean。简单地讲,bean就是由IoC容器初始化、装配及管理的对象 .

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

在Spring中有三种装配的方式

    1. 在xml显示的配置
    1. 在java中显示配置
  • 隐式的自动装配bean(重要)

测试:一个人和两个宠物

package com.zhou.pojo;

public class Cat {
    /*给猫写个方法*/
    public void shout(){
        System.out.println("喵喵喵");
    }
}
package com.zhou.pojo;

public class Dog {
    public void shout(){
        System.out.println("汪汪汪");
    }
}
package com.zhou.pojo;

public class People {
    private Cat cat;
    private Dog dog;
    private String name;

    public People() {
    }

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

    @Override
    public String toString() {
        return "People{" +
                "cat=" + cat +
                ", dog=" + dog +
                ", name='" + 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;
    }
}

配置

<?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.zhou.pojo.Cat"/>
    <bean id="dog" class="com.zhou.pojo.Dog"/>

    <bean id="people" class="com.zhou.pojo.People">
        <property name="name" value="小狂神"/>
        <property name="cat" ref="cat"/>
        <property name="dog" ref="dog"/>
    </bean>

    <!--如果不使用ref进行关联的话,使用自动装配的话,autowire
    byName:会自动在容器上下文中查询,和自己对象set方法后面的值对应的bean的id名字
    byType:会自动在容器上下文中训中,和自己对象属性相同的bean。
    -->
    <bean id="people1" class="com.zhou.pojo.People" autowire="byName">
        <property name="name" value="小狂神呀"/>
    </bean>
<!--
    byType:使用这个的时候,id可以忽略
    -->
    <bean id="people2" class="com.zhou.pojo.People" autowire="byType">
        <property name="name" value="小狂神呀属性类型"/>
    </bean>
</beans>
 @Test
    public void test(){
        ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
        People people = context.getBean("people", People.class);
        People people2 = context.getBean("people2", People.class);
        people2.getDog().shout(); // 汪汪汪
    }
byName

autowire by Name(按名称自动装配)
由于在手动配置xml过程中,常常发生字母缺漏和大小写等错误,而无法对其进行检查,使得开发效率降低。
采用自动装配将避免这些错误,并且使配置简单化。

小结:
当一个bean节点带有autowire byName的属性时。

  1. 将查找其类中所有的set方法名,例如:setCat.获取set去掉并且首字母小写的字符串,即cat
  2. 去spring容器中寻找是否此字符串名称id的对象
  3. 如果有,就取出注入,如果没有,就报空指针异常
byType

autowire by Type(按类型自动装配)
使用autowire byType首先需要保证:同一类型的对象,在spring容器侯总唯一,如果不唯一,会报不唯一的异常。

NoUniqueBeanDefinitionException
<bean id="dog" class="com.kuang.pojo.Dog"/>
<bean id="cat" class="com.kuang.pojo.Cat"/>
<bean id="cat2" class="com.kuang.pojo.Cat"/>

<bean id="user" class="com.kuang.pojo.User" autowire="byType">
   <property name="str" value="qinjiang"/>
</bean>

如果按照上面来的话,则会报不唯一bean的异常,因为使用的是byType类型去匹配。有两个Cat类型,所以要删除其中一个。因为是按类型装配,所以并不会报异常,也不影响最后的结果。甚至将id属性去掉,也不影响结果。

总结:

  • byName的时候,需要保证所有的bean的id唯一,并且这个bean需要和自动注入的属性的set方法的值一致!
  • bytype的时候,需要保证所有的bean的class唯一。并且这个bean需要和自动注入的属性的类型一致。

使用注解

使用注解

@Autowired

  1. 在spring配置文件中引入context文件头
xmlns:context="http://www.springframework.org/schema/context"

http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
  1. 开启属性注解支持
<context:annotation-config/>
  1. 在类中使用@Autowired
 /*@Autowired
   自动匹配可以直接在属性上写,也可以放在set方式上使用。
   使用Autowired我们可以不用编写set方法,前提是你在这个自动装配属性在spring中存在,而且符合名字byname
    */
    @Autowired
    private Cat cat;
    @Autowired
    private Dog dog;
    private String name;
  1. xml配置
<?xml version="1.0" encoding="UTF-8"?>
<!--加一个context的命名空间,网址是第一个的spring的网址并改为context
然后把下面的那个也给复制一份,把beans改为context
-->
<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/>

    <!--开启注解之后,ioc容器就不用在这里面进行设置,只需要在java文件中设置注解即可-->
    <bean id="cat" class="com.zhou.pojo.Cat"/>
    <bean id="dog" class="com.zhou.pojo.Dog"/>
    <bean id="people" class="com.zhou.pojo.People"/>

</beans>
  1. 测试配置
 @Test
    public void test(){
        ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
        People people = context.getBean("people", People.class);
        people.getDog().shout(); //汪汪汪
        people.getCat().shout(); //喵喵喵
    }

科普:如果@Nullable 字段标记了这个注解,说明这个字段可以为null
@Autowired(required = false) 说明:false,对象可以为null。true,对象必须存对象,不能为null

// 当对象可以为空null,则设置required = false,默认是true。
@Autowired(required = false)
private Cat cat;

@Qualifier

  • @Autowired:是根据类型自动装配的,加上**@Qualifier如果名字相同的话可以根据byName的方式自动装配。如果名字不同的话,则通过类型去匹配**
  • @Qualifier不能单独使用 @Qualifier(value = “”)

如果类型匹配不上,名字也匹配不上,可以使用@Qualifier来匹配具体的名字
测试实验步骤:
(1) 配置文件修改内容,保证类型存在对象,且名字不为类的默认名字

    <!--开启注解之后,ioc容器就不用在这里面进行设置,只需要在java文件中设置注解即可
    把cat改为cat11,dog22也可以,会通过名字或者类型自动装配。
    -->
    <bean id="cat11" class="com.zhou.pojo.Cat"/>
    <bean id="cat111" class="com.zhou.pojo.Cat"/>
    <bean id="dog22" class="com.zhou.pojo.Dog"/>
    <bean id="dog222" class="com.zhou.pojo.Dog"/>
    <bean id="people" class="com.zhou.pojo.People"/>
  @Autowired
    @Qualifier(value = "cat11")
    private Cat cat;
    @Autowired
    @Qualifier(value = "dog22")
    private Dog dog;
    private String name;

@Resource

  • @Resource如有指定的name属性,先按照该属性进行byName方式查找专配
  • 其次再进行默认的byName的方式进行装配;
  • 如果以上都不成功,则按照byType的方式自动装配。
  • 都不成功,则报异常
    /*使用resource注解,有name的话是直接指定具体的名字对象*/
    @Resource(name = "cat111")
    private Cat cat;
   @Resource
    private Dog dog;
    private String name;
<!--NoUniqueBeanDefinitionException,如果默认的resource不加name的话,必须要是唯一的类型-->
    <bean id="cat11" class="com.zhou.pojo.Cat"/>
    <bean id="cat111" class="com.zhou.pojo.Cat"/>
    <bean id="dog22" class="com.zhou.pojo.Dog"/>
<!--    <bean id="dog222" class="com.zhou.pojo.Dog"/>-->
    <bean id="people" class="com.zhou.pojo.People"/>

结论:
先进行byName查找,如果失败,再进行byType查询,直到成功

@Autowired与Resource异同
  1. @Autowired与@Resource都可以用来装配bean。都可以写在字段上,或写在setter方法上。
  2. @Autowired默认按类型装配(属于spring规范),默认情况下必须要求依赖对象必须存在,如果要允许null 值,可以设置它的required属性为false,如:@Autowired(required=false) ,如果我们想使用名称装配可以结合@Qualifier注解进行使用
  3. @Resource,默认按照名称进行装配,名称可以通过name属性进行指定。如果没有指定name属性,当注解写在字段上时,默认取字段名进行按照名称查找,如果注解写在setter方法上默认取属性名进行装配。当找不到与名称匹配的bean时才按照类型进行装配。但是需要注意的是,如果name属性一旦指定,就只会按照名称进行装配。
    他们都是使用注解方式注入对象,但是顺序不同
    @Autowired先byType,@Resource先byName。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值