Profile:

可以根据当前的环境,动态激活和切换一系列的组件功能
指定组件在那个环境下才能被注册到容器中,不指定任何环境下都能注册到
1.加了环境标识的bean只有环境激活的时候才能注册到容器中
    默认是default ,@Profile("default") 才能加入到环境中
2.还可以下载类上,只有在当时的环境下,整个类的方法才会生效
3.默认没标识的bean在,任何环境下都是加载的
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);
}
}
注:app.getEnvironment().setActiveProfiles("dev","test");可以同时写多个
mainConfigProfile
devDatasource
此时可以看出 app.getEnvironment().setActiveProfiles("dev");
这里只添加了一个环境,所以得到在dev环境下的bean,其余的均不会装配到bean容器中
此种情况下,也会自动装入到bean容器
@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:

在程序运行期间,能够动态的将某段代码切入到指定的位置进行编程
 
1.导入aop模块:aspects
2.业务逻辑类
3.日志切面类:
    前置通知:@Before
    后置通知:   @After
    异常通知:@AfterReturning
    返回通知:@AfterThrowing
    环绕通知:@Around
4.将切面类和业务逻辑加入容器里
5.告诉spring那个类是切面类
    切面类加一个注解@Aspect:告诉spring当前类是一个切面类
6.给配置类家@EnableAspectJAutoProxy 开启基于注解模式动态代理

//切面类

@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
AOP原理:
@EnableAspectJAutoProxy
@Import(AspectJAutoProxyRegistrar.class)
public @interface EnableAspectJAutoProxy {
...
}
class AspectJAutoProxyRegistrar implements  ImportBeanDefinitionRegistrar {
...
}

利用AspectJAutoProxyRegistrar自定义为容器注入bean

4.spring:@Profile,AOP的更多相关文章

  1. Spring ioc,aop的理解

    什么是控制反转? 控制反转是一种将组件依赖关系的创建和管理置于程序外部的技术. 由容器控制程序之间的关系,而不是由代码直接控制 由于控制权由代码转向了容器,所以称为反转 依赖注入,作用是避免手工在各代 ...

  2. Spring中的AOP 专题

    Caused by: java.lang.IllegalArgumentException: ProceedingJoinPoint is only supported for around advi ...

  3. Java之代理(jdk静态代理,jdk动态代理,cglib动态代理,aop,aspectj)

    一.概念 代理是什么呢?举个例子,一个公司是卖摄像头的,但公司不直接跟用户打交道,而是通过代理商跟用户打交道.如果:公司接口中有一个卖产品的方法,那么公司需要实现这个方法,而代理商也必须实现这个方法. ...

  4. 170511、Spring IOC和AOP 原理彻底搞懂

    Spring提供了很多轻量级应用开发实践的工具集合,这些工具集以接口.抽象类.或工具类的形式存在于Spring中.通过使用这些工具集,可以实现应用程序与各种开源技术及框架间的友好整合.比如有关jdbc ...

  5. Spring入门3.AOP编程

    Spring入门3.AOP编程 代码下载: 链接: http://pan.baidu.com/s/11mYEO 密码: x7wa 前言: 前面学习的知识是Spring在Java项目中的IoC或DJ,这 ...

  6. Spring Boot2(六):使用Spring Boot整合AOP面向切面编程

    一.前言 众所周知,spring最核心的两个功能是aop和ioc,即面向切面和控制反转.本文会讲一讲SpringBoot如何使用AOP实现面向切面的过程原理. 二.何为aop ​ aop全称Aspec ...

  7. 复习Spring第二课--AOP原理及其实现方式

    AOP原理: AOP,面向方面的编程,使用AOP,你可以将处理方面(Aspect)的代码注入主程序,通常主程序的主要目的并不在于处理这些aspect.AOP可以防止代码混乱.AOP的应用范围包括:持久 ...

  8. Spring 通过来AOP 实现前置,环绕,异常通知,注解(转)

    本节主要内容:     1. Spring AOP前置通知案例     2. Spring AOP环绕通知案例     3. Spring AOP异常通知案例     4. Spring AOP注解使 ...

  9. Spring.profile配合Jenkins发布War包,实现开发、测试和生产环境的按需切换

    前两篇不错 Spring.profile实现开发.测试和生产环境的配置和切换 - Strugglion - 博客园https://www.cnblogs.com/strugglion/p/709102 ...

随机推荐

  1. The Internet Communications Engine (Ice) 跨平台异构通讯方案 第一弹-ICE简介

    .net中的通讯方案很多,从.net Remoting,MSMQ,Webservice,WSE,WCF等等,他们都有一个特点,易用,但是都不能跨语种异构,如果你服务端要用java开发,客户端用C#开发 ...

  2. node版本管理nvm使用

    nvm:Node Version Manager,用来管理node版本,可以在一台机器上来回切换node版本,比较方便. win下建议使用 nvm-windows nodist linux下直接使用n ...

  3. [android] 天气app布局练习(二)

    主要练习一下GridView MainActivity.java package com.example.weatherreport; import java.util.ArrayList; impo ...

  4. JavaWeb之JSP原理

    1.为什么需要JSP? 在很多动态网页中,绝大部分内容都是固定不变的,只有局部内容需要动态产生和改变.如果使用Servlet程序来输出只有局部内容需要改动的网页,其中所有的静态内容也需要程序员用jav ...

  5. 中南oj 1213: 二叉树结点公共祖先

    1213: 二叉树结点公共祖先 Time Limit: 1 Sec  Memory Limit: 128 MB Submit: 159  Solved: 87 [Submit][Status][Web ...

  6. VS code 自定义快捷输入

    本文是从简书复制的, markdown语法可能有些出入, 想看"正版"和更多内容请关注 简书: 小贤笔记 位置 ctrl+shift+p 搜索: snippets 输入类型: 比如 ...

  7. linux 的 磁盘管理

    1. 查看信息 1.1 查看磁盘信息 在linux中如果需要查看磁盘信息,需要使用df和du命令. df: 列出文件系统中整个磁盘的使用量 du:评估文件系统中磁盘的使用量,经常用来推算目录所占的容量 ...

  8. git 回滚到上个版本命令以及忽略某些文件提交

    1.git回滚到上个版本 git reset --hard FETCH_HEAD 2.git忽略某些文件的提交 以前是用默认的.gitignore 然后再里面默认某些文件不提交.但是有个问题,.git ...

  9. Windows下 Mysql启动报1067解决方法

    前几天刚入职安装了一下Mysql  刚开始能打开  今天去公司发现启动不了服务 报1067错误, 在网上查看了一些方法,好多种版本..以下是本人的解决方法 1.打开运行-事件查看器--Windows日 ...

  10. 密码存储中MD5的安全问题与替代方案

    md5安全吗?有多么地不安全?如何才能安全地存储密码?... md5安全吗? 经过各种安全事件后,很多系统在存放密码的时候不会直接存放明文密码了,大都改成了存放了 md5 加密(hash)后的密码,可 ...