Spring学习笔记2016-3-1

一,通过在classpath自动扫描方式把组件纳入Spring容器中管理

他可以在类路径底下寻找标注了@component、@Service、@Control、@Reposltory注解的类,并把这些类纳入进Spring容器中管理,他的作用和在xml文件中配置bean节点一样,

①  如何使用

在beans.xml中打开以下配置信息

<beansxmlns="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/contexthttp://www.springframework.org/schema/context/spring-context-2.5.xsd">

        

          <context:component-scanbase-package="cn.luo"/>

</beans>

其中base-package="cn.luo"为需要扫描的包(含子包),@Service用于标记业务层组件,@Control用于标记控制层组件,@Reposltory用于标记数据访问层组件,即DAO组件,@component泛指组件,当组件不好归类时,用于标记

注意的是:<bean id=””></bean>中的id名是采用注解的类的类名的前一个字母小写

@service(“personService”)括号里为修改的id名称,@Scope”prototype”)用于指定bean的生命周期

二、AOP面向方面编程,多用于权限系统

1.AOP代理对象

AOP框架的权限拦截测试

注意的是:业务层实现类的方法需要权限才能访问,则必须实现接口

Personservice接口

publicinterface PersonService {

public void save(String name);

public void update(String name, Integerpersonid);

public String getPersonName(Integer personid);

}

Personserviceimpl实现类

publicclass PersonServiceBean implements PersonService{

private String user = null;

public String getUser() {

           return user;

}

public PersonServiceBean(){}

public PersonServiceBean(String user){

           this.user = user;

}

public String getPersonName(Integer personid) {

           System.out.println("我是getPersonName()方法");

           return "xxx";

}

public void save(String name) {

           System.out.println("我是save()方法");

}

public void update(String name, Integerpersonid) {

           System.out.println("我是update()方法");

}

 

}

当调用Personserviceimp实例中的方法时,只要当传入的user不为空时,才能执行业务方法,

创建代理对象ProxyPersonserviceimp则为目标对象,通过访问代理对象,在代理对象中判断user是否为空,不为空则执行目标对象的方法

Public class JDKProxyFactory implementsInvocationHandler{   //代理对象必须实现InvocationHandler接口

private Object targetObject;  // 目标对象

public Object createProxyIntance(ObjecttargetObject){ // 创建代理对象实例

           this.targetObject = targetObject;

           returnProxy.newProxyInstance(this.targetObject.getClass().getClassLoader(),

                             this.targetObject.getClass().getInterfaces(),this);

}

Public Object invoke (Object proxy,Method method,Object[]args)throws Throvable{

  PersonServiceimplpserimpl =( PersonServiceimpl) this. targetObject将目标对象转为PersonServiceimpl实例

Object result = null

If(pserimpl.getUser()!=null){

           Result =method.invoke(targetObject,args)

}

Return result;

}

}

单元测试中

publicvoid proxyTest(){

           JDKProxyFactory factory = newJDKProxyFactory();

           PersonService service =(PersonService) factory.createProxyIntance(new PersonServiceimpl("xxx"));

           service.save("888");

}

②  需要拦截的类没有实现接口呢

采用CGLIB类

③  Aspect切面

④  Joinpoint连接点,是指被拦截到的点,在spring中,这些点指的是每一个方法

⑤  Pointcut切入点,是指我们堆哪些连接点进行拦截的定义,例如拦截所有业务方法

⑥  Advice通知,就是指拦截到joinpoint之后需要做的事,分为前置通知,后置通知,异常通知,最终通知,环绕通知,

⑦  Target目标对象

⑧  Wcave织入 将切面应用到目标对象并导致proxy代理对象创建的过程称为织入

三、使用Spring进行面向切面AOP编程:基于注解类的AOP开发

①首先要在Spring配置文件中引入aop命名空间

xmlns:aop=http://www.springframework.org/schema/aop

http://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop-2.5.xsd

②将一个类定义为切面,采用注解方式

@Aspect                               代表所有类    

Public classMyInterceptor{         包名      代表所有方法

@PointCut(“execution{*com.luo.service..*.*{..}}”)

}                                 两点代表包下的子包进行拦截

          执行  返回值类型,所有返回值的方法

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值