Spring顾问、IOC注解和注解增强
一、顾问
通知的一种表现方式(顾问包装通知/增强)
Advisor:
名称匹配方法:
NameMecthMethodPointcutAdvisor
1.定义了一个业务类
package cn.spring.advisor;
/**
* 业务接口
*/
public interface IService {
//业务方法
public void doSome(); public void say();
}
2、定义里一个增强类,实现了增强接口
package cn.spring.advisor;
public class IServiceImpl implements IService{
@Override
public void doSome() {
System.out.println("=============真实业务=============");
}
@Override
public void say() {
System.out.println("=============say=============");
}
}
3.applicationContext.xml
将业务类和增强类注入到Spring容器当中
<!--注入业务Bean-->
<bean id="iServiceImpl" class="cn.spring.advisor.IServiceImpl"></bean>
<!--切面/通知-->
<bean id="myAdvisor" class="cn.spring.advisor.MyAdvisor"></bean>
利用顾问包装增强
<bean id="advisor" class="org.springframwork.aop.support.NameMecthMethodPointcutAdvisor">
<property name="advice" ref="MyAdvisor"/>
<property name="mapperNames" value="*do*"/>
</bean>
利用代理工程生成代理对象
<bean id="proxyFactory" class="ProxyFactoryBean">
<property name="in" value="advisor"/>
<property name="target" ref="IService"/>
</bean>
顾问自动代理:
默认顾问自动代理
applicationContext.xml
1.定义了一个业务类
2.定义了一个增强类 实现 增强接口
3.applicationContext.xml
3.1将业务类和增强类注入到Spring容器当中
3.2利用顾问包装增强
<bean id="advisor" class="org.springframwork.aop.support.RegrexMethodPointcutAdvisor">
<property name="advice" ref="MyAdvisor"/>
<property name="pattens" value=".*do.*"/>
</bean>
3.3顾问自动代理
<bean class="DefaultAdvisorAutoProxyCreator"/>
BeanName顾问自动代理
.定义了一个业务类
.定义了一个增强类 实现 增强接口
.applicationContext.xml
.1将业务类和增强类注入到Spring容器当中
.2利用顾问包装增强
<bean id="advisor" class="org.springframwork.aop.support.RegrexMethodPointcutAdvisor">
<property name="advice" ref="MyAdvisor"/>
<property name="pattens" value=".*do.*"/>
</bean>
.3顾问自动代理
<bean class="BeanNameAutoProxyCreator">
<property name="beanNames" value="iService"/>
<property name="Inters" value="advisor"/>
</bean>
二.IOC注解
IOC注解需要需要在大配置文件中,扫描包 <context:compoent-scan base-package="cn.spring"/>
注入:向Spring容器注入Bean的
@Compoent 实现业务Bean的注入
@Repository 专门注入Dao 基于@Compoent
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Component
public @interface Repository {
/**
* The value may indicate a suggestion for a logical component name,
* to be turned into a Spring bean in case of an autodetected component.
* @return the suggested component name, if any (or empty String otherwise)
*/
@AliasFor(annotation = Component.class)
String value() default "";
}
@Service 专门注入Service
@Controller 专门定位控制层
定位:
@Resource 默认根据Bean名称定位,如果名称为空,则根据Type定位
@Autowired 默认根据ByType注入 一旦出现多个兼容类型的,那么我们需要根据名称区分@Qualifier
案例:
Dao层接口
public interface IUserInfoMapper {
public int addUser(UserInfo info);
}
IUserInfoMapperImpl类实现了Dao接口
@Repository
public class IUserInfoMapperImpl implements IUserInfoMapper { @Override
public int addUser(UserInfo info) {
System.out.println("添加成功");
return ;
}
}
IUserInfoService接口
public interface IUserInfoService {
public int addUser(UserInfo info);
}
IUserInfoServiceImpl类实现了IUserInfoService接口
/**
* @Service代表Service实现类的 标识,要让Spring管理Service
*/
@Service("iUserInfoServiceImpl")
public class IUserInfoServiceImpl implements IUserInfoService{ //植入Dao层
//@Resource默认是根据ByName的方式,但是一旦名字为空,就根据ByType
@Autowired
private IUserInfoMapper iUserInfoMapper; @Override
public int addUser(UserInfo info) {
return iUserInfoMapper.addUser(info);
}
}
applicationContext.xml
<!--扫描注解:包扫描器-->
<context:component-scan base-package="cn.spring"/>
三、注解增强
业务类
package cn.spring.aop; import org.springframework.stereotype.Service; /**
* 业务类
*/
@Service("IdoSomeService")
public class IdoSomeService {
public void doSome(){
System.out.println("业务当中的doSome方法");
} public void say(){
System.out.println("业务当中的say方法");
}
}
增强类
package cn.spring.aop; import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component; /**
* 增强类
*/
@Aspect
@Component
public class MyAdvice { //定义一个空方法,为了可以应用切点表达式
@Pointcut("execution(* *..aop.*.do*(..))")
public void pointCut(){ } //自定义增强方法
@Before("pointCut()")
public void before(){
System.out.println("=================前置增强===================");
} //自定义方法增强
@AfterReturning("pointCut()")
public void after(){
System.out.println("===============后置增强==============");
}
}
applicationContext.xml
<!--开启AOP注解支持-->
<aop:aspectj-autoproxy/>
Test测试类
public class AopTest {
public static void main(String[] args) {
ApplicationContext ctx=new ClassPathXmlApplicationContext("applicationContext.xml");
IdoSomeService bea=(IdoSomeService)ctx.getBean("IdoSomeService");
bea.doSome();
}
}
Spring顾问、IOC注解和注解增强的更多相关文章
- Spring 的IOC容器之注解的方式
1. 环境搭建 1.1 导入所需 jar 包 引入 IOC 容器必须的6个jar包; spring-aop-4.3.10.RELEASE.jar, Spring 框架的AOP的jar包; 1.2 创建 ...
- Spring框架(3)---IOC装配Bean(注解方式)
IOC装配Bean(注解方式) 上面一遍文章讲了通过xml来装配Bean,那么这篇来讲注解方式来讲装配Bean对象 注解方式需要在原先的基础上重新配置环境: (1)Component标签举例 1:导入 ...
- Spring系列(三):Spring IoC中各个注解的理解和使用
原文链接:1. http://www.cnblogs.com/xdp-gacl/p/3495887.html 2. http://www.cnblogs.com/xiaoxi/p/5935 ...
- Spring IoC中各个注解的理解和使用
一.把在Spring的xml文件中配置bean改为Spring的注解来配置bean 传统的Spring做法是使用.xml文件来对bean进行注入或者是配置aop.事物,这么做有两个缺点:1.如果所有的 ...
- 死磕Spring之IoC篇 - BeanDefinition 的解析过程(面向注解)
该系列文章是本人在学习 Spring 的过程中总结下来的,里面涉及到相关源码,可能对读者不太友好,请结合我的源码注释 Spring 源码分析 GitHub 地址 进行阅读 Spring 版本:5.1. ...
- Spring IOC之基于注解的容器配置
Spring配置中注解比XML更好吗?基于注解的配置的介绍提出的问题是否这种途径比XML更好.简单来说就是视情况而定. 长一点的答案是每一种方法都有自己的长处也不足,而且这个通常取决于开发者决定哪一种 ...
- (转)java之Spring(IOC)注解装配Bean详解
java之Spring(IOC)注解装配Bean详解 在这里我们要详细说明一下利用Annotation-注解来装配Bean. 因为如果你学会了注解,你就再也不愿意去手动配置xml文件了,下面就看看 ...
- Spring的IOC注解开发入门2
注解方式设置属性的值 在我们IOC基于xml属性注入的方式中有(一般推荐set方法) 构造方法注入普通值:<constructor-arg>的使用 set方法注入普通值:<prope ...
- Spring总结四:IOC和DI 注解方式
首先我们要了解注解和xml配置的区别: 作用一样,但是注解写在Bean的上方来代替我们之前在xml文件中所做的bean配置,也就是说我们使用了注解的方式,就不用再xml里面进行配置了,相对来说注解方式 ...
- 七 Spring的IOC的注解方式
Spring的IOC的注解方式入门 引入注解约束,配置组件扫描 类上的注解: @Conponent @Controller @Service @Repository 普通属性的注解 @value ...
随机推荐
- (二)linux 学习 -- 探究操作系统
The Linux Command Line 读书笔记 - 部分内容来自 http://billie66.github.io/TLCL/book/chap04.html 文章目录 ls 命令进阶 `l ...
- Ubuntu 18.04安装arm-linux-gcc交叉编译器
Ubuntu 18.04安装arm-linux-gcc交叉编译器
- netty--buffer分配策略
AdaptiveRecvByteBufAllocator 动态分配buffer大小的类. 如果前一次读取完全填满了分配的缓冲区,它将逐渐增加预期的可读字节数.(增加的方式:初始化类的时候,会预先设置好 ...
- SOS dp
设$ans=\sum\limits_{A \cap B=\varnothing} f(A)g(B) $ 直接暴力枚举子集是$O(3^n)$, 一个技巧是先预处理出$h(S)=\sum\limits_{ ...
- AtCoder Grand Contest 034
A:如果C在D左侧,显然先让B到达终点再让A走即可,否则先判断一下A是否可以在某处超过B.也就是先判断一下起点与终点之间是否有连续的障碍,若有则无解:然后若C在D左侧输出Yes,否则判断B和D之间是否 ...
- RuntimeError: Model class users.models.UserProfile doesn't declare an explicit app_label and isn't in an application in INSTALLED_APPS.
Django启动的时候报错 File "/home/hehecat/PycharmProjects/MxShop/MxShop/urls.py", line 23, in from ...
- js按钮频繁提交解决方案:
1.封装js: /// 函数节流 xhz.canRun = true; xhz.Throttling = function () { if (!xhz.canRun) { layer.msg('处理中 ...
- openstack-neutron(2)
VXLAN 独立于底层的网络拓扑:反过来,两个 VTEP 之间的底层 IP 网络也独立于 VXLAN.VXLAN 数据包是根据外层的 IP header 路由的,该 header 将两端的 VTEP ...
- nginx代理(正向代理和反向代理)
正向代理 是一个位于客户端和原始服务器(origin server)之间的服务器,为了从原始服务器取得内容,客户端向代理发送一个请求并指定目标(原始服务器),然后代理向原始服务器转交请求并将获得的内容 ...
- GitHub开源的10个超棒后台管理面板
目录1.AdminLTE 2.vue-Element-Admin 3.tabler 4.Gentelella 5.ng2-admin 6.ant-design-pro 7.blur-admin 8.i ...