Spring简介
spring一站式框架
正是因为spring框架性质是属于容器性质的.
容器中装什么对象就有什么功能.所以可以一站式.
不仅不排斥其他框架,还能帮其他框架管理对象.
aop支持
ioc思想
spring jdbc
aop 事务
junit 测试支持
spring搭建
1.导包
我们需要导入基础核心的四个包
再导入两个额外的日志包:
spring-beans-4.2.4.RELEASE.jar
spring-context-4.2.4.RELEASE.jar
spring-core-4.2.4.RELEASE.jar
spring-expression-4.2.4.RELEASE.jar
这4个核心包必须导入
其中 com.springsource.org.apache.log4j-1.2.15.jar 这个包是可选的【做低版本日志兼容用的】
2.创建一个对象
package cn.zdfy.domain;
public class User {
private String name;
private Integer age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
}
3.书写配置注册对象到容器
配置文件名任意(建议applicationContext.xml)
3.1导入约束 applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.springframework.org/schema/beans" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd ">
<!-- 将User对象交给spring容器管理 -->
<bean name="user" class="cn.zdfy.domain.User">
</bean>
</beans>
4.代码测试
ApplicationContext ac=new ClassPathXmlApplicationContext("applicationContext.xml");
User u=(User)ac.getBean("user");
System.out.println(u.toString());