Spring框架系列(三)-XML配置和Java配置

附上示例程序的github地址:https://github.com/bjtudujunlin/SpringDataExample


1、 Spring配置方式

Spring常用的配置方式有两种,基于XML的配置和基于Java的配置。XML配置是早期Spring采用的一种方式,后来出现了强大的基于Java的配置,从此逃离了大量XML文件的编写工作,但是鉴于已经存在那么多基于XML的Spring配置,所以理解如何在Spring中使用XML还是很重要的。所以这里对两种方式都进行了介绍。

2、 XML配置

XML配置可以结合《Spring框架系列(一)-整体架构》中IOC的例子进行理解。

A、 首先需要创建XML的配置规范

在使用XML时,需要在配置文件的顶部声明多个XML模式(XSD)文件,这些文件定义了配置Spring的XML元素,也就是告诉系统怎么去解析XML文件中的元素标签,如果缺少了对应的XSD文件,系统会提示“无法找到元素 'XXX'的声明”。

下面来看看具体例子。

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"

       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

       xmlns:aop="http://www.springframework.org/schema/aop"

       xmlns:context="http://www.springframework.org/schema/context"

       xsi:schemaLocation="http://www.springframework.org/schema/beans

            http://www.springframework.org/schema/beans/spring-beans.xsd

                     http://www.springframework.org/schema/context

                     http://www.springframework.org/schema/context/spring-context.xsd 

                     http://www.springframework.org/schema/aop

                     http://www.springframework.org/schema/aop/spring-aop-3.0.xsd ">

</beans>

       在示例中,beans是整个配置文件的根节点,内部可以包含一个或者多个bean节点。

xmlns="http://www.springframework.org/schema/beans"

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

顾名思义,xmlns指的就是xml配置的命名空间,上面这两个是Spring最基本的命名空间定义,所有的xml配置中,这两个是必不可少的。

xmlns:aop定义了启用AOP功能时的命名空间,xmlns:context定义了启用自动扫描或者注解装配时的命名空间,另外还有xmlns:tx,这里没有列出来,定义了启用事务时的命令空间。

xsi:schemaLocation属性定义了上述命名空间配套的xsd文件装载路径,该属性必须是偶数行,奇数行表示命名空间,相邻偶数行表示xsd文件装载路径,例如

http://www.springframework.org/schema/aop

http://www.springframework.org/schema/aop/spring-aop-3.0.xsd

在创建XML配置规范的时候,需要根据具体需要来添加命名空间和xsd文件。

 

B、 声明Bean

声明一个Bean很简单,如下所示,添加bean元素,指定class属性就行,该属性是全限定的类名。

<bean class="iscas.springstudy.Person"></bean>

因为没有明确给定ID,所以这个bean将会根据全限定类名来进行命名。在本例中,bean的ID将会是“iscas.springstudy.Person#0”。其中,“#0”是一个计数的形式,用来区分相同类型的其他bean。如果你声明了另外一个Person,并且没有明确进行标识,那么它自动得到的ID将会是“iscas.springstudy.Person #1”。为了便于记忆,用户往往需要自己设定bean的id,这可以通过id属性来指定,如下:

<bean id="person" class="iscas.springstudy.Person">

需要注意的是,在没有特殊处理的情况下,Spring配置的Bean默认为单例模式,就是说整个context只有一个id为“xx”的bean,通过getBean方法获取到的都是这一个bean。

 

C、 借助构造器注入初始化bean

声明了Bean,紧接着我们需要利用构造函数初始化这个bean,这也是可以配置的。Person类的构造函数定义如下:

public Person(Region region,String discribe)

       所以我们需要注入两个构造参数,内容如下

              <bean id="person" class="iscas.springstudy.Person">

                            <constructor-arg ref="regiona"></constructor-arg>

                      <constructor-arg value="a funny man"></constructor-arg>

              </bean>

       通过constructor-arg属性可以完成构造函数配置,按照构造函数参数从左到右的顺序,ref标签表示参数为指定id的bean,value标签表示参数值是一个字符串。

      

D、设置属性

构造函数有了,我们还需要为Bean配置属性,内容如下

       <bean id="person" class="iscas.springstudy.Person">

              <constructor-arg ref="regiona"></constructor-arg>

              <constructor-arg value="a funny man"></constructor-arg>

              <property name="name" value="liming"></property>

              <property name="age" value="19"></property>

       </bean>

       Property定义了属性,name为属性名,value为属性的值,在例子中这两个属性都是字符串。在实际应用中,属性值可以是int,也可以是List,基本覆盖常用的数据结构。

 

E、  使用

配置完成后,就可以将xml文件加载到上下文中进行使用。加载方式如下,通过ClassPathXmlApplicationContext加载classpath路径下的xml文件,得到context上下文对象,然后根据id获取到bean。

ApplicationContext application = new ClassPathXmlApplicationContext("spring-config.xml");

Person person = (Person) application.getBean("person");

 

3、 Java配置

再来看看基于java的配置,这种配置方式更加简单,不再需要XML文件,看看下面的例子,我们新创建一个类JavaConfigPerson,类中有两个方法person和regiona。和普通类有区别的地方在于多了@Configuration和@Bean两个注解,@Configuration就相当于xml中步骤A里面定义的配置规范,是不是一下少了好多行。@Bean就相当于xml中声明Bean。类中的方法名regiona、person就相当于xml配置中的id,只是它们也可以当作普通方法来使用。

 

import org.springframework.context.annotation.Bean;

import org.springframework.context.annotation.Configuration;

 

@Configuration

public class JavaConfigPerson {

       @Bean

       public Person person (){

              Person person =  new Person(regiona (),"a funny man");

              person.setName("liming");

              person.setAge("19");

              return person;

       }

       @Bean

       public Region regiona (){

              Region region = new Region();

              region.setProvince("beijing");

              region.setCity("haidian");

              return region;

       }

}

 

配置完成后,怎么使用呢,利用AnnotationConfigApplicationContext类来实例化context上下文对象,实例化完成后操作跟xml配置完全相同,例子如下:

ApplicationContext ctx = new AnnotationConfigApplicationContext(JavaConfigPerson.class);

Person person = (Person) ctx.getBean("getPerson");

 

4、 混合配置

基于XML配置一般是一些老的系统,为了将新添加的基于java配置的系统与基于xml配置的系统进行集成,需要添加再配置文件中添加context:component-scan标签,扫描指定路径下基于java 的配置,添加到context上下文中。

<context:component-scan base-package="iscas.springstudy.aop" />

       在上面的配置中,系统会自动扫描包iscas.springstudy.aop下面包含@Configuration注解的类。

       至此完成了基于xml和java的混合配置,加载使按照基于xml配置生成context上下文即可。

  • 4
    点赞
  • 24
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值