4.spring:@Profile,AOP
Profile:
db.user=root
db.password=
db.jdbcUrl=jdbc:mysql://localhost:3306/users
db.drivetClass=com.mysql.jdbc.Driver
@PropertySource("classpath:/db.properties")
@Configuration
public class MainConfigProfile {
@Value("${db.user}")
private String user;
@Value("${db.password}")
private String pwd;
@Value("${db.jdbcUrl}")
private String jdbcUrl;
@Value("${db.drivetClass}")
private String DriverClass;
@Profile("test")
@Bean("testDatasource")
public ComboPooledDataSource datasource() throws Exception{
ComboPooledDataSource datasource = new ComboPooledDataSource();
datasource.setUser(user);
datasource.setPassword(pwd);
datasource.setJdbcUrl(jdbcUrl);
datasource.setDriverClass(DriverClass);
return datasource;
}
@Profile("dev")
@Bean("devDatasource")
public ComboPooledDataSource datasource1() throws Exception{
ComboPooledDataSource datasource = new ComboPooledDataSource();
datasource.setUser(user);
datasource.setPassword(pwd);
datasource.setJdbcUrl("jdbc:mysql://localhost:3306/ssm");
datasource.setDriverClass(DriverClass);
return datasource;
}
}
@Test
public void test7(){
//创建一个application
AnnotationConfigApplicationContext app =
new AnnotationConfigApplicationContext();
//设置环境
app.getEnvironment().setActiveProfiles("dev");
//注册配置类
app.register(MainConfigProfile.class);
//启动刷新容器
app.refresh(); String[] names = app.getBeanDefinitionNames();
for(String name : names){
System.out.println(name);
}
}
mainConfigProfile
devDatasource
@Profile("default")
@Bean("devDatasource")
public ComboPooledDataSource datasource1() throws Exception{
ComboPooledDataSource datasource = new ComboPooledDataSource();
datasource.setUser(user);
datasource.setPassword(pwd);
datasource.setJdbcUrl("jdbc:mysql://localhost:3306/ssm");
datasource.setDriverClass(DriverClass);
return datasource;
}
AOP:
//切面类
@Aspect
public class LogAspects { //抽取公共的接入点
//本类的引用:pointCut()
//外部类的引用:coom.MrChengs.aop.LogAspects.pointCut()
@Pointcut("execution(public int coom.MrChengs.aop.MathCAL.*(..))")
public void pointCut(){}; //目标方法之前切入;切入点表达式(指定在那个方面切入)
//JoinPoint这个参数一定要出现在参数表的第一位,否则会报错
@Before("pointCut()")
public void loginStart(JoinPoint joinpoint){
//拿到执行的数据
Object [] args = joinpoint.getArgs(); System.out.println("开始计算:"+joinpoint.getSignature().getName()+"--"+Arrays.asList(args));
} //无论正常还是异常结束
@After("pointCut()")
public void loginEnd(){
System.out.println("结束计算");
}
@AfterReturning(value="pointCut()",returning="res")
public void logReturn(int res ){
System.out.println("结果L:" + res);
}
@AfterThrowing(value ="pointCut()",throwing="exc")
public void loginException(Exception exc){
System.out.println("Exception:" + exc);
}
}
//业务类
public class MathCAL {
public int div(int i,int j){
System.out.println("正在计算.....");
return (i / j);
}
}
@EnableAspectJAutoProxy
@Configuration
public class MainAopConfig {
//业务逻辑类
@Bean
public MathCAL mathCal(){
return new MathCAL();
}
//切面类
@Bean
public LogAspects logAspect(){
return new LogAspects();
} }
@Test
public void test(){
AnnotationConfigApplicationContext app = new
AnnotationConfigApplicationContext(MainAopConfig.class); MathCAL math = app.getBean(MathCAL.class);
math.div(, );
}
开始计算:div--[, ]
正在计算.....
结束计算
Exception:java.lang.ArithmeticException: / by zero
@Import(AspectJAutoProxyRegistrar.class)
public @interface EnableAspectJAutoProxy {
...
}
class AspectJAutoProxyRegistrar implements ImportBeanDefinitionRegistrar {
...
}
利用AspectJAutoProxyRegistrar自定义为容器注入bean
4.spring:@Profile,AOP的更多相关文章
- Spring ioc,aop的理解
什么是控制反转? 控制反转是一种将组件依赖关系的创建和管理置于程序外部的技术. 由容器控制程序之间的关系,而不是由代码直接控制 由于控制权由代码转向了容器,所以称为反转 依赖注入,作用是避免手工在各代 ...
- Spring中的AOP 专题
Caused by: java.lang.IllegalArgumentException: ProceedingJoinPoint is only supported for around advi ...
- Java之代理(jdk静态代理,jdk动态代理,cglib动态代理,aop,aspectj)
一.概念 代理是什么呢?举个例子,一个公司是卖摄像头的,但公司不直接跟用户打交道,而是通过代理商跟用户打交道.如果:公司接口中有一个卖产品的方法,那么公司需要实现这个方法,而代理商也必须实现这个方法. ...
- 170511、Spring IOC和AOP 原理彻底搞懂
Spring提供了很多轻量级应用开发实践的工具集合,这些工具集以接口.抽象类.或工具类的形式存在于Spring中.通过使用这些工具集,可以实现应用程序与各种开源技术及框架间的友好整合.比如有关jdbc ...
- Spring入门3.AOP编程
Spring入门3.AOP编程 代码下载: 链接: http://pan.baidu.com/s/11mYEO 密码: x7wa 前言: 前面学习的知识是Spring在Java项目中的IoC或DJ,这 ...
- Spring Boot2(六):使用Spring Boot整合AOP面向切面编程
一.前言 众所周知,spring最核心的两个功能是aop和ioc,即面向切面和控制反转.本文会讲一讲SpringBoot如何使用AOP实现面向切面的过程原理. 二.何为aop aop全称Aspec ...
- 复习Spring第二课--AOP原理及其实现方式
AOP原理: AOP,面向方面的编程,使用AOP,你可以将处理方面(Aspect)的代码注入主程序,通常主程序的主要目的并不在于处理这些aspect.AOP可以防止代码混乱.AOP的应用范围包括:持久 ...
- Spring 通过来AOP 实现前置,环绕,异常通知,注解(转)
本节主要内容: 1. Spring AOP前置通知案例 2. Spring AOP环绕通知案例 3. Spring AOP异常通知案例 4. Spring AOP注解使 ...
- Spring.profile配合Jenkins发布War包,实现开发、测试和生产环境的按需切换
前两篇不错 Spring.profile实现开发.测试和生产环境的配置和切换 - Strugglion - 博客园https://www.cnblogs.com/strugglion/p/709102 ...
随机推荐
- 很小的一个函数执行时间调试器Timer
对于函数的执行性能(这里主要考虑执行时间,所耗内存暂不考虑),这里写了一个简单的类Timer,用于量化函数执行所耗时间. 整体思路很简单,就是new Date()的时间差值.我仅仅了做了一层简单的封装 ...
- 点击checkbox全选,其它被选中,再点击取消
<input type="checkbox" value="" id="checkall" name="" siz ...
- WebAPI搭建(一)如何在Webforms 下 搭建WebAPI
公司的很多项目前期一直是用的WebForms.但是因为业务的发展,公司要在原有的项目上接入移动端,webservice有点老旧了,现在比较流行RESTFul,于是乎就想到了WebAPI. 一.如果是新 ...
- MDI-设置子窗体只能弹出一个--单例模式
不足之处,欢迎指正! 什么是MDI..我表示不知道的呢. MDI(Multiple Document Interface)就是所谓的多文档界面,与此对应就有单文档界面 (SDI), 它是微软公司从Wi ...
- GET和POST请求的区别如下
POST和GET都是向服务器提交数据,并且都会从服务器获取数据. 区别: 1.传送方式:get通过地址栏传输,post通过报文传输. 2.传送长度:get参数有长度限制(受限于url长度),而post ...
- centos自带python2.6无法使用pip命令
1.首先检查linux有没有安装python-pip包,直接执行 yum install python-pip,或者 which pip 我的已经安装了 2.没有可用软件包 python-pip.就执 ...
- 第八章.Java集合
Java集合类是一种特别有用的工具类,可用于存储数量不等的对象.Java集合大致可分为Set.List.Queue和Map四种体系 Set代表无序.不可重复的集合 List代表有序.重复的集合 Map ...
- HTML总结摘要
一 概述 1.什么是HTML? HyperText Markup Language,超文本标记语言,客户端技术的技术,负责页面展示. 2.HTML的特点 标签不区分大小写. 3.请求地址 HTML是客 ...
- 005hystrix.stream信息聚合Turbine
1.POM配置 和普通Spring Boot工程相比,仅仅添加了Turbine和Spring Boot Starter Actuator依赖 <dependencies> <!--添 ...
- [转载]hive中order by,sort by, distribute by, cluster by作用以及用法
1. order by Hive中的order by跟传统的sql语言中的order by作用是一样的,会对查询的结果做一次全局排序,所以说,只有hive的sql中制定了order by所有的 ...