(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 中各个配置文件的说明的更多相关文章

  1. 解决spring中不同配置文件中存在name或者id相同的bean可能引起的问题

    小总结: 如果启用组件扫描,bean名称不同时,Spring将尝试创建一个bean,即使该类的bean已经在spring-config.xml中定义了. 但是,如果在spring配置文件中定义的bea ...

  2. Spring中多配置文件以及寻觅引用其他bean的方式

    Spring多配置文件有什么好处? 按照目的.功能去拆分配置文件,可以提高配置文件的可读性与维护性,如将配置事务管理.数据源等少改动的配置与配置bean单独分开. Spring读取配置文件的几种方式: ...

  3. Spring中的配置文件文件位置

    在Java开发中,一般在Spring框架中,classpath的位置是指src下,在IDEA中一般是指resource中 配置文件 位置:任意,开发中一般在classpath下(src) 名称:任意, ...

  4. Spring 中出现相同名称的 bean 的处理机制

    小总结: 如果启用组件扫描,bean名称不同时,Spring将尝试创建一个bean,即使该类的bean已经在spring-config.xml中定义了. 但是,如果在spring配置文件中定义的bea ...

  5. Spring集成Mybatis配置文件的简单理解

    详情可见官方文档http://www.mybatis.org/spring/zh/index.html 一.需要配置的对象实例 1.SqlSessionFactoryBean 在 MyBatis-Sp ...

  6. Spring中加载xml配置文件的六种方式

    Spring中加载xml配置文件的六种方式 博客分类: Spring&EJB XMLSpringWebBeanBlog  因为目前正在从事一个项目,项目中一个需求就是所有的功能都是插件的形式装 ...

  7. Spring中加载配置文件的方式

    原文:http://blog.csdn.net/snowjlz/article/details/8158560 Spring 中加载XML配置文件的方式,好像有3种, XML是最常见的Spring 应 ...

  8. Spring中引入其他配置文件

    一.引入其他 模块XML 在Spring的配置文件,有时候为了分模块的更加清晰的进行相关实体类的配置. 比如现在有一个job-timer.xml的配置 <?xml version="1 ...

  9. Spring中 <context:property-placeholder 的使用与解析 .properties 配置文件的加载

    转: Spring中property-placeholder的使用与解析 Spring中property-placeholder的使用与解析 我们在基于spring开发应用的时候,一般都会将数据库的配 ...

随机推荐

  1. vue基础学习一

    写一个例子,告诉你VUE的方便之处,就是双向绑定,不需要操作DOM对象,而是操作数据 div中msg 和JS中msg是一一对应的 然后看浏览器中 然后如果我想改变浏览器中值,我在console这么操作 ...

  2. C#实体类get和set的作用

    一,实体类属性访问存在两种写法: //第一种写法 private int _id; public int Id { set { _id = value; } get { return _id; } } ...

  3. C#@字符的使用

    一,在字符串中的使用 //当在字符串前面加上一个@字符的时候,我们就可以把一个字符串定义在多行 // 编译器不会再去识别字符串中的转义字符 // 如果需要在字符串中表示一个双引号的话,需要使用两个双引 ...

  4. elasticsearch 关联单词查询以及Shingles

    Shingle Token Filter A token filter of type shingle that constructs shingles (token n-grams) from a ...

  5. 第11篇Kubernetes部署微服务电商平台

        kubernetes部署sock-shop微服务电商平台: 准备条件   确保kubernetes可以访问:reg.yunwei.edu镜像库   需要准备镜像:       部署微服务   ...

  6. 程序猿必备的10款web前端动画插件

    1.基于jQuery的瀑布流图片筛选插件 瀑布流的展现方式在目前的网页中用得越来越广泛,特别是图片和首页文章的动态加载. 今天分享的这款就是基于jQuery的瀑布流图片筛选插件,我们可以点击图片分类名 ...

  7. mocha.js

    mocha 如果你听说过“测试驱动开发”(TDD:Test-Driven Development),单元测试就不陌生. 单元测试是用来对一个模块.一个函数或者一个类来进行正确性检验的测试工作. 比如对 ...

  8. Spring整合Struts2的两种方式

    https://blog.csdn.net/cuiyaoqiang/article/details/51887594

  9. java--substring内存溢出问题

    public class SubStringDemo { //substring() /** * jdk6 当调用 substring() 方法时,创建了一个新的String对象,但是string的v ...

  10. MySQL-事件总结

    是什么?事件是一组SQL集合,简单说就是mysql中的定时器,时间到了就执行. 一:查询事件变量,如果查询不到变量,说明数据库版本过低,不支持事件. SHOW VARIABLES LIKE 'even ...