首页 > 编程语言 > 浅析Spring IOC 依赖查找你需要知道的几种方式
2022
04-20

浅析Spring IOC 依赖查找你需要知道的几种方式

首先,我们创建一个包含 spring-context 依赖的 maven 项目,然后定义一个 User.class

public class User {

    private long id;
    private String name;

    public long getId() {
        return id;
    }
    public void setId(long 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 + '\'' +
                '}';
    }
 }

在 resources 目录下,创建 dependency-look-up.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
        https://www.springframework.org/schema/beans/spring-beans.xsd">


    <bean id = "user" class="org.example.overview.dependency.domain.User">
        <property name="id" value="1"/>
        <property name="name" value="彭于晏"/>
    </bean>

    <bean id="superUser" class="org.example.overview.dependency.domain.SuperUser" parent="user"
          primary="true">
        <property name="address" value="杭州"/>
    </bean>

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

</beans>

1. 根据 Bean 名称查找

实时查找

实时查找的意思就是说直接获取 beanFactory, 通过 beanFactory 获取 user 对应的 bean,代码如下所示

 BeanFactory beanFactory = new ClassPathXmlApplicationContext("classpath:/META-INF/dependency-look-up.xml");
// 这里的 “user” 就是 xml 文件中的 id,即名称实时查找                
User user = (User) beanFactory.getBean("user");
System.out.println("实时加载: " + bean);

延时查找

这里的延迟就是说,通过其他的对象来获取 user 对应的 Bean,代码如下所示:

ObjectFactory<User> objectFactory = (ObjectFactory<User>) beanFactory.getBean("objectFactory");
User user = objectFactory.getObject();
System.out.println("延迟加载" + user);

2. 根据 Bean 类型查找

这里的类型指的就是 user.class

单个 Bean 对象

User bean = beanFactory.getBean(User.class);

集合 Bean 对象

if(beanFactory  instanceof ListableBeanFactory){
      ListableBeanFactory listableBeanFactory = (ListableBeanFactory)beanFactory;
      Map<String, User> beansOfType = listableBeanFactory.getBeansOfType(User.class);
      System.out.println("集合类型:" + beansOfType);
 }

3. 根据 Bean 类型 + 名称查找

4. 根据 Java 注解查找

首先我们自定义一个注解 @Super

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
public @interface Super {
}

将注解表示到 superUser 类中,这里的 superUser 继承了 user 类

@Super
public class SuperUser extends User {

    private String address;

    public String getAddress() {
        return address;
    }

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

    @Override
    public String toString() {
        return "SuperUser{" +
                "address='" + address + '\'' +
                "} " + super.toString();
    }
}

最后根据 spring 提供的 api,获取注解 bean

 if(beanFactory  instanceof ListableBeanFactory){
       ListableBeanFactory listableBeanFactory = (ListableBeanFactory)beanFactory;
       Map<String, Object> beansOfType = listableBeanFactory.getBeansWithAnnotation(Super.class);
       System.out.println("查找 @super 的:" + beansOfType);
 }

到此这篇关于浅析Spring IOC 依赖查找你需要知道的几种方式的文章就介绍到这了,更多相关Spring IOC 依赖查找内容请搜索自学编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持自学编程网!

编程技巧