1、Spring简介
- Spring是一个开源的免费框架
- Spring是一个轻量级的,非入侵式的框架
- 控制反转(IOC),面向切面编程(AOP)【重点】
- 支持事务的处理,对框架整合的支持
Spring是一个轻量级的控制反转(IOC)和面向切面编程(AOP)的框架
2、Spring组成
Spring由7个定义良好的模块组成,每个模块都可以独立存在,
-
核心容器:核心容器提供 Spring 框架的基本功能。核心容器的主要组件是BeanFactory,它是工厂模式的实现。BeanFactory 使用控制反转(IOC) 模式将应用程序的配置和依赖性规范与实际的应用程序代码分开。
-
Spring 上下文:Spring 上下文是一个配置文件,向 Spring 框架提供上下文信息。Spring 上下文包括企业服务,例如 JNDI、EJB、电子邮件、国际化、校验和调度功能。
-
Spring AOP:通过配置管理特性,Spring AOP 模块直接将面向切面的编程功能 , 集成到了 Spring 框架中。所以,可以很容易地使 Spring 框架管理任何支持 AOP的对象。Spring AOP 模块为基于 Spring 的应用程序中的对象提供了事务管理服务。通过使用 Spring AOP,不用依赖组件,就可以将声明性事务管理集成到应用程序中。
-
Spring DAO:JDBC DAO 抽象层提供了有意义的异常层次结构,可用该结构来管理异常处理和不同数据库供应商抛出的错误消息。异常层次结构简化了错误处理,并且极大地降低了需要编写的异常代码数量(例如打开和关闭连接)。Spring DAO 的面向 JDBC 的异常遵从通用的 DAO 异常层次结构。
-
Spring ORM:Spring 框架插入了若干个 ORM 框架,从而提供了 ORM 的对象关系工具,其中包括 JDO、Hibernate 和 iBatis SQL Map。所有这些都遵从 Spring 的通用事务和 DAO 异常层次结构。
-
Spring Web 模块:Web 上下文模块建立在应用程序上下文模块之上,为基于 Web 的应用程序提供了上下文。所以,Spring 框架支持与 Jakarta Struts 的集成。Web 模块还简化了处理多部分请求以及将请求参数绑定到域对象的工作。
-
Spring MVC 框架:MVC 框架是一个全功能的构建 Web 应用程序的 MVC 实现。通过策略接口,MVC 框架变成为高度可配置的,MVC 容纳了大量视图技术,其中包括 JSP、Velocity、Tiles、iText 和 POI。
3、IoC
IOC本质
:控制反转IoC(Inversion of Control),是一种思想,DI(依赖注入)是实现IoC的一种方法。
IoC是Spring的核心内容。
3.1 Spring IoC配置
1、基础类
public class Hello {
private String str;
public String getStr() {
return str;
}
public void setStr(String str) {
this.str = str;
}
@Override
public String toString() {
return "Hello{" +
"str='" + str + '\'' +
'}';
}
}
2、配置文件
<?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.xsd">
<!--使用Spring来创建对象-->
<!--bean就是java对象 , 由Spring创建和管理-->
<bean id="hello" class="com.luke.pojo.Hello">
<property name="str" value="Spring"/>
</bean>
</beans>
3、测试类
public class MyTest {
@Test
public void test(){
//获取Spring的上下文对象
ApplicationContext context = new ClassPathXmlApplicationContext("bean.xml");
Hello hello = (Hello) context.getBean("hello");
System.out.println(hello.toString());
}
}
3.2 IoC创建对象的方式
3.2.1、默认使用无参构造创建对象
3.2.2、使用有参构造创建对象
1)通过下标赋值
<bean id="user" class="com.luke.pojo.User">
<constructor-arg index="0" value="test"/>
</bean>
2)通过类型赋值
<bean id="user" class="com.luke.pojo.User">
<constructor-arg type="java.lang.String" value="test"/>
</bean>
3)通过参数名赋值
<bean id="user" class="com.luke.pojo.User">
<constructor-arg name="name" value="test"/>
</bean>
4、Spring配置
4.1、别名
<alias name="user" alias="userTest"/>
取别名之后,可以使用别名取容器中生成的bean.
4.2、Bean的配置
<bean id="user" class="com.luke.pojo.User" name="user2 user3,user4;user5">
<property name="name" value="test"/>
</bean>
- id:bean的唯一标识符
- class:bean对象对应的全限定名,包名+类型
- name:别名,可以同时取多个别名。可以用“ ” , : 分割
4.3、import
团队开发时可以将多个配置文件合并为1个。
<import resource="bean2.xml"/>
5、DI 依赖注入
5.1、构造器注入
见3.2
5.2 Set方法注入
依赖注入是通过Set方法注入的。
- 依赖:bean对象的创建依赖于容器。
- 注入:bean对象的属性,由容器来注入
Class:
public class Student {
private String name;
private Address address;
private String[] books;
private List<String> hobbys;
private Map<String,String> card;
private Set<String> games;
private Properties info;
private String money;
}
配置文件:
<bean id="student" class="com.luke.pojo.Student">
<!--普通值注入-->
<property name="name" value="luke"/>
<!--引入类型注入-->
<property name="address" ref="address"/>
<!--数组注入-->
<property name="books" >
<array>
<value>数学书</value>
<value>语文书</value>
<value>美术书</value>
</array>
</property>
<!--List-->
<property name="hobbys">
<list>
<value>打篮球</value>
<value>跳高</value>
<value>羽毛球</value>
</list>
</property>
<!--Map-->
<property name="card">
<map>
<entry key="身份证" value="123456789"/>
<entry key="银行卡" value="1234567890"/>
</map>
</property>
<!--Set-->
<property name="games">
<set>
<value>LoL</value>
<value>DNF</value>
</set>
</property>
<!--空值-->
<!--<property name="money" value=""/>-->
<property name="money">
<null/>
</property>
<!--Properties-->
<property name="info">
<props>
<prop key="学号">123456</prop>
<prop key="座位">1</prop>
<prop key="姓名">luke</prop>
</props>
</property>
</bean>
5.3 扩展方式注入
P命名空间注入
,可以直接注入属性。针对无参构造函数。
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:p="http://www.springframework.org/schema/p"
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.xsd">
<bean id="user" class="com.luke.pojo.User" p:name="luke" p:age="28"/>
</beans>
C命名空间注入
,也可以直接注入属性。针对有参构造函数。
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:c="http://www.springframework.org/schema/c"
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.xsd">
<bean id="user2" class="com.luke.pojo.User" c:name="luke2" c:age="29"/>
</beans>
再使用时需要导入xml约束。
xmlns:p="http://www.springframework.org/schema/p"
xmlns:c="http://www.springframework.org/schema/c"
5.4、Bean的作用域
1、单例模式(默认就是单例模式)
所有创建出来的Bean使用同一个对象。
<bean id="address" class="com.luke.pojo.Address" scope="singleton"/>
2、原型模式
每次从容器中get的时候都会创建新的对象。
<bean id="address" class="com.luke.pojo.Address" scope="prototype"/>
3、其余的request、session、application 这些只能再web开发中使用。
6、Bean的自动装配
Spring中有三种装配方式
- 在xml中显式的配置
- 在java中显式的配置
- 隐式的自动装配bean
6.1 ByBame自动装配
<bean id="people" class="com.luke.pojo.People" autowire="byName">
<property name="name" value="luke"/>
</bean>
byName会在容器上下文中寻找和自己对象set方法后面的值对应的bean id,此时需要保证bean的id唯一
6.2 ByType自动装配
<bean id="people" class="com.luke.pojo.People" autowire="byType">
<property name="name" value="luke"/>
</bean>
byName会在容器上下文中寻找和自己对象属性类型相同的bean id,此时需要办证bean的class唯一
6.4 注解实现自动装配
要使用注解:
-
导入约束:context约束
-
配置注解的支持:context:annotation-config/
@Autowired
可以直接在属性上使用,也可以在set方法上使用。
public class People {
@Autowired
private Cat cat;
@Autowired
private Dog dog;
private String name;
}
@Autowired(required = false) 表示此属性可以为空
@Qualifier(value = "cat1") 显式的配置多个class相同id不同的bean
public class People {
@Autowired(required = false)
private Cat cat;
@Autowired
@Qualifier(value = "cat1")
private Dog dog;
private String name;
}
@Resource
@Resource注解也可以实现自动装配,设置name属性可以显式的配置多个class相同id不同的bean
public class People {
@Resource(name = "cat")
private Cat cat;
@Resource
private Dog dog;
private String name;
}
@Autowired和@Resource的区别:
- @Autowired通过bytype的方式实现,若无法判断唯一,再通过byname的方式实现。
- @Resource是通过byname的方式实现,若无法判断唯一,则继续通过bytype方式实现
7、使用注解开发
<?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: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/context
http://www.springframework.org/schema/context/spring-context.xsd">
<context:annotation-config/>
<!--扫描指定包,这个包下边的注解就会生效-->
<context:component-scan base-package="com.luke.pojo"/>
</beans>
7.1、bean
//等价于<bean id="user" class="com.luke.pojo.User"/>
@Component
public class User {
@Value("ice")
private String name;
}
7.2、属性如何注入
使用@Value("***")可以注入属性
7.3、衍生的注解
- Dao:
@Repository
- Service:
@Service
- Controller:
@Controller
这四个注解的功能是一样的
7.4、自动装配
@Autowired
:通过类型,名字进行自动装配@Resource
:通过名字,类型进行自动装配
7.5、作用域
单例模式:
@Component
@Scope("singleton")
public class User {
private String name;
}
原型模式:
@Component
@Scope("prototype")
public class User {
private String name;
}
7.6、小结
xml配置文件:更加万能,使用场景多,维护简单方便。
注解:不是自己的类无法使用,维护方便。
建议:xml配置文件管理bean,注解负责属性的注入
8、使用java方式配置Spring
配置类:
@Configuration
@Import(LukeConfig2.class)
public class LukeConfig {
//注册一个bean
//方法名相当于xml中的id
//返回值相当于class
@Bean
public User user(){
return new User();
}
}
测试需要通过AnnotationConfigApplicationContext获取bean
ApplicationContext context = new AnnotationConfigApplicationContext(LukeConfig.class);
9、AOP
AOP(Aspect Oriented Programming):面向切面编程。
AOP需要导入依赖:
<dependencies>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.9.4</version>
</dependency>
</dependencies>
9.1、Spring实现AOP
befor:
public class BeforeLog implements MethodBeforeAdvice {
//method:要执行的目标对象方法
//args:参数
//target:要执行的目标对象
public void before(Method method, Object[] args, Object target) throws Throwable {
System.out.println(target.getClass().getName()+"的"+method.getName()+"方法执行之前");
}
}
after:
public class AfterLog implements AfterReturningAdvice {
//returnValue:返回值
public void afterReturning(Object returnValue, Method method, Object[] orgs, Object target) throws Throwable {
System.out.println(target.getClass().getName()+"的"+method.getName()+"方法执行之后");
}
}
配置文件:
<?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"
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">
<!--注册bean-->
<bean id="userService" class="com.luke.service.UserServiceImpl"/>
<bean id="afterLog" class="com.luke.log.AfterLog"/>
<bean id="beforeLog" class="com.luke.log.BeforeLog"/>
<!--配置AOP,需要导入约束-->
<aop:config>
<!--切入点,execution(要执行的位置:public修饰词 返回值 类名 方法名 参数)-->
<aop:pointcut id="pointcut" expression="execution(* com.luke.service.UserServiceImpl.*(..))"/>
<!--执行环绕-->
<aop:advisor advice-ref="beforeLog" pointcut-ref="pointcut"/>
<aop:advisor advice-ref="afterLog" pointcut-ref="pointcut"/>
</aop:config>
</beans>
9.2、自定义实现AOP
方法:
public class DiyPointCut {
public void before(){
System.out.println("方法执行前");
}
public void after(){
System.out.println("方法执行后");
}
}
配置文件:
<?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"
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">
<!--注册bean-->
<bean id="userService" class="com.luke.service.UserServiceImpl"/>
<bean id="diy" class="com.luke.diy.DiyPointCut"/>
<aop:config>
<!--自定义切面,ref为要引用的类-->
<aop:aspect ref="diy">
<!--切入点-->
<aop:pointcut id="pointcart" expression="execution(* com.luke.service.UserServiceImpl.*(..))"/>
<!--通知:要切入的方法-->
<aop:before method="before" pointcut-ref="pointcart"/>
<aop:after method="after" pointcut-ref="pointcart"/>
<aop:around method="after" pointcut-ref="pointcart"/>
</aop:aspect>
</aop:config>
</beans>
9.3注解实现AOP
代码:
@Aspect//标注这个类是一个切面
public class AnnotationPointCut {
@Before("execution(* com.luke.service.UserServiceImpl.*(..))")
public void before(){
System.out.println("方法执行前---");
}
@After("execution(* com.luke.service.UserServiceImpl.*(..))")
public void after(){
System.out.println("方法执行后---");
}
@Around("execution(* com.luke.service.UserServiceImpl.*(..))")
public void around(ProceedingJoinPoint pj) throws Throwable {
System.out.println("方法前");
//执行方法
pj.proceed();
System.out.println("方法后");
}
}
配置文件:
<?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"
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">
<!--注册bean-->
<bean id="userService" class="com.luke.service.UserServiceImpl"/>
<bean id="annotation" class="com.luke.diy.AnnotationPointCut"/>
<!--开启注解支持-->
<aop:aspectj-autoproxy/>
</beans>