spring 中各个配置文件的说明

本文详细介绍了如何使用Maven管理项目依赖,并通过spring.xml配置文件实现Spring框架的对象实例化。从pom.xml的配置到Spring Bean的定义,再到通过ClassPathXmlApplicationContext加载配置并调用方法,全面展示了Maven与Spring的整合过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

(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");

运行结果为:

 

转载于:https://www.cnblogs.com/WQX-work24/p/9920644.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值