java bean 的xml 头部_以XML文件配置为例,Java对象Bean 创建说明

Spring 框架中有三种配置Bean 的方式,这里以XML配置文件为例说明Bean的创建。

Spring 的配置文件是基于XML格式的,Spring1.0的配置文件采用DTD格式,Spring2.0以后使用Schema的格式,后者让不同类型的配置拥有了自己的命名空间,使配置文件更具有扩展性。

一、以XML文件配置Spring

1.1、XML 文件配置Bean

一个项目中可能存在多个配置文件,那么Spring项目加载多个配置文件的方法:

1、在配置文件中使用import来导入所需的配置文件。

2、将多个配置文件构造为一个数组,然后传递给ApplicationContext实现加载多个配置文件。

这两种方式都是通过调用BeanDefinitionReader来读取定义文件的,在内部实现上没有任何的区别。

1.2、XML 文件配置示例

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

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

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

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/aop

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

在上面的代码中定义了三个命名空间

1、首先定义了一个默认命名空间,他没有空间名,用于Spring Bean的定义。

2、接下来命名了一个xsi命名空间,这个命名空间用于为每个文档中命名空间指定相对应的schema的样式文件。是标准组织定义的标准命名空间。

3、第三个命名了一个aop的命名空间,这个命名空间是Spring配置aop的命名空间,是用户自定义的命名空间。

1.3、命名空间的定义分为了两个步骤

1、指定命名空间的名称,需要指定命名空间的缩类名和全名。

2、指定命名空间的schema文档样式文件的位置,用空格或回车行来进行分割。

指定命名空间schema地址有两个用途:

1、xml解析器可以获取schema文件,并对文档进行格式合法性验证。

2、在开发环境下IDE可以用schema文件来对文档编辑器进行诱导功能。

Spring3.0 的配置Schema文件分布在各模块类包中,如果模块拥有对应的Schema文件,则可以在模块类包中找到一个config目录,Schema文件就位于该目录中。

1.4、Schema 说明

示例说明:Spring-beans-3.0.xsd

命名空间:http://www.springframework.org/schema/beans

Schema 文件:http://www.springframework.org/schema/beans/spring-beans-3.0.xsd

可以看出在Spring3.0当中,所有的Schema文件的命名空间以及对应的位置都和Beans这个Schema文件是类似的。

1.5、Spring中其他Schema文件的用途

spring-beans-3.0.xsd:Spring3.0最主要的配置文件,主要是用于配置

Beanspring-aop-3.0.xsd:aop配置定义的schema

spring-tx-3.0.xsd:声明式事物配置定义的Schema

spring-mvc-3.0.xsd:Spring3.0当中新增的

spring-util-3.0.xsd:是为简化某些复杂的标准配置而提供的Schema

spring-jee-3.0.xsd:是为简化J2EE中EJB等功能的配置而提供的Schema

spring-jdbc-3.0.xsd:为Spring内接数据库而提供的Schema,3.0新增

spring-jms-3.0.xsd:jms配置的Schema

spring-lang-3.0.xsd:增加了对动态语言的支持,为集成动态语言而定义

spring-oxm-3.0.xsd:配置对象xml映射到schema,3.0新增

spring-task-3.0.xsd:任务调度的Schema

spring-tool-3.0.xsd:为集成Schema一些有用工具而提供的Schema

二、Bean的三种创建方式

方式一、调用默认无参构造函数创建 ( 此种方式用的最多)

默认情况下,如果类中没有默认无参构造函数,则创建失败,会报异常

car.java 代码文件

public class Car {

private String brand;

private double price;

public Car(){

System.out.println("Car对象创建了 ");

}

public String getBrand() {

return brand;

}

public void setBrand(String brand) {

System.out.println("set brand: " + brand);

this.brand = brand;

}

@Override

public String toString() {

System.out.println("get the car string type");

return "Car{" +

"brand=" + brand'}';

}

}

applicationContext.xml 配置文件

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.xsd

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

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

index.java 代码文件

public class Index {

public static void main(String args[]) throws SQLException {

ApplicationContext act = new ClassPathXmlApplicationContext("applicationContext.xml");

Car car = (Car) act.getBean("car");

System.out.println(car);

}

}

方式二、使用静态工厂中的方法创建对象

需要使用bean标签的factory-method 属性,指定静态工厂中创建对象的方法

添加静态工程文件 StaticFactory.java

public class StaticFactory {

public static Car getCar(){

return new Car();

}

}

applicationContext.xml 配置文件修改一行

方式三、使用实例工厂中的方法创建

需要使用bean标签中的factory-bean=”instanceFactory” factory-method=“实例工程方法”。

创建InstanceFactory 文件

public class InstanceFactory {

public Car getCar(){

return new Car();

}

}

applicationContext.xml 配置文件修改

factory-bean="instanceFactory" factory-method="getCar"/>

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值