signed

QiShunwang

“诚信为本、客户至上”

依赖查找、依赖注入

2021/3/21 0:00:16   来源:

依赖查找

根据Bean名称查找

public class User {
  private Integer id;
  private String name;
}

实时查找

@Configuration
public class UserConfig {
  @Bean
  public User user() {
    User user = new User(1, "wangrui");
    return user;
  }
}
AnnotationConfigApplicationContext applicationContext
  = new AnnotationConfigApplicationContext(UserConfig.class);
User user = (User) applicationContext.getBean("user");
System.out.println(user);

延迟查找

@Configuration
public class UserConfig {
  @Bean
  public User user() {
    User user = new User(1, "wangrui");
    return user;
  }

  @Bean
  public ObjectFactoryCreatingFactoryBean objectFactory() {
    ObjectFactoryCreatingFactoryBean factoryBean = new ObjectFactoryCreatingFactoryBean();
    factoryBean.setTargetBeanName("user");
    return factoryBean;
  }
}
AnnotationConfigApplicationContext applicationContext
  = new AnnotationConfigApplicationContext(UserConfig.class);
ObjectFactory<User> objectFactory = (ObjectFactory<User>) applicationContext.getBean("objectFactory");
User user = objectFactory.getObject();
System.out.println("延迟查找:" + user);

根据Bean类型查找

单个Bean对象

AnnotationConfigApplicationContext applicationContext
  = new AnnotationConfigApplicationContext(UserConfig.class);
User user = applicationContext.getBean(User.class);
System.out.println(user);

集合Bean对象

AnnotationConfigApplicationContext applicationContext
  = new AnnotationConfigApplicationContext(UserConfig.class);
Map<String, User> userMap = applicationContext.getBeansOfType(User.class);
System.out.println(userMap);  // {user=User(id=1, name=wangrui)}

根据Bean名称+类型查找

AnnotationConfigApplicationContext applicationContext
  = new AnnotationConfigApplicationContext(UserConfig.class);
User user = applicationContext.getBean("user", User.class);

根据Java注解查找

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

@Super
public class SuperUser extends User {
  private String address;
}
@Configuration
public class UserConfig {
  @Bean
  public User user() {
    User user = new User(1, "wangrui");
    return user;
  }

  @Bean
  public SuperUser superUser(User user) {
    SuperUser superUser = new SuperUser();
    superUser.setAddress("广州市");
    superUser.setName(user.getName());
    superUser.setId(user.getId());
    return superUser;
  }
}
AnnotationConfigApplicationContext applicationContext
  = new AnnotationConfigApplicationContext(UserConfig.class);

String[] names = applicationContext
  .getBeanNamesForAnnotation(Super.class);

Map<String, Object> map 
  = applicationContext.getBeansWithAnnotation(Super.class);

// 获取此类上的注解
Super user = applicationContext.findAnnotationOnBean("superUser", Super.class);

依赖注入

根据Bean类型注入

Bean类型注入分为:单个Bean、集合Bean对象、注入内建Bean对象

Bean类型注入时机:实时注入、延迟注入

@Service
@Getter
public class UserService {
  // 按照beanName注入
  @Autowired
  private User user;
  // 集合注入
  @Autowired
  private List<User> users;
  // 注入内建 Bean对象
  @Autowired
  private BeanFactory beanFactory;
  // 延迟注入  此种方式可以解决Bean注入的先后顺序问题
  @Autowired
  private ObjectFactory<ApplicationContext> objectFactory;
}
AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext();
applicationContext.register(UserConfig.class, UserService.class);
applicationContext.refresh();

UserService userService = applicationContext.getBean(UserService.class);
System.out.println(userService);

System.out.println(applicationContext == userService.getObjectFactory().getObject());