通过前面的学习。我们会感觉到对于一个有较多Bean的大项目,Spring的配置会比較复杂。
那么接下来我们就介绍怎样简化Spring的配置。
简化Spring的配置主要分为两类:
1. 自己主动装配
2. 自己主动扫描
以下就具体介绍这两种简化配置的方式。
自己主动装配的种类
使用XML实现自己主动装配
<bean id="person" class="com.njupt.Person" autowire="byName">
bean>
<bean id="person" class="com.njupt.Person" autowire="byType">
bean>
byType的缺点:假设某一类型的bean有多个。那Spring在通过byType为属性寻找同类型的bean时就会抛出异常。
解决方式:
– 为同样类型的bean设置是否首选
在须要被首选的bean中作例如以下设置:
<bean id="person" class="com.njupt.Person" primary="true">
bean>
在不须要被首选的bean中作例如以下设置:
<bean id="person" class="com.njupt.Person" primary="false">
bean>
- 取消某一些同样类型bean的候选资格
使用auto-candidate属性取消某些bean的候选资格。Spring在为属性寻找同类型的bean时直接排除这些bean。
<bean id="person" class="com.njupt.Person" default-candidate="false">
bean>
<bean id="person" class="com.njupt.Person" autowire="constructor">
bean>
构造器的自己主动装配本质上仍是通过byType进行装配,仅仅只是autowire=”constructor”时。Spring会对构造器的參数进行自己主动装配。而autowire=”byType”时,Spring会对bean的成员变量进行自己主动装配。
构造器的自己主动装配和byType自己主动装配具有同样的缺点:当某一类型的bean有多个时,Spring无法确定到底选择哪个bean,就直接抛出异常。
此外。构造器的自己主动装配还有个独特的缺点:当构造器有多个时,Spring也无法选择到底初始化哪个构造器,因此也直接跑出异常。
使用默认自己主动装配
上述自己主动装配的方法都是针对单个bean,假设要让beans下的全部bean均使用某种自己主动装配策略,那么在beans标签中添加例如以下配置:
default-autowire="byType"
- 注意1:在beans中设置了default-autowire后。这个參数仅对当前beans标签之间的bean有效。
- 注意2:採用默认自己主动装配后。仍然能够在bean中设置特有的自己主动装配策略,bean中的自己主动装配策略会覆盖默认策略。
- 注意3:使用了自己主动装配后,我们仍然能够在bean中通过constructor-arg属性和property属性对bean进行显示装配。
这样的混合使用显示装配和自己主动装配的方式能够成功地解决byType出现的不确定性问题。 - 注意4:假设使用了constructor来实现构造器參数的自己主动装配,那么就不能混合使用autowire=”constructor”属性和constructor-arg标签。
使用注解实现自己主动装配
使用注解装配事实上就是把原本XML中bean中的autowire=”xxx”属性搬到了Bean类的Java代码中了。功能上没啥区别。
以下就来介绍怎样使用注解实现自己主动装配。
在beans中作例如以下配置:
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.5.xsd">
<context:annotation-config>
beans>
当Bean中的属性、函数被标记@Autowired后。Spring在创建这个bean的对象时,会通过byType寻找与属性、函数參数同样类型的bean进行自己主动装配。
– 用@Autowired标注构造函数
@Autowired
public Person(String name,long id){
this.name = name;
this.id = id;
}
- 用@Autowired标注普通函数
@Autowired
public void createInstance(){
System.out.println("对象被创建啦!");
}
- 用@Autowired标注属性
@Autowired
private Father father;
@Autowired本质上採用byType进行自己主动装配。因此也存在与byType一样的问题:若同一类型的bean有多个时,或找不到该类型的bean。Spring就会抛出异常。
@Autowired
@Qualifier("father")
private Father father;
@Qualifier(“ID”)会依据bean的id为father装配。
@Autowired(required="false")
public Person(String name,long id){
this.name = name;
this.id = id;
}
此时,假设Spring找不到与构造函数的參数同样类型的bean的话,就赋上null。
注意:若一个bean有多个构造函数时。仅仅有一个构造函数能够设为@Autowired(required=true),其余的构造函数都仅仅能设为@Autowired(required=false)
通过前面学习我们知道,在Spring的XML配置中,能够使用SpEL表达式来实现手动装配。同样,在注解中也能够使用SpEL表达式实现手动装配。
– 将名为father的bean注入给构造函数:
@Value("#{father}")
public Person(Father father){
this.father = father;
}
- 将father对象中的id注入给id:
@Value("#{father.id}")
public void setId(long id){
this.id = id;
}
自己主动装配能够降低bean标签下property标签和constructor-arg标签的数量。而自己主动检測能降低bean标签的数量。
腰开启自己主动检測功能,须要在XML的beans标签中作例如以下配置:
– base-package: 表示要扫描的包
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.5.xsd">
<context:component-scan base-package="com.xxx">context:component-scan>
beans>
要让一个Java类变成一个Bean。须要在类上加上@Component注解。
Spring在启动的时候会到base-package指定的包下寻找被@Component标记的类,把他们初始化为bean。保存在SpringContext中。
– 将类型的小写作为bean的名字:
@Component
class Person{
}
- 指定bean的名字:
@Component("person")
class Person{
}
尽管使用注解已经大大降低Spring中的XML配置,但仍然须要少量的XML配置,我们能够将XML配置用Java代码实现,从而全然避免了XML配置。
用@Configuration标签标注一个类,表示这个类是Spring的配置类:
@Comfiguration
class Person{
......
}
在Spring的配置类中,使用@Bean标签标注一个bean。
- 函数名:bean的id
- 返回值:bean的类型
- 函数体:初始化这个bean
@Comfiguration
class Person{
@Bean
public Person person(){
Person person = new Person("柴毛毛");
person.setAge(22);
return person;
}
}
在採用Java进行Spring的配置中,对bean属性和构造器的注入很easy,仅仅需在函数中操作就可以:
@Comfiguration
class Person{
@Bean
public Person person(){
return new Person();
}
}
在XML配置中。bean的类型是用String表示的。因此仅仅有在执行结点才干发现bean的类型是否写错;而在Java配置中,在编译阶段就能发现bean的类型是否出错。从而能够尽早地发现错误。
Original: https://www.cnblogs.com/yfceshi/p/7401544.html
Author: yfceshi
Title: 跟着柴毛毛学Spring(3)——简化Bean的配置
原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/541804/
转载文章受原作者版权保护。转载请注明原作者出处!