Spring基础02——Spring HelloWorld
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的更多相关文章
- Spring基础系列-Spring事务不生效的问题与循环依赖问题
原创作品,可以转载,但是请标注出处地址:https://www.cnblogs.com/V1haoge/p/9476550.html 一.提出问题 不知道你是否遇到过这样的情况,在ssm框架中开发we ...
- Spring基础——在Spring Config 文件中配置 Bean
一.基于 XML 的 Bean 的配置——通过全类名(反射) <bean <!-- id: bean 的名称在IOC容器内必须是唯一的若没有指定,则自动的将全限定类名作为 改 bean 的 ...
- Spring基础篇——Spring的AOP切面编程
一 基本理解 AOP,面向切面编程,作为Spring的核心思想之一,度娘上有太多的教程啊.解释啊,但博主还是要自己按照自己的思路和理解再来阐释一下.原因很简单,别人的思想终究是别人的,自己的理解才是 ...
- Spring基础05——Spring依赖注入的三种方式
Spring支持3种依赖注入的方式:属性注入.构造器注入.工厂 1.属性注入 属性注入即通过setter方法注入Bean的属性或依赖的对象.使用<property>元素,使用name属性指 ...
- Spring基础—— 在 Spring Config 中使用外部属性文件
一.在 Spring Config 文件中配置 Bean 时,有时候需要在 Bean 的配置里添加 系统部署的细节信息, 如文件路径,数据源配置信息.而这些部署细节实际上需要在配置文件外部来定义. 二 ...
- Spring基础——在 Spring Config 文件中基于 XML 的 Bean 的自动装配
一.Spring IOC 容器支持自动装配 Bean,所谓自动装配是指,不需要通过 <property> 或 <constructor-arg> 为 Bean 的属性注入值的过 ...
- Spring基础篇——Spring容器和应用上下文理解
上文说到,有了Spring之后,通过依赖注入的方式,我们的业务代码不用自己管理关联对象的生命周期.业务代码只需要按照业务本身的流程,走啊走啊,走到哪里,需要另外的对象来协助了,就给Spring说,我想 ...
- 【spring基础】spring声明式事务详解
一.spring声明式事务 1.1 spring的事务管理器 spring没有直接管理事务,而是将管理事务的责任委托给JTA或相应的持久性机制所提供的某个特定平台的事务实现.spring容器负责事物的 ...
- 【spring基础】spring与jdbc整合详解
先上一段简单示例 public class MyTemplate { private DataSource dataSource; public DataSource getDataSource() ...
随机推荐
- [51nod1383&1048]整数分解为2的幂:DP
算法一 分析 \(f[x]=f[x-1]+f[x/2] \times [x \equiv 0 \mod 2],O(n)\) 代码 n=int(input()) f=[0]*(n+5) f[0]=1 m ...
- 大哥带的Orchel数据库的注入
0X01 先进行判断 a.jsp?username=SMITH and = 发现单引号闭合 我们尝试构造闭合 存在注入 a.jsp?username=SMITH'='1 正确 a.jsp?user ...
- HTTP访问控制(CORS)踩坑小记
前几天在帮后端排查一个cors的问题的时候发现的一些小坑特此记录 ** cors的本质是出于安全原因,浏览器限制从脚本内发起的跨源HTTP请求. 例如,XMLHttpRequest和FetchAPI遵 ...
- java跨越请求实例
使用Access-Control-Allow-Origin解决跨域 什么是跨域 当两个域具有相同的协议(如http), 相同的端口(如80),相同的host(如www.google.com),那么我们 ...
- c/c++运算符
1.算术运算符(+ - / * %) 2.移位运算符 移运算符:操作数必须是整形,>>,逻辑左移左边移入的位用0填充,算数左移左边移入的的位用符号位补齐.(无符号数为逻辑左移,对于 ...
- 尚硅谷Docker---docker安装及简介
尚硅谷Docker---docker安装及简介 一.总结 一句话总结: docker就相当于是一个极微型的linux系统,独立 1.使用Docker的步骤? 1).安装Docker 2).去Docke ...
- 从输入url到页面展现的过程
先看一幅图:(下面的所有图我都进行拉伸压缩了 如果看不到 可以右键复制图片地址 然后到浏览器粘贴查看 不然显示不全图片) mac没有画图软件 不好意思 xmind做的 1. 输入网址 当 ...
- String 部分源码分析
String 无参数构造函数 /** * 底层存储字符串的目标字节数组, * Jdk 8 之前都是字符数组 private final char[] value; */ @Stable private ...
- CentOS7 修改网卡名称为eth0 & 在VMWare中添加多网卡配置
目录 目录 前言 在CentOS 7 中为什么这样命名网卡 在RHEL7中使用RHEL6的网卡命名规则 在VMWare中为CentOS7添加网卡设备 前言 无论是RHEL 7.还是CentOS 7都使 ...
- 【Spring】的【bean】管理(XML配置文件)【Bean实例化的三种方式】
Bean实例化的三种方式 说明:通过配置文件创建对象就称为Bean实例化. 第一种:使用类的无参构造创建(重点) 实体类 package com.tyzr.ioc; public class User ...