首页 > 编程语言 > 详细

Spring 自动装配

时间:2021-03-28 12:37:01      阅读:25      评论:0      收藏:0      [点我收藏+]

很明显之前学的方式 手动配置还是不够方便。就需要我们去学习Spring的自动装配!

autowire="byName"

定义几个简单类

package Demo;

public class Cat {
    public void shout(){
        System.out.println("miao~");
    }
}

package Demo;

public class Dog {
    public void shout(){
        System.out.println("wang~");
    }
}

package Demo;

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 + ‘\‘‘ +
                ‘}‘;
    }
}

配置文件如下:

<?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
        https://www.springframework.org/schema/beans/spring-beans.xsd">
    <bean id="cat" class="Demo.Cat"/>
    <bean id="dog" class="Demo.Dog"/>
    <!--autowire="byName" 就会将该类Set方法Set后面的值和设置的ID一致的自动装配-->
    <bean id="people" class="Demo.People" autowire="byName">
        <property name="name" value="jie"/>
    </bean>
</beans>

autowire="byType"

<?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
        https://www.springframework.org/schema/beans/spring-beans.xsd">
    <bean id="cat" class="Demo.Cat"/>
    <bean id="dog" class="Demo.Dog"/>
    <!--autowire="byType" 根据类型自动装配-->
    <bean id="people" class="Demo.People" autowire="byType">
        <property name="name" value="jie"/>
    </bean>
</beans>

小结

技术分享图片

注解配置(主流方式)

使用前提
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
        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>

@Autowired

导入上面配置后
上面案例代码可改为

package Demo;

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

public class People {
  //在此处使用Autowired
    @Autowired
  //先根据类型匹配,然后根据名字匹配
    private Cat cat;
    @Autowired
    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 + ‘\‘‘ +
                ‘}‘;
    }
}

<?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">
    <bean id="cat" class="Demo.Cat"/>
    <bean id="dog" class="Demo.Dog"/>
    <bean id="people" class="Demo.People"/>
    <context:annotation-config/>
</beans>

如果配置文件定义类重复想要指定可用下面方法
技术分享图片

小结

除了Autowire java也自带一个Resource注解,功能几乎差不多。
技术分享图片

Spring 自动装配

原文:https://www.cnblogs.com/OfflineBoy/p/14587887.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!