//==========================================Spring的IOC核心原理===============================================\\
1.IOC基本原理
什么是IOC,ioc就是控制反转或者依赖注入 反转的啥?反转的控制权 要想调用类的属性或方法 首先要实例化该对象 再调用类的属性和方法
然后有了ioc容器不让你再去创建类对象 ioc容器会初始化类的对象放到bean的缓存池中后续会涉及bean的作用域等..
//基本的接口
package com.cnse.user.service;
public interface UserService {
public String sayEnglish(String str);
}
//接口实现类
package com.cnse.user.service;
public class UserServiceImpl implements UserService{
@Override
public String sayEnglish(String str) {
return str;
}
}
//spring配置文件
<?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:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd">
<!-- 配置Bean -->
<bean id="UserServiceImpl" class="com.cnse.user.service.UserServiceImpl"></bean>
</beans>
//测试类
package com.cnse.user.main;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.cnse.user.service.UserService;
import com.cnse.user.service.UserServiceImpl;
public class Main {
public static void main(String[] args) {
/**
* @info 没有spring IOC之前调用方法 首先初始化这个类的实例 我们自己实例化这个类对象
*/
UserService YHservice = new UserServiceImpl();
String info= YHservice.sayEnglish("YHservice_手动实例化类的对象");
System.out.println(info);
/**
* @info 引入Spring IOC之后
* 1.在配置文件中注入bean
* 2.直接根据bean的id直接调用bean的方法
*/
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
UserService IOCservice = (UserService) context.getBean("UserServiceImpl");
String info2=IOCservice.sayEnglish("IOCservice_ioc容器帮你实例化");
System.out.println(info2);
}
}
//运行结果
//原理和流程
(1)定义相关接口和接口的实现类
(2)配置springxml配置文件的bean -id -class
(3)解析xml,获取到bean 的id class 反射->Object beanObj = Class.forName(clazz).newInstance();
// 反射获取对象
(4)然后再便利property标签的属性和赋值
//要想掌握spring的ioc首先要 把反射的原理 和应用掌握 这是研究框架的基础
//===================================Spring的AOP核心和原理===================================================\\
public interface UserService {
public String sayEnglish(String str);
}
package com.cnse.user.service;
public class UserServiceImpl implements UserService{
@Override
public String sayEnglish(String str) {
try {
if("".equals(str)){
throw new RuntimeException("你输入的内容为空,请重新输入");
}
} catch (Exception e) {
e.printStackTrace();
}
return str;
}
}
package com.cnse.user.aop;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
/**
* @author Administrator
* 请看执行顺序
* AOP基于java的代理机制_同Struts2的拦截器练一样
*/
@SuppressWarnings("unused")
public class UserAdvice {
private void doBefore(JoinPoint joinPoint) {
System.out.println("UserAdvice----doBefore-----我在调用方法之前出现");
System.out.println("业务前调用--获者在你调用的方法之前--进行前置处理");
}
private Object doAround(ProceedingJoinPoint pjp) throws Throwable {
System.out.println("UserAdvice----doAround-----start");
//调用核心逻辑
Object object = pjp.proceed();
System.out.println("进入方法中--或者进入你调用的方法的第一行----到抛出异常之前如果有----");
return pjp;
}
private void doAfter(JoinPoint joinPoint) {
System.out.println("UserAdvice----doAfter-----start");
System.out.println("执行完方法体后---或者理解为--记录下那个方法执行的怎么样返回值是多少等等--后置信息");
}
private void doReturn(JoinPoint joinPoint) {
System.out.println("UserAdvice----doReturn-----start");
System.out.println("执行完对返回值得处理");
}
private void doThrowing(JoinPoint joinPoint,Throwable ex) {
System.out.println("UserAdvice----doThrowing-----start");
System.out.println("对执行时捕获的异常进行处理_异常信息:"+ex.getMessage());
}
}
<?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:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:c="http://www.springframework.org/schema/c"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd">
<!-- 配置Bean -->
<bean id="UserService" class="com.cnse.user.service.UserServiceImpl"></bean>
<!-- 使用配置的方法测试aop注解 -->
<bean id="UserAdvice" class="com.cnse.user.aop.UserAdvice"></bean>
<aop:config>
<aop:aspect id="aspect" ref="UserAdvice">
<!-- 定义切入点 -->
<aop:pointcut id="userPointCut" expression="execution(* com.cnse.user.service.*.*(..))"/>
<aop:before method="doBefore" pointcut-ref="userPointCut"/>
<aop:after method="doAfter" pointcut-ref="userPointCut"/>
<aop:around method="doAround" pointcut-ref="userPointCut"/>
<aop:after-returning method="doReturn" pointcut-ref="userPointCut"/>
<aop:after-throwing method="doThrowing" throwing="ex" pointcut-ref="userPointCut"/>
</aop:aspect>
</aop:config>
</beans>
package com.cnse.user.main;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.cnse.user.service.UserService;
import com.cnse.user.service.UserServiceImpl;
public class Main {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
UserService service = (UserService) context.getBean("UserService");
//1.AOP正常输入
String msg="AOP正常测试";
String info2 = service.sayEnglish(msg);
System.out.println(info2);
//2.AOP异常输出
/* try {
String info3 = service.sayEnglish("");
System.out.println(info3);
} catch (Exception e) {
e.printStackTrace();
}
*/
}
}
//异常运行结果