Spring bean 依赖查找

2021-01-04

Spring bean 的依赖查找,可以

  • 根据名称查找

  • 根据类型查找单个和所有 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="user1" class="constxiong.User"/>

    <bean id="user2" class="constxiong.User" primary="true">
        <property name="id" value="2" />
        <property name="name" value="user2" />
    </bean>

    <bean id="lazyUser" class="org.springframework.beans.factory.config.ObjectFactoryCreatingFactoryBean">
        <property name="targetBeanName" value="user1"/>
    </bean>

</beans>

 

 

自定义注解,为演示可以通过注解查找到 bean 而创建

package constxiong;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

/**
 * 注解
 */
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
public @interface ConstXiong {
}

 

bean 类

package constxiong;

@ConstXiong
public class User {

    private Integer id = 1;

    private String name = "Constxiong";

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

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

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

 

测试代码

package constxiong;

import java.util.Map;

import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.ListableBeanFactory;
import org.springframework.beans.factory.ObjectFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.ClassPathResource;

/**
 * 依赖查找
 */
@SuppressWarnings("deprecation")
public class Test {

    public static void main(String[] args) {
        BeanFactory factory = new XmlBeanFactory(new ClassPathResource("META-INF/spring-dependency-lookup.xml"));
        findByName(factory);
        findPrimaryByType(factory);
        findAllByType(factory);
        findByNameAndType(factory);
        findByAnnotation(factory);
        findInLazy(factory);
    }

    /**
     * 根据名称查找
     */
    public static void findByName(BeanFactory factory) {
        User user = (User)factory.getBean("user1");
        System.out.println("根据名称查找: " + user);
    }

    /**
     * 根据类型查找 primary
     */
    private static void findPrimaryByType(BeanFactory factory) {
        //根据类型查找如何存在多个同一类型的bean,需要制定 primary 属性为 true
        User user = factory.getBean(User.class);
        System.out.println("根据类型查找 priary: " + user);
    }

    /**
     * 根据类型查找所有
     */
    private static void findAllByType(BeanFactory factory) {
        //ListableBeanFactory,具有查询所有类型的能力
        Map<String, User> beans = ((ListableBeanFactory) factory).getBeansOfType(User.class);
        System.out.println("根据类型查找所有: " + beans);
    }

    /**
     * 根据 名称 + 类型 查找
     */
    private static void findByNameAndType(BeanFactory factory) {
        User user = factory.getBean("user1", User.class);
        System.out.println("根据 名称 + 类型 查找: " + user);
    }

    /**
     * 根据注解查找
     */
    private static void findByAnnotation(BeanFactory factory) {
        //ListableBeanFactory,具有根据注解查找 bean 的能力
        Map<String, Object> beans = ((ListableBeanFactory) factory).getBeansWithAnnotation(ConstXiong.class);
        System.out.println("根据注解查找: " + beans);
    }

    /**
     * 延迟查找
     */
    private static void findInLazy(BeanFactory factory) {
        ObjectFactory<User> objectFactory = (ObjectFactory<User>)factory.getBean("lazyUser");
        User user = objectFactory.getObject();
        System.out.println("延迟查找: " + user);
    }

}

 

打印结果

根据名称查找: User{id=1, name='Constxiong'}
根据类型查找 priary: User{id=2, name='user2'}
根据类型查找所有: {user1=User{id=1, name='Constxiong'}, user2=User{id=2, name='user2'}}
根据 名称 + 类型 查找: User{id=1, name='Constxiong'}
根据注解查找: {user1=User{id=1, name='Constxiong'}, user2=User{id=2, name='user2'}}
延迟查找: User{id=1, name='Constxiong'}

 

ps:

ConstXiong 备案号:苏ICP备16009629号-3