1、手动

//生成普通的Bean
Aop.wrapAndPut(UserService.class, new UserServiceImpl());

//生成带注解的Bean(比如:@Controller)
Aop.context().beanMake(UserServiceImpl.class);

//获取Bean(如果不确定是否存在,可改用异步订阅获取)
Aop.get(UserService.class);

下面2种模式,必须要被扫描到。在不便扫描,或不须扫描时手动会带来一种自由感。

2、用配置器类

本质是 @Configuration + @Bean 的组合,并且 Config 要被扫描到

@Configuration
public class Config{
    @Bean
    public UserService build(){
        return new UserServiceImpl();
    }
    
    //顺带一提:也可空返回,做一些初始化动作
    @Bean
    public void initTitle(@Inject("${ser.title}") String title){
        Config.TITLE = title;
    }
}

3、使用组件注解(必须要能被扫描到)

@Component
public class UserServiceImpl implements UserService{

}

相关文章:

  • 2021-11-25
  • 2021-12-31
  • 2022-01-18
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-07-08
  • 2021-12-21
猜你喜欢
  • 2021-07-21
  • 2021-09-04
  • 2022-03-02
  • 2021-06-07
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案