一、Spring框架简述
Spring是一个非常活跃的开源框架, 它是一个基于IOC和AOP来构架多层JavaEE系统的框架,它的主要目地是简化企业开发。
Spring以一种非侵入式的方式来管理你的代码, Spring提倡“最少侵入”,这也就意味着你可以适当的时候安装或卸载Spring。
Spring采用独立的模块式开发,这样可以有选择的使用其中的模块,降低了业务对象的复杂性和组件之间的耦合性。
Spring的AOP支持允许将一些通用的功能,如:性能分析,事务管理,日志记录等进行集中的模块处理,提高了复用,降低了和业务功能的耦合。
Spring支持和ORM层以及MVC层框架的整合,简化了对其他层次的访问。
二、框架组成
如图所见当使用Spring的时候必须使用的是Spring核心容器,它是Spring框架的核心机制,Spring核心容器主要由四个包组成:Core、Beans、Context、ExpressionLanguage。主要提供Spring对于IOC的容器支持。Spring4.0版本的新特性:
1) 删除了一些过时的包和类
2)新增了泛型限定式的依赖注入,Map,List注入,延迟注入等功能
3)Web支持了Servlet3.0及更高版本
4)支持了Groovy DSL进行bean的配置
5)支持了WebSocket等协议
三、代码案列
3.1目录结构
3.2Student和IDcard实体类
package com.spring.entity;
public class Student {
private String name;
private int age;
private IDCard card;
public Student(String name,int age,IDCard card) {
this.name = name;
this.age = age;
this.card = card;
}
public IDCard getCard() {
return card;
}
//通过set方法进行注入
public void setCard(IDCard card) {
this.card = card;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "Student [name=" + name + ", age=" + age + ", cardNumer=" + card.getNubmer() + ",cardAdress="+card.getAddress()+"]";
}
}
package com.spring.entity;
public class IDCard {
private String nubmer;
private String address;
public IDCard(String number,String address) {
this.nubmer = number;
this.address = address;
}
public String getNubmer() {
return nubmer;
}
public void setNubmer(String nubmer) {
this.nubmer = nubmer;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
}
3.3DateServlet接口类
package com.spring.dao;
public interface DateServlet {
public void show();
}
3.4DateServletImpl实现类
package com.spring.dao.impl;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.spring.dao.DateServlet;
import com.spring.entity.Student;
public class DateServletImpl implements DateServlet{
private Student student;
//使用setStudent方法进行注入
public void setStudent(Student student) {
this.student = student;
}
//展示数据
@Override
public void show() {
// TODO Auto-generated method stub
System.out.println(student.toString());
}
}
3.5MainTest测试类
package com.sping.test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.spring.dao.impl.DateServletImpl;
public class MainTest {
public static void main(String[] args) {
ApplicationContext cxt = new ClassPathXmlApplicationContext("spring*.xml");
DateServletImpl test = (DateServletImpl) cxt.getBean("StuDaoImpl");
test.show();
}
}
3.6文件配置
<?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-4.0.xsd">
<bean id="student" class="com.spring.entity.Student">
<!--使用构造标签1、根据名字设置值2、可以根据构造函数里面的类型和位置3、目的:有多个构造函数会根据以上情况自动匹配 -->
<constructor-arg name="name" value="赵小航" ></constructor-arg>
<constructor-arg name="age" value="20"></constructor-arg>
<!--配置IDcard依赖-->
<constructor-arg name="card" ref="IDCard"></constructor-arg>
</bean>
<bean id="IDCard" class="com.spring.entity.IDCard">
<constructor-arg name="number" value="3422211997"></constructor-arg>
<constructor-arg name = "address" value="安徽省宿州市砀山县"></constructor-arg>
</bean>
<bean id="StuDaoImpl" class="com.spring.dao.impl.DateServletImpl">
<!--配置学生依赖 -->
<property name="student" ref="student"></property>
</bean>
</beans>
3.6结果展示
4、spring总结
ApplicationContext cxt = new ClassPathXmlApplicationContext("beans.xml");
可以获得在beans.xml中配置的类生产的实例对象。
Spring的整个运行过程是生成Bean容器ClassPathXmlApplicationContext,使用容器加载XML中配置的所有bean
通过cxt.getBean(");可以获取到容器实例cxt中Bean:DateServletImpl
思考:如果没有使用Spring情况下,AppContainer 实例是如何创建的呢?
对,是使用构造函数。那么也就是说现在使用Spring的Bean容器进行创建了AppContainer的实例,来替代直接使用构造函数创建实例了。这就是Spring的核心功能之一。
Spring容器的两核心功能:
1)Spring作为超级工厂,负责工程中所有Java对象的创建,管理。这些Java对象称为Bean。
2)Spring容器能管理容器中所有Bean的依赖关系,这个在Spring中称为“依赖注入”。