Spring 总结02
8、依赖注入--手工装配—注解方式
在java代码中使用@Autowired或@Resource注解方式进行装配的前提条件是。
1、引入context命名空间 需要在xml配置文件中配置以下信息:
<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-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd">
<context:annotation-config/>
</beans>
2、在配置文件中添加context:annotation-config标签
<context:annotation-config/>
这个配置隐式注册了多个对注释进行解析处理的处理器
AutowiredAnnotationBeanPostProcessor,CommonAnnotationBeanPostProcessor,
PersistenceAnnotationBeanPostProcessor,RequiredAnnotationBeanPostProcessor
注: @Resource注解在spring安装目录的lib\j2ee\common-annotations.jar
在java代码中使用@Autowired或@Resource注解方式进行装配,这两个注解的区别是:@Autowired 默认按类型装配,@Resource默认按名称装配,当找不到与名称匹配的bean才会按类型装配。
@Autowired注解是按类型装配依赖对象,默认情况下它要求依赖对象必须存在,如果允许null值,可以设置它required属性为false。
*****************************************************************************************
通过在classpath自动扫描方式把组件纳入spring容器中管理
spring2.5为我们引入了组件自动扫描机制,它可以在类路径底下寻找标注了@Component、@Service、@Controller、@Repository注解的类,并把这些类纳入进spring容器中管理。它的作用和在xml文件中使用bean节点配置组件是一样的。
要使用自动扫描机制,我们需要打开以下配置信息:
1、引入context命名空间 需要在xml配置文件中配置以下信息:
<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-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd">
<context:component-scan base-package="xxx.xxx"/>
</beans>
其中base-package为需要扫描的包(含子包)。
注:
1、在使用组件扫描元素时,AutowiredAnnotationBeanPostProcessor 和CommonAnnotationBeanPostProcessor会隐式地被包括进来。 也就是说,连个组件都会被自动检测并织入 - 所有这一切都不需要在XML中提供任何bean配置元数据。
2、功能介绍
@Service用于标注业务层组件、
@Controller用于标注控制层组件(如struts中的action)、
@Repository用于标注数据访问组件,即DAO组件。
而@Component泛指组件,当组件不好归类的时候,我们可以使用这个注解进行标注。
9、AOP
********spring在运行期创建代理,不需要特殊的编译器.
spring有两种代理方式:
1.若目标对象实现了若干接口,spring使用JDK的java.lang.reflect.Proxy类代理。
2.若目标对象没有实现任何接口,spring使用CGLIB库生成目标对象的子类。
使用该方式时需要注意:
1.对接口创建代理优于对类创建代理,因为会产生更加松耦合的系统。
对类代理是让遗留系统或无法实现接口的第三方类库同样可以得到通知,
这种方式应该是备用方案。
2.标记为final的方法不能够被通知。spring是为目标类产生子类。任何需要
被通知的方法都被复写,将通知织入。final方法是不允许重写的
**************************************************************************
Aspect(切面): 是通知和切入点的结合,通知和切入点共同定义了关于切面的全部内容---它的功能、在何时和何地完成其功能
joinpoint(连接点):所谓连接点是指那些被拦截到的点。在spring中,这些点指的是方法,因为spring只支持方法类型的连接点.
Pointcut(切入点):所谓切入点是指我们要对那些joinpoint进行拦截的定义.通知定义了切面的”什么”和”何时”,切入点就定义了”何地”.
Advice(通知):所谓通知是指拦截到joinpoint之后所要做的事情就是通知.通知分为前置通知,后置通知,异常通知,最终通知,环绕通知(切面要完成的功能)
Target(目标对象):代理的目标对象
Weaving(织入):是指把切面应用到目标对象来创建新的代理对象的过程.切面在指定的连接点织入到目标对象
Introduction(引入):在不修改类代码的前提下, Introduction可以在运行期为类动态地添加一些方法或Field.
******************************************************************************
要进行AOP编程,首先我们要在spring的配置文件中引入aop命名空间:
<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-2.5.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">
</beans>
Spring提供了两种切面使用方式,实际工作中我们可以选用其中一种:
1、基于XML配置方式进行AOP开发。
2、基于注解方式进行AOP开发。
*******************************************************************************
基于xml配置方式:
切面类定义如下:
public class Security{//切面
public void checkSecurity(){ //通知
System.out.println("进行安全性检查");
}
}
配置文件如下:
<bean id="security" class="com.wangjie.service.Security"/><!-- 定义切面对象 -->
<bean id="userManager" class="com.wangjie.service.UserManagerImpl"/><!--创建接口实现类对象-->
<aop:config> <!--所有的切面和通知都必须定义在aop:config元素内部 -->
<aop:aspect ref="security"><!-- 声明切面 -->
<!-- 声明切入点 -->
<aop:pointcut id="userManagerPointcut"
expression="execution(* com.wangjie.service..*.save*(..))"/>
<!--声明后置通知,在匹配的方法完全执行后运行-->
<aop:after-returning method="checkSecurity"
pointcut-ref="userManagerPointcut"/>
<aop:aspect>
</aop:config>
***************************************************************************************
基于注解方式
为了在Spring配置中使用@AspectJ切面,你首先必须启用Spring对@AspectJ切面配置的支持,并确保自动代理
<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-2.5.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.5.xsd”>
<!--启用Spring对@AspectJ的支持 -->
<aop:aspectj-autoproxy/>
<!-- 声明切面对象 -->
<bean id="security" class="com.itcast.service.Security" />
<!-- 创建接口实现类对象 -->
<bean id="userManager" class="com.itcast.service.UserManagerImpl" />
</beans>
***************************************************
@Aspect //声明切面
public class Security {
/**
* @Pointcut 用于声明切入点
* 在@AspectJ注解风格的AOP中 一个切入点签名通过一个普通的方法来定义
* 1 作为切入点签名的方法必须返回void类型
* 2 方法没有参数 用private修饰
* 3 方法体为空
*
* 切入点表达式的写法
* execution(主要)表示匹配方法执行的连接点
* 例如:
* com.itcast.service..*.save*(..))
* 1 "*" 表示方法的返回类型任意
* 2 com.itcast.service..
* 表示service包及其子包中所有的类
* 3 .save* 表示类中所有以save开头的方法
* 4 (..) 表示参数是任意数量
*/
@Pointcut("execution(* com.itcast.service..*.save*(..))")
private void userManagerPointut(){}
/**
* Before 前置通知 在方法调用前执行
* userManagerPointut() 表示前面定义的切入点
* args - 限定匹配特定的连接点(使用Spring AOP的时候方法的执行),其中参数是指定类型的实例.
* username,psw 参数表示指定类型的实例
* * 参数名称必须与通知的参数名称一致
* * 通知参数的类型必须与目标对象参数的类型一致,如不一致,拦截不到
*/
@Before("userManagerPointut() && args(username1,psw)")
public void checkSecurity(String username1,String psw){
System.out.println("进行安全检查 "+username1+" "+psw);
}
}