Spring框架的简介
什么是sprig
(1)Spring是一个分层的(一站式) 轻量级开源框架
(2)Spring为简化企业级开发而生,使用Spring开发可以将Bean对象,Dao组件对象, Service组件对象等交给Spring容器来管理,这样使得很多复杂的代码在Spring中开发却变得非常的优雅和简洁,有效的降低代码的耦合度,极大的方便项目的后期维护、升级和扩展。
Spring的优良特性
- 非侵入式:基于Spring开发的应用中的对象可以不依赖于Spring的API
- 控制反转:IOC——Inversion of Control,指的是将对象的创建权交给Spring去创建。使用Spring之前,对象的创建都是由我们自己在代码中new创建。而使用Spring之后。对象的创建都是由给了Spring框架。
- 依赖注入:DI——Dependency Injection,是指依赖的对象不需要手动调用setXX方法 去设置,而是通过配置赋值。
- 面向切面编程:Aspect Oriented Programming——AOP
- 容器:Spring是一个容器,因为它包含并且管理应用对象的生命周期
- 组件化:Spring实现了使用简单的组件配置组合成一个复杂的应用。在 Spring 中可以使用XML和Java注解组合这些对象。
- 一站式:在IOC和AOP的基础上可以整合各种企业应用的开源框架和优秀的第三方类库(实际上Spring 自身也提供了表述层的SpringMVC和持久层的Spring JDBC)
Spring jar包下载:https://repo.spring.io/libs-release-local/org/springframework/spring/
Spring的核心是?
控制反转(IOC)和面向切面(AOP)
一、控制反转(IOC)
控制反转IOC(Inversion of Control)和依赖注入DI(Dependecy Injection)是同一个概念,具体的讲:当某个角色需要另外一个角色协助的时候,在传统的程序设计过程中,通常由调用者来创建被调用者的实例。但在spring中创建被调用者的工作不再由调用者来完成,因此称为控制反转。创建被调用者的工作由spring来完成,然后注入调用者 ,因此也称为依赖注入。
二、什么是AOP?
面向切面编程(AOP)完善spring的依赖注入(DI),面向切面编程在spring中主要表现为两个 方面
1.面向切面编程提供声明式事务管理
2.spring支持用户自定义的切面
IOC的例子
注意:必须先创建lib目录,导入jar包,并配置为项目的Library.
1.创建一个USer的类来,演示IOC和DI
package com.gx.model;
public class User {
private String name;
private int age;
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 "User{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
}
2.配置applicationContext.xml
<?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.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<bean id="user01" name="user" class="com.gx.model.User">
<property name="name" value="五六七"/>
<property name="age" value="18"/>
</bean>
<bean id="useXmlConfigBean" class="com.gx.model.User"/>
<!--Java注解的配置方式 需要在XML中配置 base-package为配置扫描路径 -->
<context:component-scan base-package="com.gx.model"/>
</beans>
3.在Main类的main方法中编写测试代码
package com.gx;
import com.gx.model.User;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Main {
private static ApplicationContext context=null;
public static void main(String[] args) {
context=new ClassPathXmlApplicationContext("applicationContext.xml");
User user= (User) context.getBean("user01");//id
User user1=(User) context.getBean("user");//name
System.out.println(user);
System.out.println(user1);
}
}
打印结果: