首先说说别名alias,以一个代码的例子讲解

首先是一个User实体类

package com.zhiying.pojo;public class User {private String name;public String getName() {return name;}public void setName(String name) {this.name = name;}public void show() {System.out.println("name=" + name);}
}

然后是配置文件

<?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/beanshttps://www.springframework.org/schema/beans/spring-beans.xsd"><bean id="user" class="com.zhiying.pojo.User"><property name="name" value="贺志营"/></bean><!--    添加了别名我们可以通过别名来获取--><alias name="user" alias="me"/></beans>

最后是通过别名测试

import com.zhiying.pojo.User;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;public class MyTest {public static void main(String[] args) {ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");User user = (User) context.getBean("me");user.show();}
}

运行结果:

可以获取

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/beanshttps://www.springframework.org/schema/beans/spring-beans.xsd"><!--bean的配置id:bean的唯一标识符,也就是我们的对象名class:bean对象对应的全限类名,包名+类名name:也是别名
--><bean id="user" class="com.zhiying.pojo.User" name="hehe"><property name="name" value="贺志营"/></bean></beans>

把测试类也修改了

import com.zhiying.pojo.User;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;public class MyTest {public static void main(String[] args) {ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");User user = (User) context.getBean("hehe");user.show();}
}

这里的name属性值可以有多个,把配置文件修改为下

<?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/beanshttps://www.springframework.org/schema/beans/spring-beans.xsd"><!--bean的配置id:bean的唯一标识符,也就是我们的对象名class:bean对象对应的全限类名,包名+类名name:也是别名,可以同时取多个
--><bean id="user" class="com.zhiying.pojo.User" name="hehe me,you;aa"><property name="name" value="贺志营"/></bean></beans>

测试类也修改了

import com.zhiying.pojo.User;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;public class MyTest {public static void main(String[] args) {ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");User user = (User) context.getBean("hehe");User user1 = (User) context.getBean("me");User user2 = (User) context.getBean("aa");user.show();user1.show();user2.show();}
}

这里的name别名,比上面那个alias强大。

import:可以将多个配置文件导入合并为一个,修改配置文件如下

<?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/beanshttps://www.springframework.org/schema/beans/spring-beans.xsd"><!--bean的配置id:bean的唯一标识符,也就是我们的对象名class:bean对象对应的全限类名,包名+类名name:也是别名
--><bean id="user" class="com.zhiying.pojo.User" name="hehe me,you"><property name="name" value="贺志营"/></bean></beans>
<?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/beanshttps://www.springframework.org/schema/beans/spring-beans.xsd"><bean id="user" class="com.zhiying.pojo.User" name="aa"><property name="name" value="贺志营"/></bean>
</beans>
<?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/beanshttps://www.springframework.org/schema/beans/spring-beans.xsd"><import resource="Beans.xml"/><import resource="beans1.xml"/>
</beans>

然后是测试类

import com.zhiying.pojo.User;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;public class MyTest {public static void main(String[] args) {ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");User user = (User) context.getBean("hehe");User user1 = (User) context.getBean("me");User user2 = (User) context.getBean("aa");user.show();user1.show();user2.show();}
}

在测试类中只需加载applicationContext就可以把所有的配置加载,因为在此导入了其他配置文件

Spring配置介绍相关推荐

  1. spring事务介绍

    一.spring事务介绍 spring事务优点 对不同的api进行统一编程模型,如JTA,JDBC,Hibernate,JPA,JDO... 支持声明式事务 简化编程式事务api 对spring数据层 ...

  2. Spring入门介绍:

    Spring入门介绍 Spring诞生: 创建Spring的目的就是用来替代更加重量级的的企业级Java技术 简化Java的开发 基于POJO轻量级和最小侵入式开发 通过依赖注入和面向接口实现松耦合 ...

  3. Spring整合Mongodb,Maven的依赖,Spring配置,MongoDB的公共操作类,使用SpringMVC的Controller进行测试并返回结果的案例

    在和Spring和MongoDB进行整合的时候需要如下三个jar,分别是: spring-data-commons spring-data-mongodb mongo-java-driver 下面讲解 ...

  4. Spring之旅—Spring模块介绍

    1.0  Spring模块介绍 核心容器(Spring Core) 核心容器提供Spring框架的基本功能.Spring以bean的方式组织和管理Java应用中的各个组件及其关系.Spring使用Be ...

  5. spring 配置只读事务_只读副本和Spring Data第3部分:配置两个实体管理器

    spring 配置只读事务 我们之前的设置可以正常工作. 我们现在要做的是进一步发展,并配置两个单独的实体管理器,而不会影响我们之前实现的功能. 第一步是将默认实体管理器配置设置为主要配置. 这是第一 ...

  6. spring配置xml文件_XML配置文件中的Spring配置文件

    spring配置xml文件 我的上一个博客非常简单,因为它涵盖了我从Spring 3.0.x到Spring 3.1.x的轻松升级,最后我提到可以将Spring模式升级到3.1,以利用Spring的最新 ...

  7. shiro框架---shiro配置介绍(一)

    接上一篇文章shiro框架-通过系统介绍shiro框架中的实现逻辑   项目已分享到GitHub上,如果需要的可以看下,springboot+shiro项目Git下载地址. shiro在springb ...

  8. c3p0详细配置介绍

    C3P0是一个开放源代码的JDBC连接池,它在lib目录中与Hibernate一起发布,包括了实现jdbc3和jdbc2扩展规范说明的Connection 和Statement 池的DataSourc ...

  9. DI的概念和实现原理—Spring系列介绍

    DI的概念和实现原理-Spring系列介绍 DI和AOP是Spring中的两个核心概念,要学习DI和AOP,首先就需要了解清楚什么是DI,什么是AOP,这篇文章会讲解一下DI的概念和实现原理,不足之处 ...

最新文章

  1. 山东计算机基础模拟题及答案,2016山东农信社考试模拟题--计算机基础知识答案(1)...
  2. ASP.NET XML读取、增加、修改和删除操作
  3. [RHEL5企业级Linux服务攻略]--第9季 Squid服务全攻略之高级配置
  4. linux脚本执行进度条,shell脚本实现进度条
  5. 监控 SQL Server 的运行状况
  6. 网页交互动画终极指南
  7. Nacos源码发送心跳
  8. windows server 2008更新补丁失败排错
  9. l380废墨收集垫已到使用寿命_湖北雨水收集系统定制
  10. 我的世界基岩版json_我的世界基岩版app_我的世界基岩版app下载_我的世界基岩版安卓版下载-新手游网...
  11. SourcesTree使用手册2:文件更新
  12. Mongodb 3.2 Manual阅读笔记:CH9 存储
  13. iphone修改app名称_ios软件如何改名字 苹果手机怎么修改软件的图标名称呢
  14. 支付宝小程序开发笔记
  15. 电商网站建设步骤_电商网站建设的注意事项_OctShop
  16. NVIDIA vid2vid论文复现
  17. C++禁止键盘和鼠标事件
  18. Acrobat Pro DC 教程:了解 Acrobat Pro DC 界面
  19. android会超过苹果,任正非:超过苹果和安卓的华为操作系统,不会超过三百年...
  20. Erase/Trim/Discard/Sanitize

热门文章

  1. c++ 函数中定义函数
  2. PHP linux spl_autoload_register区分大小写
  3. sqlserver中的分页sql语句,不同于mysql中的limit,相当于top+top
  4. Eclipse无法设置NDK路径的解决方法
  5. 【Java】使用MapReduce程序统计UV数量
  6. Java——类和对象
  7. 01-03 Linux常用命令-文本处理
  8. 最详细,快速入门Web前端开发的正确姿势
  9. 7-4 sdut-运输计费问题 (10 分)python
  10. python的pip换源_[Python]Pip换源以及设置代理