1.首先我们来创建一个HelloWorld类,通过Spring来对这个类进行实例化

 package com.wzy.lesson1;

 /**
* @author wzy
* @version 1.0
* @date 2019/5/5 23:46
*/
public class HelloWorld {
private String name; public void setName(String name) {
System.out.println("setName : " + name);
this.name = name;
} public void hello() {
System.out.println("Hello " + name);
} public HelloWorld() {
System.out.println("HelloWorlds Constructor...");
}
}

2.之后在项目的类路径下,我是在resources文件夹下创建

spring.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"> <!--配置Bean-->
<bean id="helloWorld" class="com.wzy.lesson1.HelloWorld">
<!--通过setName方法注入-->
<property name="name" value="Spring"/>
</bean>
</beans>

3.编写测试类创建HelloWorld类实例

 private static void testHelloWorld2() {
//1.创建Spring的IOC容器对象
ApplicationContext ctx = new ClassPathXmlApplicationContext("spring.xml"); //2.从IOC容器中获取Bean实例
// HelloWorld helloWorld = (HelloWorld) ctx.getBean("helloWorld");
//用类型获取,但是如果由多个接口的实现类,那么容器就不知道应该为我们返回哪个实例
HelloWorld helloWorld = ctx.getBean(HelloWorld.class); //3.调用hello方法
helloWorld.hello();
}

这里使用Spring的IOC容器ApplicationContext的实现类ClassPathXmlApplicationContext对bean进行获取,这里获取bean的方式有两种:

第一种:通过bean的id进行获取

 HelloWorld helloWorld = (HelloWorld) ctx.getBean("helloWorld");

第二种:通过bean的class进行获取

 HelloWorld helloWorld = ctx.getBean(HelloWorld.class);

推荐使用第一种,因为在使用类名.class方式时,如果配置了两个HelloWorld类的bean那么,Spring容器将会抛出异常,因为容器无法确定你要获取的是哪一个bean,所以,如果要使用第二种的话,那么就要保证配置文件中只配置了一个HelloWorld类的bean,如果存在多个就会抛出一下异常:

测试结果:成功创建HelloWorld类的实例

Spring基础02——Spring HelloWorld的更多相关文章

  1. Spring基础系列-Spring事务不生效的问题与循环依赖问题

    原创作品,可以转载,但是请标注出处地址:https://www.cnblogs.com/V1haoge/p/9476550.html 一.提出问题 不知道你是否遇到过这样的情况,在ssm框架中开发we ...

  2. Spring基础——在Spring Config 文件中配置 Bean

    一.基于 XML 的 Bean 的配置——通过全类名(反射) <bean <!-- id: bean 的名称在IOC容器内必须是唯一的若没有指定,则自动的将全限定类名作为 改 bean 的 ...

  3. Spring基础篇——Spring的AOP切面编程

    一  基本理解 AOP,面向切面编程,作为Spring的核心思想之一,度娘上有太多的教程啊.解释啊,但博主还是要自己按照自己的思路和理解再来阐释一下.原因很简单,别人的思想终究是别人的,自己的理解才是 ...

  4. Spring基础05——Spring依赖注入的三种方式

    Spring支持3种依赖注入的方式:属性注入.构造器注入.工厂 1.属性注入 属性注入即通过setter方法注入Bean的属性或依赖的对象.使用<property>元素,使用name属性指 ...

  5. Spring基础—— 在 Spring Config 中使用外部属性文件

    一.在 Spring Config 文件中配置 Bean 时,有时候需要在 Bean 的配置里添加 系统部署的细节信息, 如文件路径,数据源配置信息.而这些部署细节实际上需要在配置文件外部来定义. 二 ...

  6. Spring基础——在 Spring Config 文件中基于 XML 的 Bean 的自动装配

    一.Spring IOC 容器支持自动装配 Bean,所谓自动装配是指,不需要通过 <property> 或 <constructor-arg> 为 Bean 的属性注入值的过 ...

  7. Spring基础篇——Spring容器和应用上下文理解

    上文说到,有了Spring之后,通过依赖注入的方式,我们的业务代码不用自己管理关联对象的生命周期.业务代码只需要按照业务本身的流程,走啊走啊,走到哪里,需要另外的对象来协助了,就给Spring说,我想 ...

  8. 【spring基础】spring声明式事务详解

    一.spring声明式事务 1.1 spring的事务管理器 spring没有直接管理事务,而是将管理事务的责任委托给JTA或相应的持久性机制所提供的某个特定平台的事务实现.spring容器负责事物的 ...

  9. 【spring基础】spring与jdbc整合详解

    先上一段简单示例 public class MyTemplate { private DataSource dataSource; public DataSource getDataSource() ...

随机推荐

  1. java list去重方式,以及效率问题

    之前面试被问到关于java如何去重的问题,当时没怎么留意,今天刚好项目中用到了,所以记录一下. 实体类: /** * 用户类 */ class User{ private String usernam ...

  2. nowcoder---常州大学新生寒假训练会试----F 大佬的生日礼包(二分)

    链接:https://www.nowcoder.net/acm/contest/78/F 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32768K,其他语言65536K 64bit ...

  3. shell命令别名

    ~/.bashrc文件 [root@linuxzgf ~]# vi ~/.bashrc            在alias cp='cp -i'前加上"#"注释,重新登录即可实现复 ...

  4. AndroidStudio设置SVN忽略文件

    方法一: 在SVN中进行设置: 在空白处右键单击,选择TortoiseSVN -> Settings ->General:在General界面找到Global ignore pattern ...

  5. CentOS 6.5系统使用yum方式安装LAMP环境和phpMyAdmin,mysql8.0.1/mysql5.7.22+centos7,windows mysql安装、配置

    介绍如何在CentOs6.2下面使用YUM配置安装LAMP环境,一些兄弟也很喜欢使用编译的安装方法,个人觉得如果不是对服务器做定制,用yum安装稳定简单,何必去download&make&am ...

  6. 二、Jmter查看结果数只能显示有限的数据,查看全部数据

    1.打开jmeter安装目录,找到bin目录下jmeter.properties文件 2.搜索:view.results.tree.max_size=10485760 3.将#号去掉,重启jmeter

  7. ssd写入量剩余读写次数怎么查

    固态硬盘ssd写入量剩余读写次数怎么查 为什么要查固态硬盘的写入量呢,主要是因为闪存是有写入次数限制的,所以查次数就是看看寿命还有多少,说白了这是对耐久度的一点担忧.其实目前原厂出品的固态硬盘,即便是 ...

  8. K8S 笔记,请坚持

    service 通过 selector 来选择指定 label 的 pod. 如何访问 service 呢?集群内部访问,使用 cluster ip:集群外部访问,使用 NodePort. clust ...

  9. Python Module_os_操作系统

    目录 目录 前言 软件环境 os模块内建属性 osname 获取执行平台的类型 oslinesep 输出当前平台使用的行终止符 ossep 输出操作系统特定的路径分隔符 ospathsep 输出用于分 ...

  10. App测试工具选择

    一.功能测试自动化 a) 轻量接口自动化测试: jmeter, b) APP UI层面的自动化 android:UI Automator Viewer,Android Junit,Instrument ...