Spring IOC--注入与装配
发布日期:2021-05-10 13:43:51 浏览次数:17 分类:精选文章

本文共 5166 字,大约阅读时间需要 17 分钟。

���������������������

  • @Autowired������ @Value������ Field������ ��������������� Setter������

  • ������������ ������������������������������

  • ������������������ @SpringBootApplication+@Component @ComponentScan+@Configuration+@Component @Configuration+@Bean @Import������ FactoryBean @DependsOn BeanPostProcessor


  • 1. @Autowired������

    @Autowired������������������������������������������������������������������������������ setter ������������������������������������������������������������������bean���������������

    ������

    • ������������������������������������
    • ���������������������������������������������������������������������������������

    ������

    • ������������������������������������������������������������������������
    • ������������������������������������������������

    ������

    // Filed������������
    public class FooController {
    @Autowired
    private FooService fooService;
    }
    // Constructor������������
    public class FooController {
    private final FooService fooService;
    @Autowired
    public FooController(FooService fooService) {
    this.fooService = fooService;
    }
    }

    2. @Value������

    @Value������������������������������������������������������������������������������������������������

    ������

    • ���������
      String name;
      public class Foo {
      @Autowired
      public Foo(@Value("${my.name}") String myName) {
      this.name = myName;
      }
      }

    3. Field������

    Field������������������������������������������������������������������������������������������������������������������������������

    ������

    • ��������������������������������������������������� setter ������

    ������

    • ���������������������������������������������������������
    • ���������������������������������������������������������

    4. Constructor������

    Constructor���������������������������������������������������������������������������������������������������������

    ������

    • ���������������������������������������������
    • ��� troublesomenop_penaltyException.product know_ensure������������������������������
    • ���������������������������������������������������������������

    ������

    public class FooController {
    private final FooService fooService;
    @Autowired
    public FooController(FooService fooService) {
    this.fooService = fooService;
    }
    }

    5. Setter������

    Setter������������ setter ������������������������������������������������������������������������������������������������������������������

    ������

    • ������������������������������������
    • ���������������������������������������������������

    ������

    • ������������������������������������������������������
    • ������������������������������������������������������������

    3. ������������

    Bean���������������������������������loading������������������������������������������������������������@Autowired���������

    ���������������������������

  • ������������������
  • @DependsOn������
  • BeanPostProcessor������

  • 4. ������������������

    1. @SpringBootApplication+@Component

    ������������������������������������������������������������

    @SpringBootApplication(scanBasePackages = "com.example.demo")
    public class SpringApplication {
    public static void main(String[] args) {
    SpringApplication.run(SpringApplication.class, args);
    }
    }

    2. @ComponentScan+@Configuration+@Component

    ������������������������������������������������

    @Configuration
    @ComponentScan(basePackages = { "com.example.demo.mybeans" })
    public class DemoConfig {
    }

    3. @Configuration+@Bean

    ������������@Component������������������������������������������������

    @Configuration
    public class ImportConfig {
    @Bean
    public User user() {
    return new User("Lily");
    }
    }

    4. @Import������

    ������������������������FactoryBean���

    @Configuration
    @Import({ImportDemoConfig.class, MyImportSelector.class})
    public class ImportConfig {
    }

    5. FactoryBean

    ������FactoryBean������������������������������������������������������������������&���������������������

    public class UserFactoryBean implements FactoryBean
    {
    public User getObject() {
    return new User("User04");
    }
    }
    @Configuration
    @Bean
    public UserFactoryBean userFactoryBean() {
    return new UserFactoryBean();
    }

    5. @DependsOn������

    ������Bean���������������������������������������������������������

    ������

    @DependsOn("rightDemo2")
    public class RightDemo1 {
    private String name = "right demo 1";
    public RightDemo1() {
    System.out.println(name);
    }
    }

    6. BeanPostProcessor������

    ���������������BeanPostProcessor������������������������������������������

    @Component
    public class DemoBeanPostProcessor extends InstantiationAwareBeanPostProcessorAdapter implements BeanFactoryAware {
    private ConfigurableListableBeanFactory beanFactory;
    @Override
    public void setBeanFactory(BeanFactory beanFactory) {
    if (!(beanFactory instanceof ConfigurableListableBeanFactory)) {
    throw new IllegalArgumentException("...");
    }
    this.beanFactory = (ConfigurableListableBeanFactory) beanFactory;
    }
    @Override
    public boolean postProcessBeforeInstantiation(Class
    beanClass, String beanName) {
    if ("HDemo1".equals(beanName)) {
    HDemo2 demo2 = beanFactory.getBean(HDemo2.class);
    }
    return null;
    }
    }
    上一篇:Java常用包系列--JWT
    下一篇:Spring AOP系列--AOP+注解 实例

    发表评论

    最新留言

    路过,博主的博客真漂亮。。
    [***.116.15.85]2025年04月12日 02时43分00秒