1. 新建一个maven项目,工程目录如下

2. 添加接口MessageService及其实现类MessageServiceImpl

MessageService.java

package com.peppa.service;

public interface MessageService {
String getMessage();
}

MessageServiceImpl.java

package com.peppa.serviceImpl;

import com.peppa.service.MessageService;

public class MessageServiceImpl implements MessageService {
@Override
public String getMessage() {
return "hello world!";
}
}

pom文件中添加spring-context依赖

<!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.1.5.RELEASE</version>
</dependency>

在resources目录下添加application.xml文件,内容如下

<?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.xsd" default-autowire="byName">
<bean id="messageService" class="com.peppa.serviceImpl.MessageServiceImpl"></bean>
</beans>

添加Application.java,将MessageService从context中取出来,而不是使用 new MessageServiceImpl() 获取

package com;

import com.peppa.service.MessageService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; public class Application {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("classpath:application.xml");
System.out.println("start success");
MessageService messageService = context.getBean(MessageService.class);
System.out.println("will print hello world");
System.out.println(messageService.getMessage());
}
}

通过配置文件来启动Spring的ApplicationContext,也就是IoC的核心。ApplicationContext启动过程中,会负责创建实例Bean,往各个Bean中注入依赖等。

对Spring Ioc的理解的更多相关文章

  1. 谈谈对Spring IOC的理解(转)

    学习过Spring框架的人一定都会听过Spring的IoC(控制反转) .DI(依赖注入)这两个概念,对于初学Spring的人来说,总觉得IoC .DI这两个概念是模糊不清的,是很难理解的,今天和大家 ...

  2. 谈谈对Spring IOC的理解

    学习过Spring框架的人一定都会听过Spring的IoC(控制反转) .DI(依赖注入)这两个概念,对于初学Spring的人来说,总觉得IoC .DI这两个概念是模糊不清的,是很难理解的,今天和大家 ...

  3. Spring系列之谈谈对Spring IOC的理解

    学习过Spring框架的人一定都会听过Spring的IoC(控制反转) .DI(依赖注入)这两个概念,对于初学Spring的人来说,总觉得IOC .DI这两个概念是模糊不清的,是很难理解的,今天和大家 ...

  4. 谈谈对Spring IOC的理解【转】

    学习过Spring框架的人 一定都会听过Spring的IoC(控制反转) .DI(依赖注入)这两个概念,对于初学Spring的人来说,总觉得IoC .DI这两个概念是模糊不清的,是很难理解的,今天和大 ...

  5. Spring IOC的理解

            学习过Spring框架的人一定都会听过Spring的IoC(控制反转) .DI(依赖注入)这两个概念,对于初学Spring的人来说,总觉得IoC .DI这两个概念是模糊不清的,是很难理 ...

  6. 谈谈对Spring IOC的理解(转载)

    学习过Spring框架的人一定都会听过Spring的IoC(控制反转) .DI(依赖注入)这两个概念,对于初学Spring的人来说,总觉得IoC .DI这两个概念是模糊不清的,是很难理解的,今天和大家 ...

  7. 谈谈对Spring IOC的理解(转发)

    学习过Spring框架的人一定都会听过Spring的IoC(控制反转) .DI(依赖注入)这两个概念,对于初学Spring的人来说,总觉得IoC .DI这两个概念是模糊不清的,是很难理解的,今天和大家 ...

  8. 谈谈对Spring IOC的理解(转载)

    学习过Spring框架的人一定都会听过Spring的IoC(控制反转) .DI(依赖注入)这两个概念,对于初学Spring的人来说,总觉得IoC .DI这两个概念是模糊不清的,是很难理解的,今天和大家 ...

  9. Spring学习总结(一)---谈谈对Spring IOC的理解(一:理论知识理解)

    学习过Spring框架的人一定都会听过Spring的IoC(控制反转) .DI(依赖注入)这两个概念,对于初学Spring的人来说,总觉得IoC .DI这两个概念是模糊不清的,是很难理解的,今天和大家 ...

  10. spring ioc DI 理解

    下面是我从网上找来的一些大牛对spring ioc和DI的理解,希望也能让你对Spring ioc和DI的设计思想有更进一步的认识. 一.分享Iteye的开涛对Ioc的精彩讲解 Ioc—Inversi ...

随机推荐

  1. 20155219付颖卓《网络对抗》MSF基础应用实验

    实验后回答问题 1.用自己的话解释什么是exploit,payload,encode. exploit就是进行攻击的那一步 payload是在目标主机上创建会话连接的 encode是对payload进 ...

  2. docker hub切换国内镜像

    官方docker hub访问非常的慢,安装之后最好先切换国内镜像: 执行: curl -sSL https://get.daocloud.io/daotools/set_mirror.sh | sh ...

  3. ERROR: cannot launch node of type [turtlebot_teleop/turtlebot_teleop_key] 问题解决

    当遇到问题

  4. vue state

    vuex单一状态树,直接地定位任一特定的状态片段. vuex状态存储响应式,唯一store实例,从store中读取状态: 1.在计算属性中返回某个状态 2.在根实例中注册store选项,该 store ...

  5. 软件开发者路线图梗概&书摘chapter1

    软件技能的概念 本书目的:尝试为软件开发的新颖方法整理出一份宣言 原因:定义泛化 从敏捷开发运动中学到的经验:理解支撑规则的底层因素 软件技能的愿景:价值的提取&希望的表达 价值体系: 1.基 ...

  6. 使用OpenLDAP部署目录服务

  7. 猴子分桃—Python

    def f(): for i in range(3120,4000): flag = 1 k=i for j in range(5): if i%5==1: i=(i//5)*4 else: flag ...

  8. AJAX实现注册

    先添加点击事件: <input type="button" id="submittt" value="注册"/> 展示提示消息: ...

  9. 创建一个dynamics 365 CRM online plugin (六) - Delete plugin from CRM

    我们之前都学习到怎么添加,debug还有update plugin. 今天带大家过一下怎么从CRM instance当中删除plugin. 首先让我们打开Settings -> Customiz ...

  10. eclipse项目改为maven项目导致svn无法比较历史数据的解决办法

    这个问题没有找到合适的答案,最终自己想出了一个解决方案,在此记录下. 问题描述 在将老的eclipse项目重构为maven项目时,我这边是新建了一个maven项目,然后将对应的数据分别放到相应的位置, ...