spring里面的 @Import @Configuration和@Bean的用法和理解以及区别

 

1.@Import

  • @Import注解在4.2以前只支持导入配置类
  • 在4.2,@Import注解支持导入普通的java类,并将其声明成一个bean

演示java类java

public class DemoService {
    public void doSomething(){
        System.out.println("everything is all fine");
    }

}

演示配置spring

import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
@Configuration
@Import(DemoService.class)//在spring 4.2以前是不不支持的
public class DemoConfig {

}

运行spa

import org.springframework.context.annotation.AnnotationConfigApplicationContext;

public class Main {
    public static void main(String[] args) {
        AnnotationConfigApplicationContext context =
                new AnnotationConfigApplicationContext("com.wisely.spring4_2.imp");
        DemoService ds = context.getBean(DemoService.class);
        ds.doSomething();

    }

}

输出结果.net

everything is all fine

 

2. @Bean:

①注解@Bean的属性initMethod, destroyMethod code

②接口InitializingBean, DisposableBeanxml

③注解@PostConstruct,@PreDestroy
都做用于一样的两个过程——初始化阶段和销毁阶段
对象

1.1 定义接口

从定义能够看出,@Bean只能用于注解方法和注解的定义。ci

@Target({ElementType.METHOD, ElementType.ANNOTATION_TYPE})
@Retention(RetentionPolicy.RUNTIME)

1.2 spring文档中对 @Bean的说明element

The @Bean annotation is used to indicate that a method instantiates, configures and initializes a new object to be managed by the Spring IoC container.

For those familiar with Spring’s <beans/> XML configuration the @Bean annotation plays the same role as the <bean/>element. 

用@Bean注解的方法:会实例化、配置并初始化一个新的对象,这个对象会由spring IoC 容器管理。

实例:

@Configuration
public class AppConfig {

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

}

至关于在 XML 文件中配置

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

1.3 生成对象的名字:默认状况下用@Bean注解的方法名做为对象的名字。可是能够用 name属性定义对象的名字,并且还能够使用name为对象起多个名字。

@Configuration
public class AppConfig {

    @Bean(name = "myFoo")
    public Foo foo() {
        return new Foo();
    }

}

 

@Configuration
public class AppConfig {

    @Bean(name = { "dataSource", "subsystemA-dataSource", "subsystemB-dataSource" })
    public DataSource dataSource() {
        // instantiate, configure and return DataSource bean...
    }

}

3.区别和联系

@Bean 通常和 @Component或者@Configuration 一块儿使用。

@Component和@Configuration不一样之处

(1)This method of declaring inter-bean dependencies only works when the @Bean method is declared within a@Configuration class. You cannot declare inter-bean dependencies using plain @Component classes.

 

在 @Component 注解的类中不能定义 类内依赖的@Bean注解的方法。@Configuration能够。

@Configuration可理解为用spring的时候xml里面的<beans>标签

@Bean可理解为用spring的时候xml里面的<bean>标签

相关文章
相关标签/搜索