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

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">
	
	<!-- 配置bean -->
	<bean id="helloWorld" class="com.atguigu.spring.helloworld.HelloWorld2">
	    <property name="name" value="ligang"></property>
	</bean>
</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();

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

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值