当前位置: 首页 > news >正文

.net做的网站/深圳网站建设资讯

.net做的网站,深圳网站建设资讯,软件外包平台的服务机构,河间网站建设推广文章目录Spring 框架学习(四)---- 常用配置一、 别名二、bean 的配置三、import(1)存在问题(2)总结Spring 框架学习(四)---- 常用配置 现在这里简单了解一下spring 配置文件中的一些常用配置,在后面我们还会遇到更多的…

文章目录

  • Spring 框架学习(四)---- 常用配置
  • 一、 别名
  • 二、bean 的配置
  • 三、import
    • (1)存在问题
    • (2)总结

Spring 框架学习(四)---- 常用配置


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

在这里插入图片描述

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

  • description描述不太重要,

  • bean在之前已经见识过了,

  • alias给bean起别名,

  • import在当前xml文件中导入其他xml文件


一、 别名


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


1、alias 配置

   <alias name="user" alias="u"/>

alias是给bean的id起别名

  • name 是bean的id

  • alias 是bean的别名

(1)先定义普通实体类

package com.kuang.pojo;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;@Getter
@Setter
@ToString
public class User {private int id;private String userName;private String password;
}

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

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsd"><alias name="user" alias="u"/><bean id="user" class="com.kuang.pojo.User"><property name="id" value="1"/><property name="userName" value="root"/><property name="password" value="123456"/></bean></beans>

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

    public static void main(String[] args) {ApplicationContext context = new ClassPathXmlApplicationContext("ApplicationContext.xml");User user = context.getBean("u",User.class);System.out.println(user);}

(4)查看运行结果

在这里插入图片描述


二、bean 的配置


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

   <bean id="user" class="com.kuang.pojo.User" name="u1,u2,u3,u4"><property name="id" value="1"/><property name="userName" value="root"/><property name="password" value="123456"/></bean>

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


三、import


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

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

  • 张三 beans1.xml

  • 李四 beans2.xml

  • 王五 beans3.xml

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

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

User类

package com.kuang.pojo;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;@Getter
@Setter
@ToString
public class User {private int id;private String userName;private String password;}

bean1.xml


<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsd"><bean id="user" class="com.kuang.pojo.User"><property name="id" value="1"/><property name="userName" value="root"/><property name="password" value="123456"/></bean></beans>

李四负责 Student类,bean2.xml

Stduent 类

package com.kuang.pojo;import lombok.Getter;
import lombok.Setter;
import lombok.ToString;@Setter
@Getter
@ToString
public class Student {private int id;private String name;private String sex;
}

bean2.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsd"><bean id="student" class="com.kuang.pojo.Student"><property name="id" value="1"/><property name="name" value="张三"/><property name="sex" value=""/></bean></beans>

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

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsd"><import resource="bean1.xml"/><import resource="bean2.xml"/></beans>

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

 public static void main(String[] args) {ApplicationContext context = new ClassPathXmlApplicationContext("ApplicationContext.xml");User user = context.getBean("user",User.class);Student student = context.getBean("student",Student.class);System.out.println(user);System.out.println(student);}

(1)存在问题


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

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

  • 如果总配置文件中有取这个bean,

  • 如果在导入的xml文件中,因为导入的时候id相同的bean会不断覆盖,同名的bean后面的xml会覆盖前面的 xml,所以最后取的是最后导入这个id的xml文件中的bean


(2)总结


与主配置中的id重名,调用主配置中的id;

多个import中配置中的id重名,调用最后import中配置中的id重名,即后面的覆盖前面的;

http://www.lbrq.cn/news/1247365.html

相关文章:

  • 网站的首页设计方案/四川seo快速排名
  • 广告图案大全图片素材/移动端关键词优化
  • 深圳做网站排名价格/市场营销策划公司排名
  • 外贸网站建设推广公司前景如何/北京本地网络推广平台
  • dw网站log怎么做/百度指数怎么查
  • 免费的黄冈网站有哪些平台软件/河北seo推广方案
  • ps做游戏网站/搜索引擎推广有哪些平台
  • 快速建立平台网站开发需要多少钱/百度搜索风云榜排名
  • 大连甘井子区二手房/seo的内容有哪些
  • 品牌网站建设流程/seo视频教程百度云
  • 怎么创建网站 免费的/外链相册
  • 旅游网站建设方案/网站开发的一般流程
  • 百度最容易收录的网站/郑州网络推广厂家
  • 网站后台浏览器/网站制作的基本流程是什么
  • 怎么做一键添加信任网站/百度竞价ocpc投放策略
  • wordpress标签分级/seo推广效果
  • 昆明网站建设咨询/网站关键词优化排名
  • 怎样 管理网站/北京做网站的公司有哪些
  • 网站建设及应用实施方案/平台连接
  • b站视频推广网站动漫/企业营销型网站
  • 建网站广州/外贸seo软文发布平台
  • 查找手机网站/网站友情链接
  • 手机网站开发下载/电商seo
  • 瑞安网站/企业查询系统官网
  • 购物网页设计/河南seo关键词排名优化
  • 免费seo工具/seo学院
  • 商城网站开发文档/百度推广需要什么条件
  • 聊城网站建设包括哪些/n127网推广
  • 手机网站整站模板下载/郑州网站优化seo
  • 淘宝做任务赚钱网站/百度域名注册
  • 构型空间(Configuration Space,简称C-space)
  • 【预判一手面试问题:排序】
  • 【LeetCode 热题 100】(二)双指针
  • 电脑出现英文字母开不了机怎么办 原因与修复方法
  • 自然语言处理NLP (1)
  • 通过不同坐标系下的两个向量,求解旋转矩阵