java

关注公众号 jb51net

关闭
首页 > 软件编程 > java > Java Spring之自动装配

一篇文章教带你了解Java Spring之自动装配

作者:可爱多一点@

今天小编就为大家分享一篇关于Spring中的自动装配,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看吧

在Spring中有三种装配的方式:

1.Bean的自动装配

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

1.1 autowire="byName" 实现自动装配

byname会自动在容器上下文中查找,和自己对象set方法后面的值对应的bean id。

需要保证所有bean的id唯一,并且这个bean需要和自动注入的属性的set方法的值一致。

People.java

package org.example;
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 + '\'' +
                '}';
    }
}

cat.java

package org.example;
public class Cat {
    public void shut(){
        System.out.println("喵喵喵……");
    }
}

Dog.java

package org.example;
public class Dog {
    public void shut(){
        System.out.println("汪汪汪……");
    }
}

applicationContext.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"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
    <bean id="cat" class="org.example.Cat"></bean>
    <bean id="dog" class="org.example.Dog"></bean>
    <bean id="people" class="org.example.People" autowire="byName">
        <property name="name" value="小狂神"></property>
    </bean>
</beans>

测试类

package org.example;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class App {
    public static void main( String[] args ) {
        //获取ApplicationContext对象
        ApplicationContext application=new ClassPathXmlApplicationContext("ApplicationContext.xml");
        //通过ApplicationContext获得TestHello对象
        //getBean()方法中的参数即为配置文件中Bean的id的值
        People people=(People) application.getBean("people");
        people.getCat().shut();
        people.getDog().shut();
    }
}

1.2 autowire="byType" 实现自动装配

byType:会自动在容器上下文中查找,和自己对象属性类型相同的bean。

需要保证所有bean的class唯一,并且这个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="org.example.Cat"></bean>
    <bean id="dog" class="org.example.Dog"></bean>
    <bean id="people" class="org.example.People" autowire="byType">
        <property name="name" value="小狂神"></property>
    </bean>
</beans>

2.注解实现自动装配

JDK1.5支持的注解,Spring2.5就支持注解了。

2.1 配置注解

只需在applicationContext.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:util="http://www.springframework.org/schema/util"
       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/util https://www.springframework.org/schema/util/spring-util.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">
<!--开启注解的支持-->
    <context:component-scan base-package="org.example"/>
</beans>

2.2 @Autowired注解

直接在属性上使用即可,也可以在set方式上使用。使用@Autowired 可以不用编写set方法了,前提是你这个自动装配的属性在IOC(Spring)容器中存在,且符合byname。

People.java

package org.example;
import org.springframework.beans.factory.annotation.Autowired;
public class People {
    @Autowired
    private Cat cat;
    @Autowired
    private Dog dog;
    private String name;
    public Cat getCat() {
        return cat;
    }
//set方法可以省略
    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 + '\'' +
                '}';
    }
}

applicationContext.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 http://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="org.example.Cat"></bean>
    <bean id="dog" class="org.example.Dog"></bean>
    <bean id="people" class="org.example.People" ></bean>
    <context:component-scan base-package="org.example"/>
</beans>

2.3 @Resource注解

People.java

package org.example;
import org.springframework.beans.factory.annotation.Autowired;
import javax.annotation.Resource;
public class People {
//如果没有(name="cat")那么就会找不到
    @Resource(name = "cat2")
    private Cat cat;
    @Resource
    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 + '\'' +
                '}';
    }
}

applicationContext.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 http://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="cat1" class="org.example.Cat"></bean>
    <bean id="cat2" class="org.example.Cat"></bean>
    <bean id="dog" class="org.example.Dog"></bean>
    <bean id="people" class="org.example.People" ></bean>
    <context:component-scan base-package="org.example"/>
</beans>

2.4小结

@Autowired和@Resource的区别:

3.介绍一个idea中做笔记的小技巧

总结

本篇文章就到这里了,希望能够给你带来帮助,也希望您能够多多关注脚本之家的更多内容!

您可能感兴趣的文章:
阅读全文