一、ioc
1、准备jar包
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.1.1.RELEASE</version>
</dependency>
2、在src下配置全局文件(applicationContext.xml)
<?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:conext="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">
<!--以下是注解-->
<!--告诉ioc容器去哪些包中找呗注解了的类,并创建对象-->
<conext:component-scan base-package="mapper"></conext:component-scan>
<conext:component-scan base-package="entity"></conext:component-scan>
<conext:component-scan base-package="controller"></conext:component-scan>
<conext:component-scan base-package="service"></conext:component-scan>
<!--以下是xml配置bean-->
<!--<bean id="teacher" class="entity.Teacher">-->
<!--<property name="tcName" value="罗帅"></property>-->
<!--</bean>-->
<!--<!–使用反射创建一个无参对象,再利用setXXX方法设置属性,使用scope="singleton",可以每次新创建一个对象.自动注入bean(autowire="byType/byName"(依赖注入))–>-->
<!--<bean id="student" class="entity.Student" scope="singleton" autowire="byType">-->
<!--<property name="stuId" value="1" ></property>-->
<!--<property name="stuName" value="云哥"></property>-->
<!--<!–对象属性,私有,ref="teacher"(指向外面的bean)–>-->
<!--<!–<property name="stuTeacher" ref="teacher"></property>–>-->
<!--<property name="stuDogs">-->
<!--<list>-->
<!--<bean class="entity.Dog">-->
<!--<property name="dogName" value="狗一"></property>-->
<!--</bean>-->
<!--<bean class="entity.Dog">-->
<!--<property name="dogName" value="狗二"></property>-->
<!--</bean>-->
<!--</list>-->
<!--</property>-->
<!--</bean>-->
<!--<!–使用构造方法创建对象–>-->
<!--<bean id="stu" class="entity.Student">-->
<!--<constructor-arg index="0" value="11"></constructor-arg>-->
<!--<constructor-arg index="1" value="bb"></constructor-arg>-->
<!--</bean>-->
</beans>
二、aop(面向切面编程)的使用
1、准备jar包
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.9.2</version>
</dependency>
2、src下配置全局文件(applicationContext.xml)
第一种,使用xml(xml的意思是用xml来对应相应的文件,以实现aop)
<!--aop面向切面-->
<!--xml-->
<!--全部扔到ioc容器中-->
<!--<bean id="logUtil" class="uti.LogUtil"></bean>-->
<!--<bean class="service.StudentService"></bean>-->
<!--配置aOp-->
<!--<aop:config>-->
<!--<aop:pointcut id="pt" expression="execution (void service.StudentService.insert()) or execution (void service.StudentService.delete())"></aop:pointcut>-->
<!--<!–配置切面–>-->
<!--<aop:aspect ref="logUtil">-->
<!--<aop:before method="before" pointcut-ref="pt"></aop:before>-->
<!--<aop:after method="after" pointcut-ref="pt"></aop:after>-->
<!--<aop:after-returning method="afterReturned" pointcut-ref="pt"></aop:after-returning>-->
<!--<aop:after-throwing method="afterException" pointcut-ref="pt"></aop:after-throwing>-->
<!--</aop:aspect>-->
<!--</aop:config>-->
第二种、使用注解(使用注解是因为他会自动扫描项目中对应的aop文件以及方法:个人推荐用注解,因为比较方便)
<!--以下是注解-->
<!--告诉ioc容器去哪些包中找呗注解了的类,并创建对象-->
<conext:component-scan base-package="mapper"></conext:component-scan>
<conext:component-scan base-package="entity"></conext:component-scan>
<conext:component-scan base-package="controller"></conext:component-scan>
<conext:component-scan base-package="service"></conext:component-scan>
<conext:component-scan base-package="uti"></conext:component-scan>
<!--aop:注释-->
<!--自动扫描并生成代理对象-->
<aop:aspectj-autoproxy></aop:aspectj-autoproxy>
3、aop切面方法(util中),aop的使用
第一种,以xml的方式,对应2中的xml
public void before(){
System.out.println("前置通知");
}
//后置通知无论如何都会执行
public void after(){
System.out.println("后置通知");
}
//返回通知,目标方法如果出错就不执行
public void afterReturned(){
System.out.println("返回通知");
}
public void afterException(){
System.out.println("异常通知");
}
第二种,以注解的方式、对应2中的注解
@Aspect
@Component
public class LogUtil {
@Pointcut("execution(* service.*.*(..))")
public void pointcut(){}
@Before("pointcut()")
public void before(JoinPoint jpt){
Signature sig= jpt.getSignature();
String name=sig.getName();
System.out.println("前置通知"+name);
}
//后置通知无论如何都会执行
@After("pointcut()")
public void after(){
System.out.println("后置通知");
}
//返回通知,目标方法如果出错就不执行
@AfterReturning(value = "pointcut()",returning = "val")
public void afterReturned(Object val){
System.out.println("返回通知,结果是"+val);
}
@AfterThrowing(value = "pointcut()",throwing = "e")
public void afterException(Throwable e){
System.out.println("异常通知结果是"+e.getMessage());
}
//环绕通知
// @Around("pointcut()")
public Object arround(ProceedingJoinPoint pjp){
//前置
System.out.println("环绕前");
Object result=null;
try {
//执行目标方法
result=pjp.proceed();
System.out.println(3/0);
System.out.println("环绕成功");
//返回
} catch (Throwable throwable) {
System.out.println("环绕异常");
//异常
throwable.printStackTrace();
}
//后置
System.out.println("环绕后置");
return result;
}
=============================================================================================、
备注:
以上的说法会有些模糊,但是这是我对应上课学习做出来的相应的笔记,也就是上述其实是笔记
大家学到spring的时候在看上述的代码以及注释就应该能够了解了。
上面是因为自己以防忘记弄出来的,需要回忆的时候只需要看一眼就可以记住了。
总而言之希望能帮助大家,这里给大家分享一下自己的经验,好记性不如烂笔头,但现在有电脑就方便许多了,也是因为如此我们何不如将重要以及自己记不住的东西用电脑来代替自己呢。记事本只是其中一种笔记方法,还有许许多多的软件,大家可以去了解一下,总会找到合适自己的学习方法,以及记录的软件。