• 首页

  • 笑一笑

  • 相册

  • 文学

  • 日志

  • 关于页面
u f a n y
u f a n y

英杰

获取中...

10
10

【尚硅谷】spring学习笔记(1):HelloWorld

发表于 2020-10-10 • 被 1,586 人看爆

1、安装SPRING TOOL SUITE 这个 Eclipse 插件。

2、搭建spring开发环境,引入jar包。

3、创建spring项目

3.1、创建一个HelloWorld类

public class HelloWorld {
private String name;

public void setName( String name) {
	this.name = name;
}

public void hello() {
	System.out.println("您好:" + name);
}

}

3.2、创建一个配置文件applicationContext.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">
&lt;!-- 配置bean --&gt;
&lt;bean id="helloWorld" class="com.atguigu.spring.helloworld.HelloWorld2"&gt;
    &lt;property name="name" value="ligang"&gt;&lt;/property&gt;
&lt;/bean&gt;

</beans>

3.3、创建 Spring 的 IOC 容器,并从IOC容器里面获取bean是实例,并使用bean

import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Main {

public static void main(String[] args) {

	//1. 创建 Spring 的 IOC 容器(单独这条语句会初始化构造器和类方法)
	ClassPathXmlApplicationContext cpa = new ClassPathXmlApplicationContext("applicationContext.xml");
	
	//2. 从 IOC 容器中获取 bean 的实例
	HelloWorld2 helloWorld = (HelloWorld2) cpa.getBean("helloWorld");
	
	//3. 使用 bean
	helloWorld.hello();</code></pre> 

3.4、输出

六月 06, 2018 11:59:23 上午 org.springframework.context.support.AbstractApplicationContext prepareRefresh
信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@6193b845: startup date [Wed Jun 06 11:59:23 CST 2018]; root of context hierarchy
六月 06, 2018 11:59:23 上午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
信息: Loading XML bean definitions from class path resource [applicationContext.xml]
您好:ligang

分享到:
Spring学习笔记(马士兵spring视频笔记)
Spring学习笔记之Spring MVC体系学习(一)
  • 文章目录
  • 站点概览
英杰

帅哥英杰

QQ Email RSS
看爆 Top5
  • 使用 MHA 实现 MySQL 主从复制高可用 3,214次看爆
  • nginx反向代理配置去除前缀 2,960次看爆
  • 国产微服务网关-Soul(真香) 2,883次看爆
  • Kubernetes - Xshell连接虚拟机 & 搭建Kubernetes基础集群 2,663次看爆
  • 不把一切看得太重,生命才会更加洒脱 2,628次看爆

Copyright © 2025 英杰 · 冀ICP备19001756号-1

Proudly published with Halo · Theme by fyang · 站点地图