Spring(1)--在IoC容器中添加bean

〇、整体架构

一、装配Bean

1 <bean id="Foo" class="com.will.Foo"/>          <!--使用id命名,不可id相同-->
2 <bean name="#Foo" class="com.will.Foo"/>  <!--使用name命名,没有规范限制,而且可以重复,返回后面一个-->
3  <bean class="com.will.Foo"/>                     <!--可以都不写,使用全类名返回-->
4 
5 <!--getBean("com.will.Foo")-->                                                          

二、依赖注入

1、属性注入

必须提供默认(无参)构造函数,使用Setter方法进行注入

 1 <bean id="" class=""/>
 2     <!--常值注入-->   
 3     <property name="brand" value=""/>
 4     <!--使用级联属性 常值注入-->   
 5     <property name="brand.number" value=""/>
 6           <!--引用注入-->  
 7         <property name="car" ref="car"/>    
 8 </bean>  
 9 
10 <!--简化方式可以使用p空间命名-->
11 <bean id="" class=""
12     <!--常值注入--> 
13     p:brand=""
14     p:brand.name=""
15     p:price=""
16     <!--引用注入--> 
17     p:car-ref="car"/>

注意事项:属性配置匹配的是Setter方法而不是私有字段。p命名空间需要进行配置定义。

2、构造函数注入

必须提供带参构造器

<bean id="" class=""/>
    <!--按类型匹配入参-->
    <constructor-arg type="" value=""/>
    <constructor-arg type="" value=""/>
    <!--按索引匹配入参-->
    <constructor-arg index="0" value=""/>
    <constructor-arg index="1" value=""/>
    <!--联合使用匹配入参-->
    <constructor-arg index="0" type="" value=""/>
    <constructor-arg index="1" type="" value=""/>
    <!--当非基础数据类且类型各异 可通过自身类型反射匹配入参-->
    <constructor-arg  value=""/>
    <constructor-arg  ref=""/>
    <constructor-arg  ref=""/>
</bean> 

注意事项:当有构造器有循环依赖调用,造成类似死锁的情况时,使用属性注入。

3、工厂注入(不推荐)

在一些遗留系统或第三方类库中使用

<!--非静态-->
<!--工厂类Bean-->
<bean id="carFactory" class=""/>
<!--根据工厂创建类-->
<bean id="" factory-bean="carFactory" factory-method=""/>

<!--静态-->
<bean id="" class="工厂类全类名" factory-method=""/>

注意事项:(1)变量的前两个字母要么全大写要么全小写。(2)特殊符号(<、>、&、“、‘)使用转义序列。(3)可以使用内部Bean,和内部类相似。(4)空值需要填入null;

三、方法注入

1、lookup方法注入(需要CGLib类包)

一般在希望通过一个singleton Bean 获取一个prototype Bean时使用

1 <bean id="car" class=""
2 p:brand="" scope="prototype"/>
3 
4 <bean id="boss" class=""/>
5 <lookup-method name="boss中的方法其返回一个car" bean="car"/>
6 </bean>

2、方法替换

 用于替换他人的Bean必须实现MethodReplacer接口

用于替换掉同名的方法

1 <bean id="boss1" class="">
2     <replaced-method name="" replacer="boss2"/>
3 </bean>
4 
5 <bean id="boss2" class="">

四、Bean之间的关系

1、继承

与代码的继承一样

1 <!--定义为抽象bean-->
2 <bean id="parent" class="" abstract=true/>
3 <!--继承的bean-->
4 <bean id="" class="" parent="parent"/>

2、依赖

当一个Bean初始化时保证其依赖的Bean已经被初始化

1 <bean id="" class="" depends-on="Init"/>
2 <bean id="Init" class="" />

3、引用

想在运行期通过getBean(beanName)方法获取对应的Bean

1 <bean id="car" class=""/>
2 <bean id="boss" class=""/>
3     <property name="carId">
4         <idref bean="car"/>
5     </property>
6 </bean>

五、整合多个配置文件

1 <import resource="classpath:com/will/beans.xml"/>

 六、Bean作用域

scope=""  singleton prototype request session globalSession

非Web相关作用域引用Web相关作用域的Bean需要创建代理 (JDK只支持对接口的代理,引入CGLib才支持对类的代理)使用aop Schema

1 xmlns:aop="http://www.springframework.prg/schema/aop"
2 <bean id="car" class="" scope="request">
3     <aop:scoped-proxy/>
4 <bean>
5 
6 <bean id="" class="">
7     <property name="car" ref="car"/>
8 </bean>

七、基于注解的配置

1 <context:component-scan base-package="com.smart.anno">
2 
3 <!--
4 <context:component-scan base-package="com.smart">
5      <context:include-filter type="regex"     
6                                       expression="com.smart.anno.*"/>
7 </context:component-scan>

使用类别regex可以完成所有类型的匹配。

<context:component-scan>有一个use-default-filters属性 默认为true 默认对以下进行扫描,优先级高于过滤器。

@Component(对进行标注)  {@Repository(对DAO实现类),@Service(Service实现类),@Controller(Service实现类)}

1、自动装配Bean

 1 import org.springframework.beans.factory.annotation.Autowired;
 2 import org.springframework.beans.factory.annotation.Qualifier;
 3 import org.springframework.stereotype.Service;
 4 
 5 @Service @Scope("prototype") //注解形式的作用域设置
 6 public class LoginService{
 7 
 8 @Autowired   //在Setter上使用同理,建议在方法上使用更好!!
 9 private LogDao logDao;
10 
11 @Autowired(required=false)  //表明这个注入不是必须的,可以匹配不到
12 private UserDao userDao;
13 
14 @Autowired
15 @Qualifier("serveDao")         //匹配的类型有多个Bean,通过名称注入
16 private serveDao serveDao;
17 
18 @Autowired  //会将所有符合类型的Bean都注入 可以在Plugin的实现类中使用@Order(value=1)进行按先后插入,数字越小越先插入。
19 private List<Plugin>plugins;
20 
21 @Autowired
22 private List<String,Plugin>plugins;//key是Bean的名字 value是所有实现了Plugin的Bean
23 }

八、基于Java类的配置

 

九基于Groovy DSL的配置

原文地址:https://www.cnblogs.com/WutingjiaWill/p/7766121.html