开发工具版本:
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配置文件中的各个标签和元素。
本文通过一个简单的Spring HelloWorld示例介绍了如何使用Spring框架进行依赖注入。示例中创建了一个HelloWorld类并定义了一个消息属性,通过Spring配置文件Bean.xml将消息注入到类中。
778

被折叠的 条评论
为什么被折叠?



