Spring的三大核心思想:IOC(控制反转),DI(依赖注入),AOP(面向切面编程)。
(1)IOC(控制反转)
实现将组件间的关系从程序内部提到外部容器(spring的xml)来管理。
首先外部容器(spring.xml)中会动态的注册业务所需的对象(接口/类)
(2)DI(依赖注入)
组件之间的依赖关系由容器在应用系统运行期来决定, 也就是由容器动态地将某种依赖关系的目标对象实例注入到应用系统中的各个关联的组件之中
范例:
如: 注入xml中
<bean id= "animal" class = "phz.springframework.test.Cat" ><property name= "name" value= "kitty" /> </bean>
类1:public class Cat implements Animal {
private String name;public void say() {
System.out.println("I am " + name + "cat");
}
public void setName(String name) {
this.name = name; }
}
类2:
public class Dog implements Animal {
private String name;
public void say() {
System.out.println("I am " + name + "dog");
}
public void setName(String name) {
this.name = name; }
}
public interface Animal {
public void say();
}
通过获取容器注册的方法
ApplicationContext context = new FileSystemXmlApplicationContext("applicationContext.xml");
Animal animal = (Animal) context.getBean("animal"); //只要这里xml注入的animal变化,就可以控制不同的更改变化
animal.say();
如果注入bean时,把name值也注入进入了,那这里的值就动态变化了。
<property name = "name" value = "haha"></property>
其实DI就是IOC(控制反转)后,直接注入了其具体的数值
(3) AOP(面向切面编程)
利用一种称为"横切"的技术,剖解开封装的对象内部,并将那些影响了多个类的公共行为封装到一个可重用模块,并将其命名为"Aspect",即切面。所谓"切面",简单说就是那些与业务无关,却为业务模块所共同调用的逻辑或责任封装起来,便于减少系统的重复代码,降低模块之间的耦合度,并有利于未来的可操作性和可维护性。
使用"横切"技术,AOP把软件系统分为两个部分:核心关注点和横切关注点。业务处理的主要流程是核心关注点,与之关系不大的部分是横切关注点。横切关注点的一个特点是,他们经常发生在核心关注点的多处,而各处基本相似,比如权限认证、日志、事物。AOP的作用在于分离系统中的各种关注点,将核心关注点和横切关注点分离开来。
publicinterfaceHelloWorld{voidprintHelloWorld();voiddoPrint();}
publicclassHelloWorldImpl1implementsHelloWorld{publicvoidprintHelloWorld() { System.out.println("Enter HelloWorldImpl1.printHelloWorld()"); }publicvoiddoPrint() { System.out.println("Enter HelloWorldImpl1.doPrint()");return; }}
publicclassHelloWorldImpl2implementsHelloWorld{publicvoidprintHelloWorld() { System.out.println("Enter HelloWorldImpl2.printHelloWorld()"); }publicvoiddoPrint() { System.out.println("Enter HelloWorldImpl2.doPrint()");return; }}
publicclassTimeHandler{publicvoidprintTime() { System.out.println("CurrentTime = " +System.currentTimeMillis()); }}
<beansxmlns="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"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.2.xsd">
<beanid="helloWorldImpl1"class="com.xrq.aop.HelloWorldImpl1"/>
<beanid="helloWorldImpl2"class="com.xrq.aop.HelloWorldImpl2"/>
<beanid="timeHandler"class="com.xrq.aop.TimeHandler"/>
<aop:config>
<aop:aspectid="time"ref="timeHandler">
<aop:pointcutid="addAllMethod"expression="execution(* com.xrq.aop.HelloWorld.*(..))"/>
<aop:beforemethod="printTime"pointcut-ref="addAllMethod"/>
<aop:aftermethod="printTime"pointcut-ref="addAllMethod"/>
</aop:aspect>
</aop:config>
</beans>
结果调用:
ApplicationContext ctx=newClassPathXmlApplicationContext("aop.xml");
HelloWorld hw1= (HelloWorld)ctx.getBean("helloWorldImpl1");
HelloWorld hw2= (HelloWorld)ctx.getBean("helloWorldImpl2");
hw1.printHelloWorld();
hw1.doPrint();
System.out.println();
hw2.printHelloWorld();
hw2.doPrint();
最后结果:
CurrentTime = 1446129611993Enter
HelloWorldImpl1.printHelloWorld()
CurrentTime= 1446129611993
CurrentTime= 1446129611994Enter
HelloWorldImpl1.doPrint()
CurrentTime= 1446129611994
CurrentTime= 1446129611994
Enter HelloWorldImpl2.printHelloWorld()
CurrentTime= 1446129611994
CurrentTime= 1446129611994
Enter HelloWorldImpl2.doPrint()
CurrentTime= 1446129611994