Spring学习(四)

使用表达式配置切入点

1 切入点:实际增强方法

2 常用表达式

execution(<访问修饰符>?<返回类型><方法名>(<参数>)<异常>)

(1)execution(* cn.itcast.aop.Book.add(..))

(2)execution(* cn.itcast.aop.Book.*(..))

(3)execution(* *.*(..))

(4)匹配所有save开头的方法execution(*save*(..))

Aspectj的aop操作

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"> <!-- bean definitions here -->

    <!--1 配置对象 -->
    <bean id="book" class="cn.itcast.aop.Book"></bean>
    <bean id="myBook" class="cn.itcast.aop.MyBook"></bean>
    <!-- 2配置aop操作 -->
    <aop:config>
    <!--2.1配置切入点-->
    <aop:pointcut expression="execution(* cn.itcast.aop.Book.add(..))" id="pointcut1"/>
    <!--2.2配置切面 
        把增强用到方法上面
     -->
    <aop:aspect ref="myBook">
    <!-- 增强类型 
        method:增强类里面使用哪个方法作为前置
    -->                     
          <aop:before method="before1" pointcut-ref="pointcut1"/>
          <aop:after-returning method="after1" pointcut-ref="pointcut1"/>
          <aop:around method="around1" pointcut-ref="pointcut1"/>
    </aop:aspect>
    </aop:config>
</beans>
package cn.itcast.aop;

import org.aspectj.lang.ProceedingJoinPoint;

public class MyBook {

	public void before1() {
		System.out.println("前置增强.....");
	}
	
	public void after1() {
		System.out.println("后置增强");
	}
	
	//环绕通知
	public void around1(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
		//方法之前
		System.out.println("方法之前 、。。。。");
		
		//执行被增强的方法
		proceedingJoinPoint.proceed();
		
		//方法之后
		System.out.println("方法之前后、。。。。");
		
	}
}
package cn.itcast.aop;

import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class TestAnno {

	@Test
	public void testService() {
		ApplicationContext context=
				new ClassPathXmlApplicationContext("bean3.xml");
		Book book=(Book)context.getBean("book");
		book.add();
		
	}
	

}

结果如下:


log4j介绍

1 通过log4j可以看到程序运行过程中更详细的信息

(1)经常使用log4j查看日志

2 使用

(1)导入log4j的jar包

(2)复制log4j.properties配置文件,复制到src下面

log4j.rootLogger=info,stdout(debug,stdout更为详细)

3 设置日志级别

(1)info:看到基本信息

(2)debug:看到更详细的信息

log4j.properties配置文件如下:

### \u8F93\u51FA\u4FE1\u606F\u5230\u63A7\u5236\u62AC ###
log4j.appender.stdout = org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target = System.out
log4j.appender.stdout.layout = org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern = [%-5p] %d{yyyy-MM-dd HH:mm:ss,SSS} method:%l%n%m%n

### \u8F93\u51FADEBUG \u7EA7\u522B\u4EE5\u4E0A\u7684\u65E5\u5FD7\u5230=E://logs/error.log ###
log4j.appender.D = org.apache.log4j.DailyRollingFileAppender
log4j.appender.D.File = E:\mylog.log
log4j.appender.D.Append = true
log4j.appender.D.Threshold = DEBUG 
log4j.appender.D.layout = org.apache.log4j.PatternLayout
log4j.appender.D.layout.ConversionPattern = %-d{yyyy-MM-dd HH:mm:ss}  [ %t:%r ] - [ %p ]  %m%n

### \u8F93\u51FAERROR \u7EA7\u522B\u4EE5\u4E0A\u7684\u65E5\u5FD7\u5230=E://logs/error.log ###
log4j.appender.E = org.apache.log4j.DailyRollingFileAppender
log4j.appender.E.File =E:\mylog.log 
log4j.appender.E.Append = true
log4j.appender.E.Threshold = ERROR 
log4j.appender.E.layout = org.apache.log4j.PatternLayout
log4j.appender.E.layout.ConversionPattern = %-d{yyyy-MM-dd HH:mm:ss}  [ %t:%r ] - [ %p ]  %m%n
log4j.rootLogger = info,stdout

spring整合web项目

1 演示问题

(1)action调用service,service调用dao,

每次访问action时候,都会加载spring配置文件

2 解决方案:

(1)在服务器启动时候,创建对象加载配置文件,

(2)底层使用监听器、ServiceContext对象

3 在spring里面不需要我们自己写代码实现,帮封装

(1)封装了一个监听器,只需要配置监听器就可以了

(2)配置监听器之前做的事情:导入spring整合web项目jar包

     <!-- web.xml配置监听器 -->
	<listener>
	  <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
	</listener>

(3)指定加载spring配置文件位置(否则找不到配置文件)

  <!-- 指定spring配置文件位置 -->
  <context-param>
  	<param-name>contextConfigLocation</param-name>
  	<param-value>classpath:bean1.xml</param-value>
  </context-param>


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值