Spring是一个基于IOC和AOP的结构J2EE系统的框架 ,IOC 反转控制 是Spring的基础,Inversion Of Control ,简单说就是创建对象由以前的程序员自己new 构造方法来调用,变成了交由Spring创建对象 ,DI 依赖注入 Dependency Inject. 简单地说就是拿到的对象的属性,已经被注入好相关值了,直接使用即可。
我这边为大家准备了Spring相关的JAR包:
链接:https://pan.baidu.com/s/15W3eBQMHdbgl2gyr8O38zA 密码:jbjg
首先,我们先准备一个JavaBean类
package cn.encore.pojo;
public class Rapper {
private int id;
private String name;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
然后,我们要在src目录下创建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-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<bean name="r" class="cn.encore.pojo.Rapper">
<property name="name" value="GongFuPen" />
</bean>
</beans>
这里我们为这个bean起了一个“r”的名字,并制定的类是我们创建的Rapper类,并为这个Rapper的name属性注入GongFuPen(CSC功夫胖)
然后,编写测试类来测试一下
package cn.encore.test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import cn.encore.pojo.*;
public class TestSpring {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext(
new String[] {"applicationContext.xml"});
Rapper r = (Rapper)context.getBean("r");
System.out.println(r.getName());
}
}
我们通过ClassPathXmlApplicationContext得到一个ApplicationContext对象,然后通过这个对象的getBean方法,并传入我们配置的"r",就得到了对象
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
以获取对象的方式来进行比较
传统的方式:
通过new 关键字主动创建一个对象
IOC方式:
对象的生命周期由Spring来管理,直接从Spring那里去获取一个对象。 IOC是反转控制 (Inversion Of Control)的缩写,就像控 制权从本来在自己手里,交给了Spring。
打个比喻:
传统方式:相当于你自己去菜市场new 了一只鸡,不过是生鸡,要自己拔毛,去内脏,再上花椒,酱油,烤制,经过各种工序之后,才可以食用。
用 IOC:相当于去馆子(Spring)点了一只鸡,交到你手上的时候,已经五味俱全,你就只管吃就行了。
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
然后我们尝试注入对象
先编写一个厂牌类
package cn.encore.pojo;
public class ChangPai {
private String name;
private String city;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
@Override
public String toString() {
return "ChangPai [name=" + name + ", city=" + city + "]";
}
}
然后,为Rapper类增加ChangPai属性,并为其提供getter,setter
package cn.encore.pojo;
public class Rapper {
private int id;
private String name;
private ChangPai changPai;
public ChangPai getChangPai() {
return changPai;
}
public void setChangPai(ChangPai changPai) {
this.changPai = changPai;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
然后修改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-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<bean name="r" class="cn.encore.pojo.Rapper">
<property name="name" value="GongFuPen" />
<property name="changPai" ref="c" />
</bean>
<bean name="c" class="cn.encore.pojo.ChangPai">
<property name="name" value="CSC" />
<property name="city" value="长沙" />
</bean>
</beans>
注入类要使用ref属性,值就是他的name属性
然后写一个测试类
package cn.encore.test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import cn.encore.pojo.*;
public class TestSpring {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext(
new String[] {"applicationContext.xml"});
Rapper r = (Rapper)context.getBean("r");
System.out.println(r.getName());
System.out.println(r.getChangPai());
}
}
这里,厂牌类就被注入到了Rapper类
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
使用注解配置
我们删除applicationContext.xml中的<property name="changPai" ref="c" />,并在之前新增<context:annotation-config/>
<?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-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<context:annotation-config/>
<bean name="r" class="cn.encore.pojo.Rapper">
<property name="name" value="GongFuPen" />
</bean>
<bean name="c" class="cn.encore.pojo.ChangPai">
<property name="name" value="CSC" />
<property name="city" value="长沙" />
</bean>
</beans>
然后在Rapper类中private ChangPai changPai;之前加上@Autowired这个注解
package cn.encore.pojo;
import org.springframework.beans.factory.annotation.Autowired;
public class Rapper {
private int id;
private String name;
@Autowired
private ChangPai changPai;
public ChangPai getChangPai() {
return changPai;
}
public void setChangPai(ChangPai changPai) {
this.changPai = changPai;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
再次运行测试,效果一样
@Autowired加在setChangPai之上也是可以的
除了@Autowired之外,@Resource也是常用的手段:
@Resource(name="c")
private ChangPai changPai;
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
AOP 即 Aspect Oriented Program 面向切面编程 ,首先,在面向切面编程的思想里面,把功能分为核心业务功能,和周边功能。
所谓的核心业务,比如登陆,增加数据,删除数据都叫核心业务
所谓的周边功能,比如性能统计,日志,事务管理等等
周边功能在Spring的面向切面编程AOP思想里,即被定义为切面 ,在面向切面编程AOP的思想里面,核心业务功能和切面功能分别独立进行开发 ,然后把切面功能和核心业务功能 "编织" 在一起,这就叫AOP
1. 功能分两大类,辅助功能和核心业务功能
2. 辅助功能和核心业务功能彼此独立进行开发
3. 比如登陆功能,即便是没有性能统计和日志输出,也可以正常运行
4. 如果有需要,就把"日志输出" 功能和 "登陆" 功能 编织在一起,这样登陆的时候,就可以看到日志输出了
5. 辅助功能,又叫做切面,这种能够选择性的,低耦合的把切面和核心业务功能结合在一起的编程思想,就叫做切面编程
准备业务类 ProductService
package cn.encore.service;
public class ProductService {
public void doSomeService(){
System.out.println("doSomeService");
}
}
准备日志切面 LoggerAspect
package cn.encore.aspect;
import org.aspectj.lang.ProceedingJoinPoint;
public class LoggerAspect {
public Object log(ProceedingJoinPoint joinPoint) throws Throwable {
System.out.println("start log:" + joinPoint.getSignature().getName());
Object object = joinPoint.proceed();
System.out.println("end log:" + joinPoint.getSignature().getName());
return object;
}
}
<bean name="s" class="com.how2java.service.ProductService">
</bean>
声明业务对象
<bean id="loggerAspect" class="com.how2java.aspect.LoggerAspect"/>
声明日志切面
<aop:pointcut id="loggerCutpoint"
expression=
"execution(* com.how2java.service.ProductService.*(..)) "/>
指定右边的核心业务功能
<aop:aspect id="logAspect" ref="loggerAspect">
<aop:around pointcut-ref="loggerCutpoint" method="log"/>
</aop:aspect>
指定左边的辅助功能
然后通过aop:config把业务对象与辅助功能编织在一起。
execution(* com.how2java.service.ProductService.*(..))
这表示对满足如下条件的方法调用,进行切面操作:
* 返回任意类型
<?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-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<context:annotation-config/>
<bean name="r" class="cn.encore.pojo.Rapper">
<property name="name" value="GongFuPen" />
</bean>
<bean name="c" class="cn.encore.pojo.ChangPai">
<property name="name" value="CSC" />
<property name="city" value="长沙" />
</bean>
<bean name="s" class="cn.encore.service.ProductService">
</bean>
<bean id="loggerAspect" class="cn.encore.aspect.LoggerAspect"/>
<aop:config>
<aop:pointcut id="loggerCutpoint"
expression=
"execution(* cn.encore.service.ProductService.*(..)) "/>
<aop:aspect id="logAspect" ref="loggerAspect">
<aop:around pointcut-ref="loggerCutpoint" method="log"/>
</aop:aspect>
</aop:config>
</beans>
运行测试,可以发现在编织之后,业务方法运行之前和之后分别会打印日志
package cn.encore.test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import cn.encore.service.ProductService;
public class TestSpring2 {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext(
new String[] { "applicationContext.xml" });
ProductService s = (ProductService) context.getBean("s");
s.doSomeService();
}
}
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
把XML方式配置AOP 改造为注解方式
使用@Component("s") 注解ProductService 类
package cn.encore.service;
import org.springframework.stereotype.Component;
@Component("s")
public class ProductService {
public void doSomeService(){
System.out.println("doSomeService");
}
}
注解配置切面
@Aspect 注解表示这是一个切面
@Component 表示这是一个bean,由Spring进行管理
@Around(value = "execution(* cn.encore.service.ProductService.*(..))") 表示对com.how2java.service.ProductService 这个类中的所有方法进行切面操作
package cn.encore.aspect;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;
@Aspect
@Component
public class LoggerAspect {
@Around(value = "execution(* cn.encore.service.ProductService.*(..))")
public Object log(ProceedingJoinPoint joinPoint) throws Throwable {
System.out.println("start log:" + joinPoint.getSignature().getName());
Object object = joinPoint.proceed();
System.out.println("end log:" + joinPoint.getSignature().getName());
return object;
}
}
去掉原有信息,添加如下3行
<context:component-scan base-package="cn.encore.aspect"/>
<context:component-scan base-package="cn.encore.service"/>
扫描包cn.encore.aspect和cn.encore.service,定位业务类和切面类
<aop:aspectj-autoproxy/>
找到被注解了的切面类,进行切面配置
<?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-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<context:annotation-config/>
<bean name="r" class="cn.encore.pojo.Rapper">
<property name="name" value="GongFuPen" />
</bean>
<bean name="c" class="cn.encore.pojo.ChangPai">
<property name="name" value="CSC" />
<property name="city" value="长沙" />
</bean>
<context:component-scan base-package="cn.encore.aspect"/>
<context:component-scan base-package="cn.encore.service"/>
<aop:aspectj-autoproxy/>
</beans>
运行测试发现与之前效果相同
这里先让大家对Spring框架有个大致的了解,之后会更详细的介绍,Spring框架可以说十分重要,他是其他框架整合的基础,一定要深入理解