Spring框架的作用:为了简化java开发
Spring是一个开源框架,Spring是于2003 年兴起的一个轻量级的Java 开发框架,由Rod Johnson 在其著作Expert One-On-One J2EE Development and Design中阐述的部分理念和原型衍生而来。它是为了解决企业应用开发的复杂性而创建的
一.IOC(控制反转)
1 构造器注入
(xml配置)
2方法注入
(注解方式)
(xml配置)
二,AOP(面向切面)
注解方式
xml配置
只是使用,那简单,原理后面补上
代码架构
spring-test.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:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd "> <!-- 注解支持,为了让我们可以使用注解创建bean --> <context:annotation-config /> <!--设置要扫描的包的位置 --> <context:component-scan base-package="com.eyugame.test" /> <!-- 支持aop注解 --> <aop:aspectj-autoproxy /> <aop:config > <!-- 配置aop触发调用的类是myService也就是下面对应的MyServiceImpl --> <aop:aspect id="spect" ref="myAop"> <!-- 配置调用com.eyugame.test.MyBean的toDo()方法的时候触发aop --> <aop:pointcut id="target" expression="execution(* com.eyugame.test.MyBean.toDo(..))" /> <!-- 执行前调用,调用方法为doBefore --> <aop:before pointcut-ref="target" method="doBefore()" /> <!-- 执行后调用,调用方法为doAfter --> <aop:after pointcut-ref="target" method="doAfter()" /> <!-- 返回时调用,调用方法doReturn --> <aop:after-returning pointcut-ref="target" method="doReturn()" /> <!-- 抛出异常时候调用,调用方法doReturn --> <aop:after-throwing pointcut-ref="target" method="doThrowing()" throwing="ex" /> <!-- 注意 aop:aroup在新版本spring好像失效,坑我 --> <!-- <aop:around pointcut-ref="businessService" method="doAround"/> --> </aop:aspect> </aop:config> <bean id="myAop" class="com.eyugame.test.MyAop"></bean> <bean id="depedecyBean" class="com.eyugame.test.DependencyBean"></bean> <!-- 依赖注入1构造器注入 --> <bean id="myBean" class="com.eyugame.test.MyBean"> <constructor-arg ref="depedecyBean"></constructor-arg> </bean> <!-- 依赖注入2设值注入 --> <bean id="myBean2" class="com.eyugame.test.MyBean2"> <!-- 名称要跟类里面要注入的类的名字一致 --> <property name="iDependency" ref="depedecyBean"></property> </bean> <!-- 注解Aop --> <bean id="myAop2" class="com.eyugame.test.MyAop2"></bean> </beans>依赖注入时的接口
package com.eyugame.test; public interface IDependency { void show(); }依赖注入的具体类
package com.eyugame.test; public class DependencyBean implements IDependency { public void show() { System.out.println("my class name:" + this.getClass()); } }AOP类
package com.eyugame.test; public class MyAop { public MyAop() { }; public void doBefore() { System.out.println("执行前调用"); } public void doAfter() { System.out.println("执行后调用"); } public void doReturn() { System.out.println("结束时候调用"); } public void doThrowing() { System.out.println("有异常时候调用"); } }
package com.eyugame.test; import org.aspectj.lang.annotation.After; import org.aspectj.lang.annotation.AfterReturning; import org.aspectj.lang.annotation.AfterThrowing; import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.Before; import org.aspectj.lang.annotation.Pointcut; /*注解AOP*/ @Aspect public class MyAop2 { public MyAop2() { }; @Pointcut(value= "execution(* com.eyugame.test.MyBean2.toDo(..))") private void init() { } @Before("execution(* com.eyugame.test.MyBean2.toDo(..))") public void doBefore() { System.out.println("执行前调用"); } @After("execution(* com.eyugame.test.MyBean2.toDo(..))") public void doAfter() { System.out.println("执行后调用"); } @AfterReturning("execution(* com.eyugame.test.MyBean2.toDo(..))") public void doReturn() { System.out.println("结束时候调用"); } @AfterThrowing("execution(* com.eyugame.test.MyBean2.toDo(..))") public void doThrowing() { System.out.println("有异常时候调用"); } }
package com.eyugame.test; public class MyBean { private IDependency iDependency; public MyBean(){ } /*依赖注入1:构造器注入*/ public MyBean(IDependency iDependency){ this.iDependency=iDependency; } public void toDo() { this.iDependency.show(); } }
package com.eyugame.test; /*方法注入*/ public class MyBean2 { private IDependency iDependency; public MyBean2() { } public void toDo() { this.iDependency.show(); } /* 依赖注入2:之设值注入 */ public void setiDependency(IDependency iDependency) { this.iDependency = iDependency; } }注解创建的Bean
package com.eyugame.test; import org.springframework.stereotype.Component; @Component("myBean3") public class MyBean3 { public void show() { System.out.println("my name is MyBean3"); } }
import com.eyugame.test.MyBean; import com.eyugame.test.MyBean2; import com.eyugame.test.MyBean3; public class TestSpring { @SuppressWarnings("resource") public static void main(String[] args) { ApplicationContext context = new ClassPathXmlApplicationContext("config/spring/spring-test.xml"); System.out.println("使用构造器注入和配置aop-------------"); /*依赖注入一:构造器注入|aop测试*/ MyBean myBean = context.getBean("myBean",MyBean.class); myBean.toDo(); System.out.println("使用方法注入和注解aop-----------"); /*依赖注入二:方法注入*/ MyBean2 myBean2 = context.getBean("myBean2",MyBean2.class); myBean2.toDo(); /*使用注解创建bean*/ System.out.println("使用注解创建bean-----------"); MyBean3 myBean3=context.getBean("myBean3",MyBean3.class); myBean3.show(); } }
依赖的包
<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/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>Test</groupId> <artifactId>test2</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>test2</name> <url>http://maven.apache.org</url> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <!--各个jar的版本 --> <spring.version>3.2.3.RELEASE</spring.version> <junit.version>4.11</junit.version> <jsonlib.version>2.4</jsonlib.version> <shiro.version>1.2.0</shiro.version> </properties> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>${junit.version}</version> <scope>test</scope> </dependency> <!-- Spring jars --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-beans</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aop</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjweaver</artifactId> <version>1.8.5</version> </dependency> <!-- <dependency> <groupId> org.aspectj</groupId> <artifactId> aspectjweaver</artifactId> <version> 1.6.11</version> </dependency> --> </dependencies> </project>
Spring学习一:IOC(控制反转)和AOP(面向切面)的xml配置和注解方式
原文地址:http://blog.csdn.net/h348592532/article/details/45567611