导入Jar包

注 : spring 需要导入commons-logging进行日志记录 . 我们利用maven , 他会自动下载对应的依赖项 .

  1. <dependency>
  2. <groupId>org.springframework</groupId>
  3. <artifactId>spring-webmvc</artifactId>
  4. <version>5.1.10.RELEASE</version>
  5. </dependency>

编写代码

1、编写一个Hello实体类

  1. public class Hello {
  2. private String name;
  3. public String getName() {
  4. return name;
  5. }
  6. public void setName(String name) {
  7. this.name = name;
  8. }
  9. public void show(){
  10. System.out.println("Hello,"+ name );
  11. }
  12. }

2、编写我们的spring文件 , 这里我们命名为beans.xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation="http://www.springframework.org/schema/beans
  5. http://www.springframework.org/schema/beans/spring-beans.xsd">
  6. <!--bean就是java对象 , 由Spring创建和管理-->
  7. <bean id="hello" class="com.kuang.pojo.Hello">
  8. <property name="name" value="Spring"/>
  9. </bean>
  10. </beans>

3、我们可以去进行测试了 .

  1. @Test
  2. public void test(){
  3. //解析beans.xml文件 , 生成管理相应的Bean对象
  4. ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
  5. //getBean : 参数即为spring配置文件中bean的id .
  6. Hello hello = (Hello) context.getBean("hello");
  7. hello.show();
  8. }

思考

  • Hello 对象是谁创建的 ? 【hello 对象是由Spring创建的
  • Hello 对象的属性是怎么设置的 ? hello 对象的属性是由Spring容器设置的

这个过程就叫控制反转 :

  • 控制 : 谁来控制对象的创建 , 传统应用程序的对象是由程序本身控制创建的 , 使用Spring后 , 对象是由Spring来创建的
  • 反转 : 程序本身不创建对象 , 而变成被动的接收对象 .

依赖注入 : 就是利用set方法来进行注入的.

IOC是一种编程思想,由主动的编程变成被动的接收

可以通过newClassPathXmlApplicationContext去浏览一下底层源码 .

2、Spring教程之HelloSpring的更多相关文章

  1. 9、Spring教程之AOP

    那我们接下来就来聊聊AOP吧! 1.什么是AOP AOP(Aspect Oriented Programming)意为:面向切面编程,通过预编译方式和运行期动态代理实现程序功能的统一维护的一种技术.A ...

  2. 4、Spring教程之Spring配置

    别名 alias 设置别名 , 为bean设置别名 , 可以设置多个别名 <!--设置别名:在获取Bean的时候可以使用别名获取--> <alias name="userT ...

  3. 1、Spring教程之Spring概述

    1.Spring概述 简介 Spring : 春天 --->给软件行业带来了春天 2002年,Rod Jahnson首次推出了Spring框架雏形interface21框架. 2004年3月24 ...

  4. 3、Spring教程之IOC创建对象方式

    1.通过无参构造方法来创建 1.User.java public class User { private String name; public User() { System.out.printl ...

  5. Spring 系列教程之 bean 的加载

    Spring 系列教程之 bean 的加载 经过前面的分析,我们终于结束了对 XML 配置文件的解析,接下来将会面临更大的挑战,就是对 bean 加载的探索.bean 加载的功能实现远比 bean 的 ...

  6. RabbitMQ基础教程之Spring&JavaConfig使用篇

    RabbitMQ基础教程之Spring使用篇 相关博文,推荐查看: RabbitMq基础教程之安装与测试 RabbitMq基础教程之基本概念 RabbitMQ基础教程之基本使用篇 RabbitMQ基础 ...

  7. SpringBoot系列教程之Bean加载顺序之错误使用姿势辟谣

    在网上查询 Bean 的加载顺序时,看到了大量的文章中使用@Order注解的方式来控制 bean 的加载顺序,不知道写这些的博文的同学自己有没有实际的验证过,本文希望通过指出这些错误的使用姿势,让观文 ...

  8. SpringBoot系列教程之Bean之指定初始化顺序的若干姿势

    上一篇博文介绍了@Order注解的常见错误理解,它并不能指定 bean 的加载顺序,那么问题来了,如果我需要指定 bean 的加载顺序,那应该怎么办呢? 本文将介绍几种可行的方式来控制 bean 之间 ...

  9. springboot入门系列(三):SpringBoot教程之RabbitMQ示例

    SpringBoot教程之RabbitMQ示例 SpringBoot框架已经提供了RabbitMQ的使用jar包,开发人员在使用RabbitMQ的时候只需要引用jar包简单的配置一下就可以使用Rabb ...

随机推荐

  1. React PureComponent All In One

    React PureComponent All In One import React, { // useState, // useEffect, // Component, PureComponen ...

  2. WoT

    WoT IoT / AIoT Web of Things (WoT) Architecture W3C Recommendation 9 April 2020 https://www.w3.org/T ...

  3. website & blogs & about me & contact

    website & blogs & about me & contact demos https://davidwalsh.name/about-david-walsh htt ...

  4. macOS & Xnip

    macOS & Xnip close finished notation cancel checked xgqfrms 2012-2020 www.cnblogs.com 发布文章使用:只允许 ...

  5. Linux & SIGUSER1

    Linux & SIGUSER1 https://stackoverflow.com/questions/10824886/how-to-signal-an-application-witho ...

  6. linux bash shell & lsof & grep & ps

    linux bash shell & lsof & grep & ps lsof list all open files # lsof & grep $ lsof -P ...

  7. 07_MySQL修改数据库的表结构

    修改数据库的表结构

  8. Linux关机指令详解

    Linux关机指令 在linux领域内大多用在服务器上,很少遇到关机的操作.毕竟服务器上跑一个服务是永无止境的,除非特殊情况下,不得已才会关机. 正确的关机流程为:sync > shutdown ...

  9. 判断app是否安装

    Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS); intent.setData(Uri.fromPar ...

  10. Linux 切换 shell

    查看当前已安装的shell [root@CentOSv64 my]# cat /etc/shells /bin/sh /bin/bash /sbin/nologin /bin/dash /bin/tc ...