Spring的目录
Apache Common Logging API 下载地址
Spring 下载地址:
名称 | 作用 |
---|---|
docs: | 包含Spring的API文档和开发规范 |
libs: | 包含开发需要的jar包和源码包 |
schema: | 包含开发所需要的schema文件,在这些文件中定义了Spring相关配置文件的约束。 |
libs目录下的4个jar包
名称 | 作用 |
---|---|
spring-core-x.x.xx.jar | 包含Spring框架基本核心工具类,Spring其他组件都要用到这个包中的类,时其他组件的基本核心。 |
spring-beans-x.x.xx.jar | 所有应用都要用到的,它包含访问配置文件、创建和管理 Bean 以及进行 Inversion of Control(IoC)或者 Dependency Injection(DI)操作相关的所有类。 |
spring-context-x.x.xx.jar | Spring 提供在基础 IoC 功能上的扩展 |
spring-expression-x.x.xx.jar | 定义了 Spring 的表达式语言.需要注意的是,在使用 Spring 开发时,除了 Spring 自带的 JAR 包以外,还需要一个第三方 JAR 包 commons.logging 处理日志信息。 |
使用 Spring 框架时,只需将 Spring 的 4 个基础包以及 commons-logging-1.2.jar 包复制到项目的 lib 目录,并发布到类路径中即可。
Spring项目的创建
创建Java项目,
添加jar包
创建Java类
package org.example;
public class HelloWorld {
private String message;
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
}
设置Main.java
package org.example;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Main {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("Beans.xml");
HelloWorld obj = context.getBean("helloWorld",HelloWorld.class);
System.out.println(obj.getMessage());
}
}
创建配置文件
<?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="org.example.HelloWorld">
<property name="message" value="Hello World!" />
</bean>
</beans>
运行: