关于Spring的学习记录
- Spring的概述:一个开源框架,由Rod Johnson创建,功能为降低开发企业应用的复杂性。
- Spring框架由7大模块组成,Spring Core,Context,AOP,DAO,O/R,Web,MVC等。
- 下载Spring:可以登入官网下载jar包本次学习只用到4.0.6。
下图为解压后的Spring文件夹
文件夹libs里是我们主要用到的jar
而说明框架则是以html和pdf格式给我们,在docs/spring-framework-reference里分别为html、htmlsingle和pdf。html文件夹下是分开说明的,而htmlsingle和pdf则是统一说明。
以下jar是常用到的需要导入,其中common需要另外下载。
下面简单说明一下Spring的使用
1.创建工程,并导入所需jar
2.创建两个包和两个class(需要记住)
3.创建一个xml(名字随意)
结果图如下:
Text.java中内容:
package com.service;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.text.hello;
public class Text {
public static void main(String[] args) {
ApplicationContext ac=new ClassPathXmlApplicationContext("applicationContext.xml"); //创建使用路径
hello he=(hello)ac.getBean("hello"); //获取Bean
he.say(); //调用方法
}
}
hello.java中内容:`
package com.text;
public class hello {
public void say() {
System.out.println("hello123"); //方法
}
}
然后就是applicationContext.xml中内容(其中内容不需要手打,可以在html中的beans.html文件中复制粘贴):
<?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 id="hello" class="com.text.hello"></bean> //bean 连接id唯一,class为路径
</beans>
运行结果如下: