IOC–inverse of control控制反转
在传统的开发中,对象的创建是由程序猿根据程序逻辑来创建的,使用了spring以后,对象的创建交给spring的容器来完成。容器的创建好对象后,会自动将对象设置到指定的位置。创建对象的控制权限由程序猿变成了spring容器。这种控制权限的转移,称为控制反转。由传统的主动式编程变为被动式编程。
代码:
User类:
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;
}
}
spring配置文件
<?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">
<bean id="user" class="cn.zwq.vo.User">
<property name="name" value="张三"/>
<property name="age" value="22"/>
</bean>
</beans>
测试:
public static void main(String[] args) {
//启动容器
ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
//从容器中获取对象
User user = (User)ac.getBean("user");
System.out.println(user.getName()+"----"+user.getAge());
}
下面我简单陈述一下我自己的理解:ioc的机制就是将生成对象的过程交给第三方的容器进行处理,利用反射的机制生成对象,全部的控制权都交给了第三方,第三方的容器起到了类似于粘合剂的作用。如此一来,对象与对象之间,耦合的程度也得到了降低。
DI的理解:依赖注入是类与类之间不再是以硬编码的方式耦合在一起,而是以配置文件组织在一起。
AOP:aspect orentied programming–面向切面编程
首先简单陈述一下我自己的理解:在不改变原有代码的前提下,为程序增加新的功能。
spring的aop的应用:声明式事务,自定义AOP
增加日志处理:
log类:
/**
* 切面实现spring提供的 api。实现了前置通知。
*
*/
public class Log implements MethodBeforeAdvice{
@Override
public void before(Method method, Object[] args, Object target)
throws Throwable {
System.out.println("info:"+new Date()+"\n "+target.getClass().getName()+"\t"+method.getName());
}
}
service层
public class UserServiceImpl implements UserService{
/**
* 在每次实现业务方法时 需要增加一些公共的功能。
* 如:日志,缓存,安全检查,事务等
*/
public void save() {
System.out.println("保存用户");
}
public void update() {
System.out.println("更新用户");
}
public void delete() {
System.out.println("删除用户");
}
public void get() {
System.out.println("获取用户");
}
}
spring配置文件:
<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 class="cn.zwq.service.impl.UserServiceImpl"/>
<bean id="log" class="cn.zwq.log.Log"/>
<aop:config>
<!-- 切入点配置
execution 表示 匹配断言的表达式
第一个* 表示所有返回值
第二个* 表示impl包下的所有类
第三个* 表示类中的所有方法
(..)括号中的..表示所有参数
-->
<aop:pointcut expression="execution(* cn.zwq.service.impl.*.*(..))" id="pointcut"/>
<aop:advisor advice-ref="log" pointcut-ref="pointcut"/>
</aop:config>
</beans>
测试:
public class Demo {
public static void main(String[] args) {
ApplicationContext ac = new ClassPathXmlApplicationContext("beans.xml");
UserService userService = ac.getBean(UserService.class);
System.out.println(userService.getClass());
userService.save();
}
}
spring对AOP的支持:
说到spring的AOP那就离不开代理模式了。
这里我就不一点点的讲起了,有兴趣的可以去看看我之前的关于静态代理和动态代理的博客。
<aop:aspect> 标签定义横切逻辑,就是要在连接点上做什么
<aop:Advisor> 标签定义在哪些连接点应用什么样的<aop:aspect>
Spring根据配置文件中关于Advisor和Pointcut的配置找到所有Advisor,在bean初始化时根据需要为其生成代理。并在生成代理的过程中把advice织入在代理对象里。这应该就是aop的实现原理了。