对Spring Ioc的理解
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的理解的更多相关文章
- 谈谈对Spring IOC的理解(转)
学习过Spring框架的人一定都会听过Spring的IoC(控制反转) .DI(依赖注入)这两个概念,对于初学Spring的人来说,总觉得IoC .DI这两个概念是模糊不清的,是很难理解的,今天和大家 ...
- 谈谈对Spring IOC的理解
学习过Spring框架的人一定都会听过Spring的IoC(控制反转) .DI(依赖注入)这两个概念,对于初学Spring的人来说,总觉得IoC .DI这两个概念是模糊不清的,是很难理解的,今天和大家 ...
- Spring系列之谈谈对Spring IOC的理解
学习过Spring框架的人一定都会听过Spring的IoC(控制反转) .DI(依赖注入)这两个概念,对于初学Spring的人来说,总觉得IOC .DI这两个概念是模糊不清的,是很难理解的,今天和大家 ...
- 谈谈对Spring IOC的理解【转】
学习过Spring框架的人 一定都会听过Spring的IoC(控制反转) .DI(依赖注入)这两个概念,对于初学Spring的人来说,总觉得IoC .DI这两个概念是模糊不清的,是很难理解的,今天和大 ...
- Spring IOC的理解
学习过Spring框架的人一定都会听过Spring的IoC(控制反转) .DI(依赖注入)这两个概念,对于初学Spring的人来说,总觉得IoC .DI这两个概念是模糊不清的,是很难理 ...
- 谈谈对Spring IOC的理解(转载)
学习过Spring框架的人一定都会听过Spring的IoC(控制反转) .DI(依赖注入)这两个概念,对于初学Spring的人来说,总觉得IoC .DI这两个概念是模糊不清的,是很难理解的,今天和大家 ...
- 谈谈对Spring IOC的理解(转发)
学习过Spring框架的人一定都会听过Spring的IoC(控制反转) .DI(依赖注入)这两个概念,对于初学Spring的人来说,总觉得IoC .DI这两个概念是模糊不清的,是很难理解的,今天和大家 ...
- 谈谈对Spring IOC的理解(转载)
学习过Spring框架的人一定都会听过Spring的IoC(控制反转) .DI(依赖注入)这两个概念,对于初学Spring的人来说,总觉得IoC .DI这两个概念是模糊不清的,是很难理解的,今天和大家 ...
- Spring学习总结(一)---谈谈对Spring IOC的理解(一:理论知识理解)
学习过Spring框架的人一定都会听过Spring的IoC(控制反转) .DI(依赖注入)这两个概念,对于初学Spring的人来说,总觉得IoC .DI这两个概念是模糊不清的,是很难理解的,今天和大家 ...
- spring ioc DI 理解
下面是我从网上找来的一些大牛对spring ioc和DI的理解,希望也能让你对Spring ioc和DI的设计思想有更进一步的认识. 一.分享Iteye的开涛对Ioc的精彩讲解 Ioc—Inversi ...
随机推荐
- snprintf笔记
在weibo上看到Laruence大神修复了一个使用snprintf的bug (http://t.cn/Rm6AuFh) 引起了TK教主的关注.TK教主着重提到了在windows下snprintf与_ ...
- Python mysql-python及pycurl使用一例
#环境:CentOS Linux release 7.5.1804 (Core) mini安装,使用python2.7 #使用pucurl对输入的url地址进行测试,将结果存放到mysql中,代码来之 ...
- React-redux深入理解
首先,一张 Redux 解释图镇楼: [回顾]Redux 的核心: store 是什么?(createStore 函数的实现) const store = createStore(reducer); ...
- CrawlSpider模板
crawlSpider 创建CrawlSpider模板 scrapy genspider -t crawl <爬虫名字> <域名> 模板代码示例: # -*- coding: ...
- backref 用法
源码 def backref(name, **kwargs): """Create a back reference with explicit keyword argu ...
- 高通平台获取secure boot,串号等状态
adb shell下 运行./system/bin/r address 其中address对应各个flag参数的地址,具体如下: 无法打开/dev/mem节点(没有该节点),这时只需在内核配置中选上C ...
- selenium_Python3_邮箱登录:动态元素定位
这里的关键是动态frame定位: 其他元素定位不用多说,常规操作. 不过需要注意加上这个: from selenium.webdriver.remote.webelement import WebEl ...
- C语言入门(1)
开始学习C语言 第一个C语言程序 #include<stdio.h> int main() { printf("Hello World!"); } C程序结构 1. 头 ...
- Java实现打印日历的功能
编写一个程序,显示给定年月的日历.程序提示用户输入年份和月份,然后显示该月的整个日历. 代码: import java.util.Scanner; public class PrintCalendar ...
- Ubuntu中,wxpython的TextCtrl引发的error:_pixman_log_error
1>在windows下,执行下面的代码,不会报错,但是在Ubuntu下,就会报错 2>在Ubuntu下执行上面的代码,会提示下面的错. 解决方法: 若是TextCtrl中不写wx.TE_M ...