Spring深入了解常用配置应用_第1页
Spring深入了解常用配置应用_第2页
Spring深入了解常用配置应用_第3页
Spring深入了解常用配置应用_第4页
Spring深入了解常用配置应用_第5页
已阅读5页,还剩2页未读, 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

第Spring深入了解常用配置应用目录常用配置一、别名二、bean的配置三、import存在问题总结

常用配置

现在这里简单了解一下spring配置文件中的一些常用配置,在后面我们还会遇到更多的配置,在后文继续进行介绍了。

spring中的配置一共也就这几个

description描述不太重要,bean在之前已经见识过了,alias给bean起别名,import在当前xml文件中导入其他xml文件

一、别名

在spring中别名主要是给bean的id起一个别名,同样也有好几种方式。

1、alias配置

aliasname=useralias=u/

alias是给bean的id起别名

name是bean的idalias是bean的别名

(1)先定义普通实体类

packagecom.kuang.pojo;

importlombok.Getter;

importlombok.Setter;

importlombok.ToString;

@Getter

@Setter

@ToString

publicclassUser{

privateintid;

privateStringuserName;

privateStringpassword;

}

(2)在配置文件中装配bean,并定义bean的别名

xmlversion="1.0"encoding="UTF-8"

beansxmlns="/schema/beans"

xmlns:xsi="/2001/XMLSchema-instance"

xsi:schemaLocation="/schema/beans

/schema/beans/spring-beans.xsd"

aliasname="user"alias="u"/

beanid="user"

propertyname="id"value="1"/

propertyname="userName"value="root"/

propertyname="password"value="123456"/

/bean

/beans

(3)通过别名也能拿到装配的bean

publicstaticvoidmain(String[]args){

ApplicationContextcontext=newClassPathXmlApplicationContext("ApplicationContext.xml");

Useruser=context.getBean("u",User.class);

System.out.println(user);

(4)查看运行结果

二、bean的配置

也可以通过bean来配置别名,而且可以给一个bean配置多个别名

beanid="user"name="u1,u2,u3,u4"

propertyname="id"value="1"/

propertyname="userName"value="root"/

propertyname="password"value="123456"/

/bean

name就是给当前bean配置别名,可以多个别名写在一起,中间使用空格/逗号/分号进行分割,spring都能识别

三、import

在团队开发使用中,还是非常常见的。它可以将多个配置文件,导入合成一个

假设一个团队中有多个人进行开发,这三个人负责不同类的开发,不同的类需要注册到不同的bean中

张三beans1.xml李四beans2.xml王五beans3.xml

我们可以利用import将所有人的beans.xml合并成一个总的ApplicationContext.xml,最后使用的时候使用总的配置文件即可。

张三负责User类以及注册到bean1.xml文件中

User类

packagecom.kuang.pojo;

importlombok.Getter;

importlombok.Setter;

importlombok.ToString;

@Getter

@Setter

@ToString

publicclassUser{

privateintid;

privateStringuserName;

privateStringpassword;

}

bean1.xml

xmlversion="1.0"encoding="UTF-8"

beansxmlns="/schema/beans"

xmlns:xsi="/2001/XMLSchema-instance"

xsi:schemaLocation="/schema/beans

/schema/beans/spring-beans.xsd"

beanid="user"

propertyname="id"value="1"/

propertyname="userName"value="root"/

propertyname="password"value="123456"/

/bean

/beans

李四负责Student类,bean2.xml

Stduent类

packagecom.kuang.pojo;

importlombok.Getter;

importlombok.Setter;

importlombok.ToString;

@Setter

@Getter

@ToString

publicclassStudent{

privateintid;

privateStringname;

privateStringsex;

}

bean2.xml

xmlversion="1.0"encoding="UTF-8"

beansxmlns="/schema/beans"

xmlns:xsi="/2001/XMLSchema-instance"

xsi:schemaLocation="/schema/beans

/schema/beans/spring-beans.xsd"

beanid="student"

propertyname="id"value="1"/

propertyname="name"value="张三"/

propertyname="sex"value="男"/

/bean

/beans

总的ApplicationContext.xml配置文件,导入了bean1.xml和bean2.xml

xmlversion="1.0"encoding="UTF-8"

beansxmlns="/schema/beans"

xmlns:xsi="/2001/XMLSchema-instance"

xsi:schemaLocation="/schema/beans

/schema/beans/spring-beans.xsd"

importresource="bean1.xml"/

importresource="bean2.xml"/

/beans

使用的时候,使用总的配置文件即可

publicstaticvoidmain(String[]args){

ApplicationContextcontext=newClassPathXmlApplicationContext("ApplicationContext.xml");

Useruser=context.getBean("user",User.class);

Studentstudent=context.getBean("student",Student.class);

System.out.println(user);

System.out.println(student);

存在问题

同时使用import还存在几个问题导入bean的id冲突

如果导入的文件中有多个重名id相同的bean

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

最新文档

评论

0/150

提交评论