2. Spring 配置Bean

2018-11-06  本文已影响0人  迎风布阵x

配置形式:

  1. 基于XML文件的方式
  2. 基于注解的方式
    (为了使文章不至于过于亢长,一些配置知识和技巧会在2.x的文章中进行阐述)

1 .基于XML文件的方式

public class HelloWorld {
    
    private String name;
    
    public void setName(String name) {
        this.name = name;
        System.out.println("helloworld set name = "+name);
    }
    
    public void hello() {
        System.out.println("hello "+name);
    }

    public HelloWorld() {
        super();
        System.out.println("helloWorld's constructor is running");
        
        // TODO Auto-generated constructor stub
    }
}
<!-- 配置bean -->
    <bean id="helloWorld" class="thread.conor.spring.beans.HelloWorld">
        <property name="name" value="Spring"></property>
    </bean> 

其中id为这个bean的id(后面Main类中要用到),这个class中的是你写的HelloWord的全类名,其中property中配置这个bean的属性,name中的是原来那个类中属性的属性名,value中的是赋给这个属性的属性值

public class Main {
    public static void main(String[] args) {
        //此处的applicationContext.xml即为配置bean时所使用的配置文件
        ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
        //这里的“helloWorld”字符串与applicationContext.xml文件中的标签中的id属性相对应
//      HelloWorld helloWorld = (HelloWorld) ctx.getBean("helloWorld");
//      System.out.println(helloWorld);
//      helloWorld.hello();
    }
}

直接运行的结果

原理分析

这里介绍一下此程序的思路。

  1. 编写HelloWorld类
  2. 在项目根目录下(就是你项目下的第一级目录)创建并编写xml文件来配置Bean对象。
  3. 编写Main类测试这个Bean对象(其中你需要创建一个IOC容器实现ApplicationContext接口的实体类,而你所配置好的Bean对象就在这个IOC容器中,可以通过它的方法来得到)



xml配置Bean属性的细节:

此处引用尚硅谷的课件

<!-- 配置一个 bean -->
    <bean id="helloWorld" class="com.atguigu.spring.helloworld.HelloWorld">
        <!-- 为属性赋值 -->
        <property name="user" value="Jerry"></property>
    </bean>
<!-- 若一个 bean 有多个构造器, 如何通过构造器来为 bean 的属性赋值 -->
    <!-- 可以根据 index 和 value 进行更加精确的定位. (了解) -->
    <bean id="car" class="com.atguigu.spring.helloworld.Car">
        <constructor-arg value="KUGA" index="1"></constructor-arg>
        <constructor-arg value="ChangAnFord" index="0"></constructor-arg>
        <constructor-arg value="250000" type="float"></constructor-arg>
    </bean>
    
    <bean id="car2" class="com.atguigu.spring.helloworld.Car">
        <constructor-arg value="ChangAnMazda"></constructor-arg>
        <!-- 若字面值中包含特殊字符, 则可以使用 DCDATA 来进行赋值. (了解) -->
        <constructor-arg>
            <value><![CDATA[<ATARZA>]]></value>
        </constructor-arg>
        <constructor-arg value="180" type="int"></constructor-arg>
    </bean>

<bean id="dao5" class="com.atguigu.spring.ref.Dao"></bean>

    <bean id="service" class="com.atguigu.spring.ref.Service">
        <!-- 通过 ref 属性值指定当前属性指向哪一个 bean! -->
        <property name="dao" ref="dao5"></property>
    </bean>
<bean id="action" class="com.atguigu.spring.ref.Action">
        <property name="service" ref="service2"></property>
        <!-- 设置级联属性(了解) -->
        <property name="service.dao.dataSource" value="DBCP2"></property>
    </bean>
<bean id="dao2" class="com.atguigu.spring.ref.Dao">
        <!-- 为 Dao 的 dataSource 属性赋值为 null, 若某一个 bean 的属性值不是 null, 使用时需要为其设置为 null(了解) -->
        <property name="dataSource"><null/></property>
    </bean>
<!-- 装配集合属性 -->
    <bean id="user" class="com.atguigu.spring.helloworld.User">
        <property name="userName" value="Jack"></property>
        <property name="cars">
            <!-- 使用 list 元素来装配集合属性 -->
            <list>
                <ref bean="car"/>
                <ref bean="car2"/>
            </list>
        </property>
    </bean>
<!-- 声明集合类型的 bean -->
    <util:list id="cars">
        <ref bean="car"/>
        <ref bean="car2"/>
    </util:list>
    <bean id="user2" class="com.atguigu.spring.helloworld.User">
        <property name="userName" value="Rose"></property>
        <!-- 引用外部声明的 list -->
        <property name="cars" ref="cars"></property>
    </bean>
    
    <bean id="user3" class="com.atguigu.spring.helloworld.User"
        p:cars-ref="cars" p:userName="Titannic"></bean>
        
    <!-- bean 的配置能够继承吗 ? 使用 parent 来完成继承 -->    
    <bean id="user4" parent="user" p:userName="Bob"></bean>
    
    <bean id="user6" parent="user" p:userName="维多利亚"></bean>
    
  1. 子Bean从父Bean中继承配置,包括Bean的属性配置
  2. 子Bean可以覆盖继承过来的配置
  3. 父Bean可以作为配置模板,也可以作为Bean的实例,若只想把父Bean作为模板,可以设置<bean>的abstract属性为true,这样Spring将不会实例化这个Bean
  4. 并不是<bean>中的所有属性都会被继承,如:autowire,abstract等属性就不会被继承。
  5. 也可以忽略父Bean中的class属性,让子Bean指定自己的类,而共享相同的属性配置。但此时abstract必须为true
  1. byType(根据类型自动装配): 必须保证IOC容器中与目标Bean类型一致的Bean只有唯一 一个
  2. byName(根据名称进行自动装配): 必须将目标Bean的名称和属性名设置得完全相同。

Spring允许用户通过depends-on属性设定Bean的前置依赖的Bean,前置依赖的Bean会在本Bean实例化之前创建好
如果前置依赖于多个Bean,则可以通过逗号或空格的方式配置Bean的名称



2. 基于注解的方式

  1. classPath中扫描组件
  1. @Component: 基本注解,标识了一个受Spring管理的组件
  2. @Repository: 标识持久层组件(如数据库的DAO之类)
  3. @Service:标识服务层(业务层)组件
  4. @Controller: 标识表现层组件
<!-- 指定Spring IOC 容器扫描的包 -->
    <context:component-scan 
    base-package="thread.conor.spring.beans.annotation">  </context:component-scan>

<context:component-scan
  base-package="thread.conor.spring.beans"
  resource-pattern="autowire/*.class"/>
类别 示例 说明
annotation thread.conor.XxxAnnotation 所有标注了XxxAnnotation的类
assinable thread.conor.XxxService 所有继承或扩展了XxxService的类
aspectj thread.conor..*Service+ 所有类名以Service结束的类及继承或扩展它们的类。该类型采用AspectJ表达式进行过滤
regex thread.\conor.anno..* 所有thread.conor.anno包下的类,采用正则过滤
custom thread.conor.XxxTypeFilter 采用XxxTypeFilter通过代码的方式定义过滤规则,该类必须实现org.springframework.core.TypeFilter接口
上一篇 下一篇

猜你喜欢

热点阅读