码迷,mamicode.com
首页 > 编程语言 > 详细

Spring 自动装配

时间:2021-03-29 12:52:06      阅读:0      评论:0      收藏:0      [点我收藏+]

标签:demo   代码   private   mic   方便   就会   auto   sch   imp   

很明显之前学的方式 手动配置还是不够方便。就需要我们去学习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 自动装配

标签:demo   代码   private   mic   方便   就会   auto   sch   imp   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!