spring 中各个配置文件的说明
(1)pom.xml
pom.xml文件是在整个项目下面,该xml的主要作用是:导入框架的jar包及其所依赖的jar包,导入的jar包是写在<dependencies></dependencies>中
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>oracle.MavenPro</groupId>
<artifactId>MyFirstPro</artifactId>
<version>0.0.1-SNAPSHOT</version>
<dependencies>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-core -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>5.1.1.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.1.1.RELEASE</version>
</dependency> </dependencies>
</project>
(2)spring.xml
spring.xml是通过spring框架来创建对象,而不需要在类中创建对象。
下面通过一个简单的sayhello例子来说明:
定义一个接口 ISayHello
package com.service;
public interface ISayHello {
public void sayHello();
}
定义 接口ISayHello 的实现类 SayHelloImple
package com.service.imple;
import com.service.ISayHello;
public class SayHelloImple implements ISayHello{
@Override
public void sayHello() {
System.out.println("hello!!!");
}
}
通过spring.xml来实例化对象,其中class属性是实现类的全路径,id是实例化对象的别名(hello)。
<?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/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<bean id="hello" class="com.service.imple.SayHelloImple"></bean>
</beans>
其中下面这段代码是告诉spring,通过什么编码来解析<bean>

新建一个test类
package com.service; import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; import com.service.imple.SayHelloImple; public class Test {
public static void main(String[] args) {
ApplicationContext context =
new ClassPathXmlApplicationContext("spring.xml"); SayHelloImple sayhello =
(SayHelloImple) context.getBean("hello");
sayhello.sayHello(); } }
下面这段代码是加载 spring.xml 文件
ApplicationContext context =
new ClassPathXmlApplicationContext("spring.xml");
下面代码是获取实例化的对象hello
SayHelloImple sayhello =
(SayHelloImple) context.getBean("hello");
运行结果为:

spring 中各个配置文件的说明的更多相关文章
- 解决spring中不同配置文件中存在name或者id相同的bean可能引起的问题
小总结: 如果启用组件扫描,bean名称不同时,Spring将尝试创建一个bean,即使该类的bean已经在spring-config.xml中定义了. 但是,如果在spring配置文件中定义的bea ...
- Spring中多配置文件以及寻觅引用其他bean的方式
Spring多配置文件有什么好处? 按照目的.功能去拆分配置文件,可以提高配置文件的可读性与维护性,如将配置事务管理.数据源等少改动的配置与配置bean单独分开. Spring读取配置文件的几种方式: ...
- Spring中的配置文件文件位置
在Java开发中,一般在Spring框架中,classpath的位置是指src下,在IDEA中一般是指resource中 配置文件 位置:任意,开发中一般在classpath下(src) 名称:任意, ...
- Spring 中出现相同名称的 bean 的处理机制
小总结: 如果启用组件扫描,bean名称不同时,Spring将尝试创建一个bean,即使该类的bean已经在spring-config.xml中定义了. 但是,如果在spring配置文件中定义的bea ...
- Spring集成Mybatis配置文件的简单理解
详情可见官方文档http://www.mybatis.org/spring/zh/index.html 一.需要配置的对象实例 1.SqlSessionFactoryBean 在 MyBatis-Sp ...
- Spring中加载xml配置文件的六种方式
Spring中加载xml配置文件的六种方式 博客分类: Spring&EJB XMLSpringWebBeanBlog 因为目前正在从事一个项目,项目中一个需求就是所有的功能都是插件的形式装 ...
- Spring中加载配置文件的方式
原文:http://blog.csdn.net/snowjlz/article/details/8158560 Spring 中加载XML配置文件的方式,好像有3种, XML是最常见的Spring 应 ...
- Spring中引入其他配置文件
一.引入其他 模块XML 在Spring的配置文件,有时候为了分模块的更加清晰的进行相关实体类的配置. 比如现在有一个job-timer.xml的配置 <?xml version="1 ...
- Spring中 <context:property-placeholder 的使用与解析 .properties 配置文件的加载
转: Spring中property-placeholder的使用与解析 Spring中property-placeholder的使用与解析 我们在基于spring开发应用的时候,一般都会将数据库的配 ...
随机推荐
- BZOJ 2724蒲公英 (分块) 【内有块大小证明】
题面 luogu传送门 分析 先分块,设块大小为x(之后我们会证明块大小取何值会更优) 步骤1 把所有的数离散化,然后对每个值开一个vector pos[i],pos[i]存储数i出现的位置 我们设查 ...
- 【置顶】CSP/S 2019退役祭
标题没错,今年就是我的最后一年了. 才高一啊,真不甘心啊. DAY1(之前的看前几篇博客吧) T1 现在没挂 T2 貌似是树形DP,跑到80000的深度时挂了,于是特判了链的情况,大样例过了,现在没挂 ...
- ZOJ 3681E - Cup 2(记忆化dfs)不好读
版权声明:本文为博主原创文章.未经博主同意不得转载. https://blog.csdn.net/opm777/article/details/25726221 E - Cup 2 Time Limi ...
- 十五、API请求接口-远程服务器返回错误: (400) 错误的请求错误
一.远程服务器返回错误: (400) 错误的请求错误 捕获异常查看具体错误 using Newtonsoft.Json; using System; using System.Collections. ...
- 三、TortoiseSVN 单独拉取项目某个文件
一.项目拉取后,单独对某个文件拉取 实践中会出现这样的问题,在svn 中 我项目 ,已经拉取了,但是 某个文件改乱了 ,想从新对某个文件拉取 . 解决方案:1.删除某个文件,从新更新获取-右击该文件上 ...
- 2019牛客暑期多校训练营(第三场)I Median
题意:给出n-2的中位数序列b,b[i]代表原序列中(a[i],a[i+1],a[i+2])的中位数,求a. 解法:比赛的时候没做出来,赛后看题解的.解法跟网上各位大佬一样:首先要证明其实原序列a中的 ...
- python3.x filter,map,reduce浅析
#map用法: #传递函数api进入map去执行,把字符串第一个字母变大写, #其他变小写返回 def format_name(s): s=s.lower() print(s) return s[0] ...
- 1.什么是微信小程序
微信小程序,简称CX,是一种不需要下载安装即可使用的应用,它实现了应用“触手可及”的梦想,用户扫一扫或者搜一下即可打开应用.也体现了“用完即走”的理念,用户不用关心是否安装太多应用的问题. 应用将无处 ...
- Tomcat是否关闭 maxEntriesLocalHeap
EHCache does not allow attribute "maxEntriesLocalHeap". 这个错误是由于这个属性不支持2.5以下版本 故更新ehcache版本 ...
- v-bin:href 绑定链接
<div id="app06"> <a v-bind:href="URL">我是百度</a> </div> 调用 ...