Spring
Spring
1、介绍
Spring是业务层框架,在web开放领域使用广泛。Spring中设计两个思想,AOP和IOC。Spring是容器技术,提供强大的Bean管理机制。
2、体验Spring
-
引入maven依赖
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.oldboy</groupId> <artifactId>myspring</artifactId> <version>1.0-SNAPSHOT</version> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.11</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context-support</artifactId> <version>4.3.5.RELEASE</version> </dependency> <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjweaver</artifactId> <version>1.8.10</version> </dependency> <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjrt</artifactId> <version>1.6.1</version> </dependency> </dependencies> </project>
-
创建javabean
[WelcomeService]
public interface WelcomService { public void sayHello() ; }
[WelcomeServiceImpl]
package com.oldboy.spring.service; import org.springframework.context.annotation.Scope; import org.springframework.stereotype.Service; import javax.annotation.Resource; /** * 实现类 */ @Service("welcomeService") @Scope(value = "singleton") public class WelcomeServiceImpl implements WelcomService { private String name ; @Resource(name="byeService") private ByeService bs ; public WelcomeServiceImpl(){ System.out.println("1111"); } public WelcomeServiceImpl(String str){ System.out.println("2222"); } public WelcomeServiceImpl(String str ,Integer str2){ System.out.println("3333"); } public WelcomeServiceImpl(Integer i , String str){ System.out.println("4444"); } public WelcomeServiceImpl(Integer i , int str){ System.out.println("5555"); } public String getName() { return name; } public void setName(String name) { this.name = name; } public ByeService getBs() { return bs; } public void setBs(ByeService bs) { this.bs = bs; } public void sayHello() { bs.sayBye(); } }
[ByeService]
public interface ByeService { public void sayBye(); }
[ByeServiceImpl]
package com.oldboy.spring.service; import org.springframework.context.annotation.Scope; import org.springframework.stereotype.Service; /** */ @Service("byeService") @Scope("singleton") public class ByeServiceImpl implements ByeService { public ByeServiceImpl(){ System.out.println("new ByeServiceImpl()"); } private String bye ; public String getBye() { return bye; } public void setBye(String bye) { this.bye = bye; } public void sayBye() { System.out.println(bye); } }
-
创建配置文件
<?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="welcomeService" class="com.oldboy.spring.service.WelcomeServiceImpl" scope="prototype"> <constructor-arg type="int" value="1" index="1" /> <constructor-arg type="java.lang.Integer" value="12" index="0"/> <property name="name" value="tomas" /> <!-- 配置依赖关系 --> <property name="bs" ref="byeService" /> </bean> <!-- --> <bean id="byeService" class="com.oldboy.spring.service.ByeServiceImpl"> <property name="bye" value="byebye!!" /> </bean> </beans>
-
测试方法
public static void main(String[] args) { ApplicationContext ac = new ClassPathXmlApplicationContext("beans.xml") ; WelcomService ws = (WelcomService) ac.getBean("welcomeService"); ws.sayHello(); ws = (WelcomService) ac.getBean("welcomeService"); ws.sayHello(); ByeService bs = (ByeService) ac.getBean("byeService"); bs.sayBye(); }
3、IOC和AOP
Spring有两个核心思想,IOC和AOP。
-
IOC
inverse of control,反转控制,获得依赖对象的方式同传统方式是相反的。传统方式是从上到下按序依赖的,spring创建对象的方式是从下到上的依赖方式。并且对象的创建是有spring容器来完成。
-
AOP
aspect of control,面向切面编程,目的是不改变源代码,还为类增加新的功能。底层是通过Java的代理技术实现的。
4、AOP编程
4.1 原始编程手段
-
定义通知
package com.oldboy.spring.aop.service; import org.springframework.aop.MethodBeforeAdvice; import java.lang.reflect.Method; /** * 前置通知:方法前通知 */ public class MyMethodBeforeAdvice implements MethodBeforeAdvice{ public void before(Method method, Object[] args, Object target) throws Throwable { System.out.println("hello world!!!"); } }
-
配置文件
<?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-4.3.xsd"> <!-- 前置通知 --> <bean id="beforeAdvice" class="com.oldboy.spring.aop.service.MyMethodBeforeAdvice" /> <!-- 目标对象 --> <bean id="welcomeServiceTarget" class="com.oldboy.spring.aop.service.WelcomeServiceImpl" /> <!-- 代理对象--> <bean id="welcomeService" class="org.springframework.aop.framework.ProxyFactoryBean"> <!-- 代理接口集合 --> <property name="proxyInterfaces"> <list> <value>com.oldboy.spring.aop.service.WelcomeService</value> </list> </property> <!-- 拦截器名称集合 --> <property name="interceptorNames"> <list> <value>beforeAdvice</value> <value>afterAdvice</value> <value>aroundAdvice</value> <value>throwsAdvice</value> </list> </property> <!-- 指定目标对象 --> <property name="target" ref="welcomeServiceTarget" /> </bean> </beans>
-
测试类
public static void main(String[] args) { ApplicationContext ac = new ClassPathXmlApplicationContext("aop.xml"); WelcomeService ws = (WelcomeService) ac.getBean("welcomeService"); ws.sayHello("tomas"); }
4.2 pojo+xml方式
-
定义pojo
package com.oldboy.spring.aop2; import org.aspectj.lang.JoinPoint; /** * 观众 , pojo */ public class Audience { //坐下 public void sitdown(){ System.out.println("sitdown"); } //关机 public void turnoffCellphone(JoinPoint jp){ String method = jp.getSignature().getName(); Object[] args = jp.getArgs(); Object targ = jp.getTarget() ; Object proxy = jp.getThis() ; System.out.println("turnoffCellphone"); } public void applaud(){ System.out.println("applaud"); } public void payOff(){ System.out.println("退票"); } public void goHome(){ System.out.println("goHome"); } }
-
定义目标类和接口
public interface Actor { public void show() ; } public class Singer implements Actor{ public void show() { System.out.println("~~~~啦啦啦"); String s = null ; s.toLowerCase(); } }
-
配置文件
<?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:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd"> <!-- 观众 --> <bean id="audience" class="com.oldboy.spring.aop2.Audience" /> <!-- 歌手 --> <bean id="singer" class="com.oldboy.spring.aop2.Singer" /> <aop:config> <aop:aspect id="audienceAspect" ref="audience"> <!-- 定义切入点--> <!-- expression="execution(* com.oldboy.spring.aop2.*Service.*(..))" --> <!-- expression="execution(* *..*Service.*(..))" --> <aop:pointcut id="audiencePointCut" expression="execution(* com.oldboy.spring.aop2.Actor.show())" /> <!-- 定义通知 --> <aop:before method="sitdown" pointcut-ref="audiencePointCut" /> <aop:before method="turnoffCellphone" pointcut-ref="audiencePointCut" /> <aop:after-returning method="applaud" pointcut-ref="audiencePointCut" /> <aop:after-throwing method="payOff" pointcut-ref="audiencePointCut" /> <aop:after method="goHome" pointcut-ref="audiencePointCut"/> </aop:aspect> </aop:config> </beans>
-
测试
public static void main(String[] args) { ApplicationContext ac = new ClassPathXmlApplicationContext("aop2.xml"); Actor actor = (Actor) ac.getBean("singer"); actor.show(); }