Spring学习笔记(一)Spring的Hello World!

开发工具版本:

Eclipse:eclipse-jee-luna-SR2-win32-x86_64:4.4.2

Spring: 4.38

JDK :1.8

Tomcat: 7.0.57

Spring 架包:


其中commons-loggingIOC和AOP-1.2.jar 这个是spring依赖的日志包,其他四个是spring的ioc核心包,也是spring的核心包。

Spring的核心是IOC和AOP,这里按照惯例,先来一个Spring的Hello World!

先看看新建的这个工程结构:


HelloWorld.java

package com.spring;

public class HelloWorld {
	private String message;

	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		System.out.println("spring 的 HelloWorld:" + message);
	}
}

MainApp.java

package com.spring;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class MainApp {
	public static void main(String[] args) {
		ApplicationContext context = new ClassPathXmlApplicationContext("Bean.xml");
		
		HelloWorld obj = (HelloWorld) context.getBean("helloWorld");
	    obj.getMessage();
	}
}

spring的配置文件:Bean.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-3.0.xsd">

   <bean id="helloWorld" class="com.spring.HelloWorld">
       <property name="message" value="Hello World!"/>
   </bean>

</beans>

运行结果是:

三月 26, 2018 11:26:02 下午 org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh
信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@76fb509a: startup date [Mon Mar 26 23:26:02 CST 2018]; root of context hierarchy
三月 26, 2018 11:26:02 下午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
信息: Loading XML bean definitions from class path resource [Bean.xml]
spring 的 HelloWorld:Hello World!
今天就先到这里了,明天总结下spring配置文件中的各个标签和元素。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值