包含和排除
使用默认的过滤 为true时是扫描所有!
Annotation为注解 assignable为类型
<!-- <context:include-filter>:在设定的包结构下,再次通过注解或类型具体包含到某个或某几个类 注意:在使用包含时,一定要设置use-default-filters="false",将默认的过滤(即扫描包下的所有类)关闭 <context:exclude-filter>:在设定的包结构下,再次通过注解或类型排除某个或某几个类 注意:在使用排除时,一定要设置use-default-filters="true",将默认的过滤(即扫描包下的所有类)打开 切记:一个<context:component-scan>中可以出现多个include,也可以出现多个exclude,但不能两个同时出现 --> <context:component-scan base-package="com.atguigu.ioc.userMod" use-default-filters="true"> <!-- 根据注释来进行包含 --> <!-- <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/> --> <!-- 根据类型来进行包含 --> <!-- <context:include-filter type="assignable" expression="com.atguigu.ioc.userMod.service.UserServiceImpl"/> --> <!-- 根据注释来进行排除 --> <!-- <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Repository"/> --> <!-- 根据类型来进行排除 --> <!-- <context:exclude-filter type="assignable" expression="com.atguigu.ioc.userMod.service.UserServiceImpl"/> --> </context:component-scan>
基于注解的自动装配
通过三层联动 controller(控制层)->service(业务层)->dao(持久层)
加上@Autowired 自动装配注解 默认通过byType的方式获取!
组件管理
注:在组件管理过程中 可能会出现一些错误案例
NoSuchBean 指缺少注解
NoUniqueBean 指bean过多 缺少指定
今日重点:AOP前奏 JDK动态代理 Proxy
public interface MathI { int add(int i,int j); int sub(int i,int j); int mul(int i,int j); int div(int i,int j); } public class MathImpl implements MathI{ @Override public int add(int i, int j) { System.out.println("Method:add,arguments:"+i+","+j); int result = i + j; System.out.println("Method:add,result"+result); return result; } @Override public int sub(int i, int j) { System.out.println("Method:sub,arguments:"+i+","+j); int result = i - j; System.out.println("Method:sub,result"+result); return result; } @Override public int mul(int i, int j) { System.out.println("Method:mul,arguments:"+i+","+j); int result = i * j; System.out.println("Method:mul,result"+result); return result; } @Override public int div(int i, int j) { System.out.println("Method:div,arguments:"+i+","+j); int result = i / j; System.out.println("Method:div,result"+result); return result; } } public class Test { public static void main(String[] args) { MathI math = new MathImpl(); int result = math.add(1, 1); System.out.println(result); } } import java.lang.reflect.InvocationHandler; import java.lang.reflect.Method; import java.lang.reflect.Proxy; //工具类 在任何情况下生成一个代理对象 public class ProxyUtil { private MathImpl mathImpl;//目标对象 代理对象:帮助完成目标对象中的功能 public Object getProxy(){//获得代理对象 要求:必须有接口 保证结果一致性 //jdk动态代理 使用的是jdk中的Proxy对象 //当前代理对象所属类的类加载器(loader) 目标对象所实现的所有接口(interfaces) 代理对象如何实现目标对象的功能(InvocationHandler) ClassLoader loader = this.getClass().getClassLoader(); Class[] interfaces = mathImpl.getClass().getInterfaces(); return Proxy.newProxyInstance(loader, interfaces, new InvocationHandler() { @Override //动态代理:暂不需设置(proxy) 处理的方法(method) 处理的参数(args) public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { //方法的执行 用目标对象来执行 return method.invoke(mathImpl, args); } }); } } //可以通过匿名内部类的方式 也可通过实现InvocationHandler接口 两种方式!