spring java config ,xml config 和 annotation config

使用AnnotationConfigApplicationContext实例化Spring容器

使用@Configuration注解 的类 相当于 applicationContext.xml

使用@Bean注解 的类 相当于 <bean> xxx </bean>

1、创建Bean

以下三种等价

@Configuration
public class AppConfig {

    @Bean
    public MyService myService() {
        return new MyServiceImpl();
    }
}


<beans>
    <bean id="myService" class="com.acme.services.MyServiceImpl"/>
</beans>


@Service
public class MyServiceImpl() {

}

实例化 spring容器 ApplicationContext

public static void main(String[] args) {
    ApplicationContext ctx = new AnnotationConfigApplicationContext(AppConfig.class);
    MyService myService = ctx.getBean(MyService.class);
    myService.doStuff();
}

 2、启动包扫描

 以下两种等价

@Configuration
@ComponentScan(basePackages = "com.acme")
public class AppConfig  {
    ...
}


<beans>
    <context:component-scan base-package="com.acme"/>
</beans>

在上面的例子中,com.acme将会被扫描,它会寻找任何@Component注解的类,这些类将会在Spring的容器中被注册成为一个bean。AnnotationConfigApplicationContext暴露的scan(String…​)方法以达到相同组件扫描的功能:

public static void main(String[] args) {
    AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
    ctx.scan("com.acme");
    ctx.refresh();
    MyService myService = ctx.getBean(MyService.class);
}

使用@Configuration注解的类是使用@Component进行元注解,所以它们也是组件扫描的候选,假设AppConfig被定义在com.acme这个包下(或者它下面的任何包),它们都会在调用scan()方法期间被找出来,然后在refresh()方法中它们所有的@Bean方法都会被处理,在容器中注册成为bean

  3、带参数的@Bean 方法

以下两种等价

这种解决原理和基于构造函数的依赖注入几乎相同 

@Configuration
public class AppConfig {

    @Bean
    public TransferService transferService(AccountRepository accountRepository) {
        return new TransferServiceImpl(accountRepository);
    }
}

4、bean依赖注入

以下两种等价

这种方法声明的bean的依赖关系只有在@Configuration类的@Bean方法中有效。你不能在@Component类中来声明bean的依赖关系。

@Configuration
public class AppConfig {

    @Bean
    public Foo foo() {
        return new Foo(bar());
    }

    @Bean
    public Bar bar() {
        return new Bar();
    }

}



<beans>
    <bean id="foo" class="com.acme.services.Foo">
        <property name="bar" ref="bar"/>
    </bean>

    <bean id="bar" class="com.acme.services.Bar"/>
</beans>

5、其它注入方式

@Configuration
public class Config {

    @Autowired
    public OtherBean otherBean;  //在其它地方声明的bean

    @Bean
    public SomeBean someBean() {
        SomeBean someBean = new SomeBean(otherBean);
   
        return someBean;
    }

}
@Configuration
public class Config {

    @Bean
    public SomeBean someBean() {
        SomeBean someBean = new SomeBean();

        someBean.setOtherBean(otherBean());  // 需要在 SomeBean 中写 setOtherBean方法

        return someBean;
    }


    @Bean
    public OtherBean otherBean () {
        return new OtherBean();
    }

}

6、@import

引入其它地方的配置类


@Configuration
public class ConfigA {

    @Bean
    public A a() {
        return new A();
    }

}

@Configuration
@Import(ConfigA.class)
public class ConfigB {

    @Bean
    public B b() {
        return new B();
    }

}

猜你喜欢

转载自blog.csdn.net/q503385724/article/details/88635622