Spring是分层的JavaSE/EE一站式轻量级开源框架,它以IOC(Inverse of Control 控制反转)和AOP(Aspect Oriented Programming 面向切面编程)为内核 ;Spring中可以让众多优秀模式进行自由组合。
Spring的好处是方便解耦,简化开发,Spring像一个巨大工厂,可以将所有的对象的创建和依赖关系进行维护,原来由我们自己进行管理的,现在交给Spring管理;
2.Spring架构的组成Spring 框架是一个分层架构,它由 7 个模块组成。Spring 模块构建在核心容器之上,核心容器定义了创建、配置和管理 bean 的方式 。
3.Spring的入门1)在pom.xml文件下导入相关依赖
org.springframework spring-context5.0.2.RELEASE
2)在resources目录下创建我们的applicationContext.xml配置文件
3)在测试类中进行测试
public class SpringTest {
@Test
public void test(){
//加载配置文件
ClassPathXmlApplicationContext applicationContext =
new ClassPathXmlApplicationContext("applicationContext.xml");
//获取Bean对象
UserService userService = (UserService)applicationContext.getBean("userService");
//调用方法
userService.addUser();
userService.deleteUser();
//关闭
applicationContext.close();
}
}
4.Spring创建bean的三种方式
Bean元素:Spring管理对象的容器
id属性:给被管理对象起个名字(一般使用它的类名或接口名)
class属性:被管理对象的完整类名
name属性:根据名字获取对象
创建bean的三种方式: 第一种方式:使用默认的构造函数创建在application.xml中配置
注意:如果类中没有默认构造函数,则对象无法创建
第二种方式:使用工厂中的方法创建对象
注意:这种创建方式在工厂中的对应的方法中创建对象,再放入spring容器之中进行管理
第三种方式:使用工厂中的静态方法来创建对象
注意:这种方式与第二种类似,其中的方法为静态的方法



