- public class DefaultSingletonBeanRegistry extends SimpleAliasRegistry implements SingletonBeanRegistry {
- // 一级缓存
- private final Map
singletonObjects = new ConcurrentHashMap(256); - // 三级缓存
- private final Map
> singletonFactories = new HashMap(16); - // 二级缓存
- private final Map
earlySingletonObjects = new HashMap(16); - }
- /**
- * 函数式接口
- *
- * 可以将lambda表达式作为参数放到方法实参中
- * 在方法执行的时候并不会调用当前lambda表达式
- * 只有在调用getObject方法的时候才会去调用lambda表达式
- */
- @FunctionalInterface
- public interface ObjectFactory
{ - T getObject() throws BeansException;
- }
1.6 AOP
- @Configuration
- @EnableAspectJAutoProxy
- @ComponentScan(basePackages = {"com.ww.spring5"})
- public class SpringConfig {
-
- }
-
- public interface UserDao {
-
- void add();
-
- }
-
- @Repository
- public class UserDaoImpl implements UserDao {
-
- @Autowired
- @Qualifier(value = "jdbcTemplate")
- JdbcTemplate jdbcTemplate;
-
- public JdbcTemplate getJdbcTemplate() {
- return jdbcTemplate;
- }
-
- @Override
- public void add() {
- System.out.println("do add......");
- }
-
- }
-
- public class UserDaoProxy implements InvocationHandler {
-
- private UserDaoImpl userDaoImpl;
-
- public UserDaoProxy(UserDaoImpl userDaoImpl) {
- this.userDaoImpl = userDaoImpl;
- }
-
- @Override
- public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
- System.out.println("before do method......");
-
- method.invoke(userDaoImpl);
-
- System.out.println("after do method......");
-
- return null;
- }
-
- }
-
- public class TestSpring {
- /**
- * jdk动态代理
- */
- @Test
- public void testProxy() {
- Class[] clazzs = {UserDao.class};
- UserDaoProxy userDaoProxy = new UserDaoProxy(new UserDaoImpl());
- UserDao userDao = (UserDao) Proxy.newProxyInstance(TestSpring.class.getClassLoader(), clazzs, userDaoProxy);
- userDao.add();
- }
- }
术语
AspectJ
AspectJ不是Spring组成部分,是一个独立的框架,Spring框架一般基于AspectJ实现AOP操作。
切入点语法结构
execution([权限修饰符] [返回类型] [类全路径].[方法名称]([参数列表]))
- @Aspect
- @Component
- public class UserDaoAspect {
-
- /**
- * 相同切入点抽取
- */
- @Pointcut(value = "execution(* com.ww.spring5.dao.Impl.UserDaoImpl.add())")
- public void logPointCut() {
-
- }
-
- /**
- * 前置通知
- */
- @Before(value = "execution(* com.ww.spring5.dao.Impl.UserDaoImpl.add())")
- public void beforeAdd() {
- System.out.println("beforeAdd......");
- }
-
- /**
- * 后置通知
- */
- @After(value = "execution(* com.ww.spring5.dao.Impl.UserDaoImpl.add())")
- public void afterAdd() {
- System.out.println("afterAdd......");
- }
-
- /**
- * 环绕通知
- */
- @Around(value = "logPointCut()")
- public void aroundAdd(ProceedingJoinPoint joinPoint) throws Throwable {
- System.out.println("aroundAdd before......");
- joinPoint.proceed(); // 执行被增强的方法【UserDaoImpl.add()】
- System.out.println("aroundAdd after......");
- }
-
- /**
- * 最终通知
- */
- @AfterReturning(value = "execution(* com.ww.spring5.dao.Impl.UserDaoImpl.add())")
- public void afterReturningAdd() {
- System.out.println("afterReturningAdd......");
- }
-
- /**
- * 异常通知
- */
- @AfterThrowing(value = "execution(* com.ww.spring5.dao.Impl.UserDaoImpl.add())")
- public void afterThrowingAdd() {
- System.out.println("afterThrowingAdd......");
- }
-
- }
-
- public class TestSpring {
- @Test
- public void testAspectJ() {
- ApplicationContext ac = new AnnotationConfigApplicationContext(SpringConfig.class);
- UserDao userDao = ac.getBean("userDaoImpl", UserDao.class);
- userDao.add();
- }
- }