Spring IOC AOP

IOC

控制反转
DI依赖注入
spring容器创建对象

/业务逻辑层代码
public class UserService{
 //调用dao,创建dao的对象
 private UserDao userdao;
 public void setUserDao(UserDao userDao){
  this.userDao=userDao;
 } 
 public void userRegister(User user){
  //userDao.任何方法或者属性均会空指针 
 }
<beans>   
   <bean id = "UserService" class = "UserService" singleton = "false">      <constrctor-arg>       
       <ref bean = "userDao"/>
 </constrctor-arg>  
    </bean>   
    <bean id = "userDao" class = "userDao" singleton = "false"/>
</beans>

被调用的对象的实例化对象通过构造函数或者set()方法的形式注入到调用者对象当中。

AOP

面向切面 AOP专门用于处理系统中分布在不同包,不同类,不同方法中的交叉关注点
在不改变原来业务逻辑模型的基础可以进行动态的增加日志,安全或异常处理功能。
创建接口以及实现这个接口的类。

public interface TestAOPIn{    
  public void doSomething();
  }
 public class TestAOPImpl implements TestAOPIn{  
    public void doSomething(){       System.out.println("TestAOPImp:doSomething");  
       } 
       }

配置文件,使得这个类得实例化对象可以被注入到使用这个对象得得Test类当中。

<beans>      
    <bean id = "testAOPBean" class ="org.springframework.aop.framework.ProxyFactoryBean">            
  <property name = "target">              
         <bean class = "testAOPIn" singleton = "false"/>                                     </property>       
    </bean>
</beans>

完成配置文件后,编写测试代码

public class Test{      
  public static void mian(String []args){                       ApplicationContext ctx = new FileSystemXmlAppicationContext("配置文件");             TestAOPIn t = (TestAOPIn)ctx.getBean(testAOPBean);             t.dosomething();        
        }
   }

提供了事务管理的能力
在实际开发中,比如商品查询、促销查询等业务,都需要记录日志、异常处理等操作,AOP把所有共用代码都剥离出来,单独放置到某个类中进行集中管理,在具体运行时,由容器进行动态织入这些公共代码。

注解

<!--aop的实现有两种 xml[事务]  注解[日志]-->
    <!--aop:before 是前置通知-->
    <!--pointcut切点 需要一个aspect表达式-->
    <!--execution(访问修饰符 返回值 包 类 方法(参数)) 执行目标类中的方法-->
    <!--execution( public   void com.oracle.service.UserInfoService.sayHello() )-->
    <!--execution()可以使用通配符配置 *-->
    <!--execution(* com.oracle..*.*.*(..));-->
   <aop:config>
       <aop:aspect  id="logasp" ref="loggerBean">
           <!--切入点-->
           <aop:pointcut id="userServiceSayHello" expression="execution(* com.oracle..*.*.*(..))"/>
           <!--五种通知类型-->
           <!--前置通知-->
           <aop:before method="before"  pointcut-ref="userServiceSayHello"  />
           <!--后置通知-->
           <aop:after-returning method="returning" pointcut-ref="userServiceSayHello"     />
           <!--最终通知-->
           <aop:after method="after" pointcut-ref="userServiceSayHello"    />
           <!--环绕通知-->
           <aop:around method="around" pointcut-ref="userServiceSayHello"   />
           <!--异常通知-->
           <!--<aop:after-throwing method="throwing" pointcut-ref="userServiceSayHello"     />-->
       </aop:aspect>
 </aop:config>

spring bean 生命周期

  • 实例化一个bean,new
  • spring 上下文对实例化的bean进行配置,ioc注入
  • bean实现BeanNameAware接口,会调用setBeanName(String) 方法,传递bean的id值
  • BeanNameAware实现,传递spring工厂本身
  • ApplicationContextAware实现,调用setApplicationContext方法,传入spring上下文
  • postProcessBeforeInitialization接口实现-初始化预处理
  • init-method
  • 如果bean关联了BeanPostProcessor接口,调用postProcessAfterInitializaion方法
  • destory过期自动清理
  • destory-method自配置清理
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值