(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. xterm.js的深入学习

    demo <template> <div id="app" class="app-box">Hello</div> < ...

  2. ret/retn人为改变执行地址

    1.CALL和RET/RETN是一对指令,CALL把返回地址压入堆栈,RET/RETN把返回地址从堆栈取出,然后将IP寄存器改为该返回地址.  2.不使用CALL,而是人为地把地址放入堆栈即可实现.如 ...

  3. ORM:Chloe

    ORM的一种:Chloe注意实体类模板特色:多表连接 利用chloe实现对各表的增删查改的管理,判断现有物料是否能够支持生产规模. 一开始报错: IQuery<ProductionPlans&g ...

  4. perl升级后shutter程序无法运行的修复

    这个问题真的很无语,perl升级后一些依赖perl的aur包不能用了,需要rebuild,这样让系统稳定性何在-- 运行shutter错误信息: $ shutter Can't locate Gnom ...

  5. Ubuntu命令行操作

    一.文件/文件夹管理 ls 列出当前目录文件(不包括隐含文件) ls -a 列出当前目录文件(包括隐含文件) ls -l 列出当前目录下文件的详细信息 cd .. 回当前目录的上一级目录 cd - 回 ...

  6. Kaggle数据集下载

    Kaggle数据集下载步骤: 安装Kaggle库: 注册Kaggle账户: 找到数据集,接受rules: 在My Account>>API中,点击Create New API Token, ...

  7. ubuntu下oracle 数据库安装

    环境:腾讯云 一. 由于腾讯云直接下载oracle太慢,先安装docker 1.sudo apt update 2.接下来,使用apt安装一些允许通过HTTPS才能使用的软件包: sudo apt i ...

  8. spring boot poi 导出Excel

    public class ExcelData implements Serializable { private static final long serialVersionUID = 444401 ...

  9. spring Data solr依赖文件 和xml配置文件

    <dependencies>        <dependency>            <groupId>org.springframework.data< ...

  10. 【JVM】内存区域

    程序运行时,有六个地方都可以保存数据: 1. 寄存器:这是最快的保存区域,因为它位于和其他所有保存方式不同的地方:处理器内部.然而,寄存器的数量十分有限,所以寄存器是根据需要由编译器分配.我们对此没有 ...