引言
首先大致介绍一下,假如大家都是小白,框架是什么?是不是要自己重头实现?并不是,框架是别人已经做好的,你可以搜一下spring的官网,人家已经把这个框架做成jar包了,我们要学的是如何使用这个框架来实现我们的需求(明白其中原理)。
开发包jar链接地址:
http://pan.baidu.com/s/1kVuklMn
IOC/DI
Spring是一个基于IOC和AOP的结构J2EE系统的框架
首先理解下IOC和DI…AOP先别管他,慢慢了解。
IOC 反转控制 是Spring的基础,Inversion Of Control
简单说就是创建对象由以前的程序员自己new 构造方法来调用,现在呢,交给了框架来创建咯。就是说控制权给了框架啦。
DI 依赖注入 Dependency Inject. 可以理解为,不靠代码来给对象赋值,用配置文件xml(这个架构有个配置文件,下面会说)里面来规定对象的属性。
配置spring环境
如何找到spring的jar包?你可以到官方网站下载,我这个博客提供3.0的jar包,你们也可以下载来跟着一起实现。
如何将jar包导进去你的project,这个小弟我就不说了。
首先创建一个spring项目吧。然后将jar包导进去。
最后再src目录下新建一个xml文件。。所谓的配置文件,,通常命名为applicationContext.xml
配置文件xml代码如下:
第一个bean头,就是些规定什么的吧,照抄就行,(因为播猪我不懂);
接下来这那两个bean是自己加进去的就有点意思了。。先别管,等我把Category和product代码放出来再一起弄懂。
我们这次实验,就是写两个类,Category和product类,然后你往下翻翻product类的代码,你会发现,product类里面有一个Category变量。
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<bean name="c" class="spring.Category">
<property name="name" value="category 1" />
<property name="id" value="5" />
</bean>
<bean name="p" class="spring.Product">
<property name="name" value="product1" />
<property name="category" ref="c" />
</bean>
</beans>
Category类
package spring;
public class Category {
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
private int id;
private String name;
}
Product类
package spring;
public class Product {
private int id;
private String name;
private Category category;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Category getCategory() {
return category;
}
public void setCategory(Category category) {
this.category = category;
}
}
xml文件下面两个bean是啥意思?
当你把我上面的代码合起来之后,你就可以理解xml文件到底在说些什么,其实第一个bean就是他创造了一个名字叫c的Category,然后把他两个成员变量赋值了分别为category 1和id=5.。
当然**第二个bean就是定义了一个名字叫p的并且把成员变量赋值了,当然这里要注意给category赋值的时候用的是ref=“c”!!!!c就是各个上面那个定义的category。
可以看出这个框架大概就是用配置文件来组织各种要用的对象,然后其他地方用啊!那怎么用这些变量啊?看以下代码!!**
首先要读取这个文件咯,用ApplicationContext context =
new ClassPathXmlApplicationContext
(new String[] { “applicationContext.xml” });
然后通过bean关键字来获得对象
Category c = (Category) context.getBean(“c”);
然后就可以用了。。product这个类用起来也是一样的。。
package spring;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class test {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext(new String[] { "applicationContext.xml" });
Category c = (Category) context.getBean("c");
Product p = (Product)context.getBean("p");
System.out.println(c.getName());
System.out.println(c.getId());
c.setId(11);
System.out.println(c.getId());
System.out.println(p.getCategory().getId());
}
}
总结
大家实现了这个之后,有没有初步的印象啦?哈哈,加油一起学习。