spring学习笔记(2)

spring对AOP的支持

1、如果目标对象实现了接口,默认情况下会采用JDK的动态代理实现AOP
2、如果目标对象实现了接口,可以强制使用CGLIB实现AOP
3、如果目标对象没有实现了接口,必须采用CGLIB库,spring会自动在JDK动态代理和CGLIB之间转换

如何强制使用CGLIB实现AOP?
 * 添加CGLIB库,SPRING_HOME/cglib/*.jar
 * 在spring配置文件中加入<aop:aspectj-autoproxy proxy-target-class="true"/>
 
JDK动态代理和CGLIB字节码生成的区别?
 * JDK动态代理只能对实现了接口的类生成代理,而不能针对类
 * CGLIB是针对类实现代理,主要是对指定的类生成一个子类,覆盖其中的方法
   因为是继承,所以该类或方法最好不要声明成final 
实例如下:
(1):实现类:
package com.jiangfeng.spring;

public class UserManagerImpl { // implements UserManager {

 public void addUser(String username, String password) {
  System.out.println("-------UserManagerImpl.addUser()----------");
 }

 public void deleteUser(int id) {
  System.out.println("-------UserManagerImpl.deleteUser()----------");
 }

 public String findUserById(int id) {
  System.out.println("-------UserManagerImpl.findUserById()----------");
  return null;
 }

 public void modifyUser(int id, String username, String password) {
  System.out.println("-------UserManagerImpl.modifyUser()----------");
 }
}

(2):
package com.jiangfeng.spring;

import org.aspectj.lang.JoinPoint;

public class SecurityHandler {
 
 private void checkSecurity(JoinPoint joinPoint) {
  Object[] args = joinPoint.getArgs();
  for (int i=0; i<args.length; i++) {
   System.out.println(args[i]);
  }
  
  System.out.println(joinPoint.getSignature().getName());
  System.out.println("----------checkSecurity()---------------");
 }
 
}
(3)测试:
package com.jiangfeng.spring;

import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Client {

 public static void main(String[] args) {
  BeanFactory factory = new ClassPathXmlApplicationContext("applicationContext.xml");
  
  //UserManager userManager = (UserManager)factory.getBean("userManager");
  
  UserManagerImpl userManager = (UserManagerImpl)factory.getBean("userManager");
  userManager.addUser("张三", "123");
  //userManager.deleteUser(1);
 }
}
(4)配置文件:
<?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:tx="http://www.springframework.org/schema/tx"
      xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
           http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
           http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd">
 <!--
 <aop:aspectj-autoproxy proxy-target-class="true"/>
  -->
 <bean id="securityHandler" class="com.jiangfeng.spring.SecurityHandler"/>          
 
 <bean id="userManager" class="com.jiangfeng.spring.UserManagerImpl"/>
 
 <aop:config>
  <aop:aspect id="security" ref="securityHandler">
   <aop:pointcut id="allAddMethod" expression="execution(* com.jiangfeng.spring.UserManagerImpl.add*(..))"/>
   <aop:before method="checkSecurity" pointcut-ref="allAddMethod"/>
  </aop:aspect>
 </aop:config> 
</beans>

输出结果:
2009-10-22 01:04:55,687 INFO [org.springframework.core.CollectionFactory] - JDK 1.4+ collections available
2009-10-22 01:04:55,750 INFO [org.springframework.beans.factory.xml.XmlBeanDefinitionReader] - Loading XML bean definitions from class path resource [applicationContext.xml]
2009-10-22 01:04:56,046 INFO [org.springframework.context.support.ClassPathXmlApplicationContext] - Bean factory for application context [org.springframework.context.support.ClassPathXmlApplicationContext;hashCode=10748354]: org.springframework.beans.factory.support.DefaultListableBeanFactory defining beans [securityHandler,userManager,org.springframework.aop.config.internalAutoProxyCreator,allAddMethod,org.springframework.aop.aspectj.AspectJPointcutAdvisor]; root of BeanFactory hierarchy
2009-10-22 01:04:56,062 INFO [org.springframework.context.support.ClassPathXmlApplicationContext] - 5 beans defined in application context [org.springframework.context.support.ClassPathXmlApplicationContext;hashCode=10748354]
2009-10-22 01:04:56,078 INFO [org.springframework.aop.framework.DefaultAopProxyFactory] - CGLIB2 available: proxyTargetClass feature enabled
2009-10-22 01:04:56,140 INFO [org.springframework.context.support.ClassPathXmlApplicationContext] - Bean 'org.springframework.aop.config.internalAutoProxyCreator' is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
2009-10-22 01:04:56,140 INFO [org.springframework.context.support.ClassPathXmlApplicationContext] - Unable to locate MessageSource with name 'messageSource': using default [org.springframework.context.support.DelegatingMessageSource@15a0305]
2009-10-22 01:04:56,140 INFO [org.springframework.context.support.ClassPathXmlApplicationContext] - Unable to locate ApplicationEventMulticaster with name 'applicationEventMulticaster': using default [org.springframework.context.event.SimpleApplicationEventMulticaster@17653ae]
2009-10-22 01:04:56,156 INFO [org.springframework.beans.factory.support.DefaultListableBeanFactory] - Pre-instantiating singletons in factory [org.springframework.beans.factory.support.DefaultListableBeanFactory defining beans [securityHandler,userManager,org.springframework.aop.config.internalAutoProxyCreator,allAddMethod,org.springframework.aop.aspectj.AspectJPointcutAdvisor]; root of BeanFactory hierarchy]
张三
123
addUser
----------checkSecurity()---------------
-------UserManagerImpl.addUser()----------

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring是一个开源的Java框架,用于构建企业级应用程序。它提供了一种轻量级的、非侵入式的开发方式,通过依赖注入和面向切面编程等特性,简化了Java应用程序的开发过程。 以下是关于Spring学习的一些笔记: 1. IoC(控制反转):Spring通过IoC容器管理对象的创建和依赖关系的注入。通过配置文件或注解,将对象的创建和依赖关系的维护交给Spring容器来管理,降低了组件之间的耦合度。 2. DI(依赖注入):Spring通过依赖注入将对象之间的依赖关系解耦。通过构造函数、Setter方法或注解,将依赖的对象注入到目标对象中,使得对象之间的关系更加灵活和可维护。 3. AOP(面向切面编程):Spring提供了AOP的支持,可以将与业务逻辑无关的横切关注点(如日志、事务管理等)从业务逻辑中分离出来,提高了代码的可重用性和可维护性。 4. MVC(模型-视图-控制器):Spring提供了一个MVC框架,用于构建Web应用程序。通过DispatcherServlet、Controller、ViewResolver等组件,实现了请求的分发和处理,将业务逻辑和视图展示进行了分离。 5. JDBC和ORM支持:Spring提供了对JDBC和ORM框架(如Hibernate、MyBatis)的集成支持,简化了数据库访问的操作,提高了开发效率。 6. 事务管理:Spring提供了对事务的支持,通过声明式事务管理和编程式事务管理,实现了对数据库事务的控制和管理。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值