Spring Beans自动装配

byName

这种模式由属性名称指定自动装配。Spring 容器看作 beans,在 XML 配置文件中 beans 的 auto-wire 属性设置为 byName。然后,它尝试将它的属性与配置文件中定义为相同名称的 beans 进行匹配和连接。如果找到匹配项,它将注入这些 beans,否则,它将抛出异常。

<bean id="textEditor" class="com.tutorialspoint.TextEditor" 
      autowire="byName">
      <property name="name" value="Generic Text Editor" />
   </bean>

byType

这种模式由属性类型指定自动装配。Spring 容器看作 beans,在 XML 配置文件中 beans 的 autowire 属性设置为 byType。然后,如果它的 type 恰好与配置文件中 beans 名称中的一个相匹配,它将尝试匹配和连接它的属性。如果找到匹配项,它将注入这些 beans,否则,它将抛出异常。

<bean id="textEditor" class="com.tutorialspoint.TextEditor">
      <property name="spellChecker" ref="spellChecker" />
      <property name="name" value="Generic Text Editor" />
   </bean>

构造函数自动装配

Spring 容器看作 beans,在 XML 配置文件中 beans 的 autowire 属性设置为 constructor。然后,它尝试把它的构造函数的参数与配置文件中 beans 名称中的一个进行匹配和连线。如果找到匹配项,它会注入这些 bean,否则,它会抛出异常。

<bean id="textEditor" class="com.tutorialspoint.TextEditor" 
      autowire="constructor">
      <constructor-arg value="Generic Text Editor"/>
   </bean>

基于注解配置

使用注解之前需要在spring配置文件中启用。

<context:annotation-config/>

1.如果想使用@ Resource 、@ PostConstruct、@ PreDestroy等注解就必须声明CommonAnnotationBeanPostProcessor。
2.如果想使用@PersistenceContext注解,就必须声明PersistenceAnnotationBeanPostProcessor的Bean。
3.如果想使用@Autowired注解,那么就必须事先在 Spring 容器中声明 AutowiredAnnotationBeanPostProcessor Bean。
4.如果想使用 @Required的注解,就必须声明RequiredAnnotationBeanPostProcessor的Bean。
而使用context:annotation-config/ 就可以隐式地自动向Spring容器注册以上4个BeanPostProcessor.
context:component-scan除了具有context:annotation-config的功能之外,context:component-scan还可以在指定的package下扫描以及注册javabean 。还具有自动将带有@component,@service,@Repository等注解的对象注册到spring容器中的功能。

默认情况下,使用注解类被自动发现并注册bean。可以使用过滤器修改,如下:

<context:component-scan base-package="com.myl">
		<context:include-filter type="annotation"
			expression="org.springframework.stereotype.Service" />
		<context:exclude-filter type="annotation"
			expression="org.springframework.stereotype.Repository" />
	</context:component-scan>

常用注解

  • @Component:通用注解,可用于任何bean
  • @Repository:注解DAO类
  • @Service:注解Service类
  • @Controller:注解Controller类
  • @Scope(“prototype”):作用域
    singleton
    prototype
    request
    session
    application
    websocket

@Scope(value="",proxyMode = 指定代理方式)

  • @Required:setter方法上,必须在配置时被填充
  • @Autowired:
    • setter方法上:setter方法上可以使用@Autowired(required=false),表示不是必须在配置时被填充。每个类只有一个构造器被标记为required=true。
    • 成员变量上
    • List Map
@Autowired
private List<BeanInterface> list;//bean name

@Autowired
private Map<String,BeanInterface> map;//bean name,bean实例
  • @Order(1):只对数组有效,使数组有序
  • @Qualifier:
    • 按类型注册多个bean实例,可以使用@Qualifier注解缩小范围或指定唯一(结合@Autowired使用)
	@Autowired
    @Qualifier("ttsTicketService")
    private TicketService ticketService;
- 可以用于指定单独的构造器参数或者方法参数
public void prepare(@Qualifier("ttsTicketService"TicketService ticketService)){}
  • @Resource:通过其独特的名称定义来识别特定的目标
  • @Bean:告诉方法产生一个bean对象,并交给spring管理。产生这个bean对象的方法spring只会调用一次,产生后将bean对象放在IoC容器中。
  • @Configuration:SpringIOC 容器管理一个或者多个bean,这些bean都需要在@Configuration注解下进行创建,在一个方法上使用@Bean注解就表明这个方法需要交给Spring进行管理。
@Configuration
public class AppConfig {

  	// 使用@Bean 注解表明myBean需要交给Spring进行管理
  	// 未指定bean 的名称,默认采用的是 "方法名" + "首字母小写"的配置方式
    @Bean
    public MyBean myBean(){
        return new MyBean();
    }
}


public class MyBean {

    public MyBean(){
        System.out.println("MyBean Initializing");
    }

    public void init(){
        System.out.println("Bean 初始化方法被调用");
    }

    public void destroy(){
        System.out.println("Bean 销毁方法被调用");
    }
}

@Configuration
public class AppConfig {

//    @Bean
    @Bean(initMethod = "init", destroyMethod = "destroy")
    public MyBean myBean(){
        return new MyBean();
    }

}
  • @Profile:把一些meta-data进行分类,分成Active和InActive这两种状态,然后你可以选择在active 和在Inactive这两种状态下配置bean
  • @Lazy:延迟加载
  • @DependsOn
@Configuration
public class AppConfigWithDependsOn {

    @Bean("firstBean")
    @DependsOn(value = {
            "secondBean",
            "thirdBean"
    })
    public FirstBean firstBean() {
        return new FirstBean();
    }

    @Bean("secondBean")
    public SecondBean secondBean() {
        return new SecondBean();
    }

    @Bean("thirdBean")
    public ThirdBean thirdBean() {
        return new ThirdBean();
    }
}

firstBean创建过程首先需要依赖sencodBean和thirdBean的创建,所以首先加载sencondBean、thirdBean最后是fristBean

  • @Primary:多个时优先加载
  • @ImportResource :
  • @Value:
@Component
@ImportResource("classpath:config.xml")
public class storeConfig{
	@Value
	private String url;
	
	@Value
	private String name;

	@Value
	private String password;
}
  • 基于范型
public interface Store<T>{}
public class IntegerStore implements Store<Integer>{}
public class StringStore implements Store<String>{}
public class StoreConfig{
	@Autowired
	private Store<String> s1;
	@Autowired
	private Store<Integer> s2;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值