第六章-深入学习spring框架的ioc和aop
分类: ssm 专栏: ssm框架课 标签: spring框架 深入学习ioc 深入学习aop
2022-12-08 11:09:58 1216浏览
1.增强类型扩展
1.异常抛出增强
<bean id="errorLog" class="com.jf3q.part2.aop.ErrorLog"></bean> <!--配置切面--> <aop:config> <!--定义切入点--> <aop:pointcut id="pc" expression="execution( * com.jf3q.part2.service..*.*(..))"/> <aop:aspect ref="errorLog"> <aop:after-throwing method="error" throwing="e" pointcut-ref="pc"></aop:after-throwing> </aop:aspect> </aop:config>
@Log4j public class ErrorLog { public void error(JoinPoint jp, Exception e) { log.error(jp.getSignature().getName() + "方法发生异常" + e); } }
2.最终增强
<bean id="finallyLog" class="com.jf3q.part2.aop.FinallyLog"></bean> <aop:config> <!--定义切入点--> <aop:pointcut id="pc" expression="execution( * com.jf3q.part2.service..*.*(..))"/> <aop:aspect ref="finallyLog"> <aop:after method="after" pointcut-ref="pc"></aop:after> </aop:aspect> </aop:config>
@Log4j public class FinallyLog { public void after(JoinPoint jp){ log.info(jp.getSignature().getName()+"被调用了"); } }
3.环绕增强
<aop:config> <!--定义切入点--> <aop:pointcut id="pc" expression="execution( * com.jf3q.part2.service..*.*(..))"/> <aop:aspect ref="aroundLog"> <aop:around method="aroundMethod" pointcut-ref="pc"></aop:around> </aop:aspect> </aop:config>
@Log4j public class AroundLog { public Object aroundMethod(ProceedingJoinPoint jp) throws Throwable { try { log.info("调用对象:" + jp.getTarget() + "\n 方法" + jp.getSignature().getName() + "\n 入参:" + Arrays.toString(jp.getArgs())); Object result= jp.proceed(); log.info("调用对象:" + jp.getTarget() + "\n 方法" + jp.getSignature().getName() + "\n 返回值:" +result); return result; } catch (Throwable throwable) { throwable.printStackTrace(); log.error(jp.getSignature().getName() + "方法发生异常" + throwable); throw throwable; }finally { log.info(jp.getSignature().getName()+"被调用了,执行完毕"); } } }
备注:环绕增强的这种权限很高,可以修改传入的参数之类的
Object[] objects = {new UserInfo().setUsername("afan").setPassword("11111")}; Object result= jp.proceed(objects);
2.依赖注入方式扩展
2.1构造注入
<bean id="userinfo" class="com.jf3q.part2.domain.UserInfo"> <constructor-arg value="1"></constructor-arg> <constructor-arg value="123456" index="2"></constructor-arg> <constructor-arg value="L4" index="1"></constructor-arg> </bean> <bean id="userinfo" class="com.jf3q.part2.domain.UserInfo"> <constructor-arg value="1" type="java.lang.Integer"></constructor-arg> <constructor-arg name="password" value="123456" ></constructor-arg> <constructor-arg name="username" value="L4" ></constructor-arg> </bean>
如果依赖是对象的话就用ref
<bean id="userdao" class="com.jf3q.part2.dao.impl.UserDaoImpl"></bean> <bean id="usersevice" class="com.jf3q.part2.service.impl.UserServiceImpl"> <constructor-arg name="userDao" ref="userdao"/> </bean>
2.2p命名空间注入
底层还是用的设值注入,没有set方法的话是不行的呢。
<bean id="userdao" class="com.jf3q.part2.dao.impl.UserDaoImpl"></bean> <bean id="usersevice" class="com.jf3q.part2.service.impl.UserServiceImpl" p:userDao-ref="userdao" />
2.3不同数据类型的注入
xml里写特殊字符
< | < | 小于号 |
> | > | 大于号 |
& | & | 和 |
' | ’ | 单引号 |
" | " | 双引号 |
案例:
package com.jf3q.part2.domain; import java.util.List; import java.util.Map; import java.util.Properties; import java.util.Set; public class TestEntity { private String specialCharacter1; // 特殊字符值1 private String specialCharacter2; // 特殊字符值2 private User outUser; // 引用外部对象 private User innerBean; // JavaBean类型 private List<String> list; // List类型 private String[] array; // 数组类型 private Set<String> set; // Set类型 private Map<String, String> map; // Map类型 private Properties props; // Properties类型 private String emptyValue; // 注入空字符串值 private String nullValue = "init value"; // 注入null值 public void setSpecialCharacter1(String specialCharacter1) { this.specialCharacter1 = specialCharacter1; } public void setSpecialCharacter2(String specialCharacter2) { this.specialCharacter2 = specialCharacter2; } public void setInnerBean(User user) { this.innerBean = user; } public void setList(List<String> list) { this.list = list; } public void setArray(String[] array) { this.array = array; } public void setSet(Set<String> set) { this.set = set; } public void setMap(Map<String, String> map) { this.map = map; } public void setProps(Properties props) { this.props = props; } public void setEmptyValue(String emptyValue) { this.emptyValue = emptyValue; } public void setNullValue(String nullValue) { this.nullValue = nullValue; } public void setOutUser(User outUser) { this.outUser = outUser; } public void showValue() { System.out.println("特殊字符1:" + this.specialCharacter1); System.out.println("特殊字符2:" + this.specialCharacter2); System.out.println("外部引用Bean:" + this.outUser.getUsername()); System.out.println("内部Bean:" + this.innerBean.getUsername()); System.out.println("List属性:" + this.list); System.out.println("数组属性[0]:" + this.array[0]); System.out.println("Set属性:" + this.set); System.out.println("Map属性:" + this.map); System.out.println("Properties属性:" + this.props); System.out.println("注入空字符串:[" + this.emptyValue + "]"); System.out.println("注入null值:" + this.nullValue); } }
<bean id="user" class="com.jf3q.part2.domain.User" > <property name="username" value="afan"/> <property name="password" value="1234556"/> </bean> <bean id="entity" class="com.jf3q.part2.domain.TestEntity"> <property name="specialCharacter1" > <value>p&b</value> </property> <property name="specialCharacter2"> <value><![CDATA[p>b]]></value> </property> <property name="outUser" ref="user"></property> <property name="innerBean"> <bean class="com.jf3q.part2.domain.User"> <property name="username" value="xiaojie"/> <property name="password" value="123456"/> </bean> </property> <property name="list"> <list> <value>springboot</value> <value>springcloud</value> </list> </property> <property name="array"> <list> <value>java</value> <value>html</value> </list> </property> <property name="set"> <set> <value>c43</value> <value>c45</value> </set> </property> <property name="map"> <map> <entry key="username" value="xiaojie"/> <entry key="password" value="1234567"/> </map> </property> <property name="props"> <props> <prop key="username">root</prop> <prop key="port">3306</prop> </props> </property> <property name="emptyValue"> <value></value> </property> <property name="nullValue"> <null></null> </property> </bean>
3.使用注解实现springIOC
1.引入aop的jar包
2.使用注解定义bean,常见的几个注解:
@Repository
@Controller
@Service
@Component
3.注入依赖
@Autowired:按类型匹配的方式注入
如果有相同类型的bean的话,需要配合@Qualifier("xxx")
@Resource: 按注入的bean名称注入
4.修改配置文件使注解生效
<?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:context="http://www.springframework.org/schema/context" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd "> <context:component-scan base-package="com.jf3q.part2.dao,com.jf3q.part2.service"></context:component-scan>
4.使用注解实现springAop
1.修改配置文件
2.开启注解配置aop
<aop:aspectj-autoproxy/>
3.给增强类添加注解
3.1前置和后置增强
@Log4j @Component @Aspect public class UserServiceLog { @Pointcut("execution(* com.jf3q.part2.service..*.* (..))") public void poincut(){} @Before("poincut()") public void before(JoinPoint jp){ log.info("调用对象:" + jp.getTarget() + "\n 方法" + jp.getSignature().getName() + "\n 入参:" + Arrays.toString(jp.getArgs())); } @AfterReturning(pointcut ="poincut()",returning = "result") public void afterReturn(JoinPoint jp,Object result){ log.info("调用对象:" + jp.getTarget() + "\n 方法" + jp.getSignature().getName() + "\n 返回值:" +result); } }
3.2异常增强
@Log4j @Component @Aspect public class ErrorLog { @Pointcut("execution(* com.jf3q.part2.service..*.*(..))") public void pc(){} @AfterThrowing(pointcut = "pc()", throwing = "e") public void error(JoinPoint jp, Exception e) { log.error(jp.getSignature().getName() + "方法发生异常" + e); } }
3.3最终增强
@Log4j @Component @Aspect public class FinallyLog { @After("execution(* com.jf3q.part2.service..*.*(..))") public void after(JoinPoint jp){ log.info(jp.getSignature().getName()+"被调用了"); } }
3.4环绕增强
@Log4j @Component @Aspect public class AroundLog { @Around("execution(* com.jf3q.part2.service..*.*(..))") public Object aroundMethod(ProceedingJoinPoint jp) throws Throwable { try { log.info("调用对象:" + jp.getTarget() + "\n 方法" + jp.getSignature().getName() + "\n 入参:" + Arrays.toString(jp.getArgs())); Object[] objects = {new UserInfo().setUsername("afan").setPassword("11111")}; Object result= jp.proceed(objects); log.info("调用对象:" + jp.getTarget() + "\n 方法" + jp.getSignature().getName() + "\n 返回值:" +result); return result; } catch (Throwable throwable) { throwable.printStackTrace(); log.error(jp.getSignature().getName() + "方法发生异常" + throwable); throw throwable; }finally { log.info(jp.getSignature().getName()+"被调用了,执行完毕"); } } }
好博客就要一起分享哦!分享海报
此处可发布评论
评论(0)展开评论
展开评论
您可能感兴趣的博客