From 88a218962ba2b9403ae56b3e28e4a5a08e2825ed Mon Sep 17 00:00:00 2001 From: aoshiguchen <1052045476@qq.com> Date: Wed, 6 Jul 2022 17:21:51 +0800 Subject: [PATCH] =?UTF-8?q?aop=E4=BB=A3=E7=A0=81=E8=B0=83=E6=95=B4?= =?UTF-8?q?=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../core/aop/interceptor/InterceptorFactory.java | 12 ++++++------ .../neutrino/core/context/ApplicationContext.java | 8 ++++---- .../core/db/mapper/SqlMapperInterceptor.java | 6 ++++++ 3 files changed, 16 insertions(+), 10 deletions(-) diff --git a/neutrino-core/src/main/java/fun/asgc/neutrino/core/aop/interceptor/InterceptorFactory.java b/neutrino-core/src/main/java/fun/asgc/neutrino/core/aop/interceptor/InterceptorFactory.java index abd14493..c908cc7b 100644 --- a/neutrino-core/src/main/java/fun/asgc/neutrino/core/aop/interceptor/InterceptorFactory.java +++ b/neutrino-core/src/main/java/fun/asgc/neutrino/core/aop/interceptor/InterceptorFactory.java @@ -61,12 +61,12 @@ public class InterceptorFactory { () -> { try { // TODO - cache.set(clazz, clazz.newInstance()); -// if (BeanManager.getBean(clazz) != null) { -// cache.set(clazz, BeanManager.getBean(clazz)); -// } else { -// cache.set(clazz, clazz.newInstance()); -// } +// cache.set(clazz, clazz.newInstance()); + if (BeanManager.getBean(clazz) != null) { + cache.set(clazz, BeanManager.getBean(clazz)); + } else { + cache.set(clazz, clazz.newInstance()); + } } catch (InstantiationException|IllegalAccessException e) { throw new RuntimeException(e); } diff --git a/neutrino-core/src/main/java/fun/asgc/neutrino/core/context/ApplicationContext.java b/neutrino-core/src/main/java/fun/asgc/neutrino/core/context/ApplicationContext.java index de55e915..36e6fa0c 100644 --- a/neutrino-core/src/main/java/fun/asgc/neutrino/core/context/ApplicationContext.java +++ b/neutrino-core/src/main/java/fun/asgc/neutrino/core/context/ApplicationContext.java @@ -85,6 +85,10 @@ public class ApplicationContext implements LifeCycle { this.rootBeanFactory.registerBean(this, "rootApplicationContext"); this.rootBeanFactory.registerBean(environment.getConfig(), "rootApplicationConfig"); register(); + List beanFactoryAwareList = this.applicationBeanFactory.getBeanList(BeanFactoryAware.class); + if (CollectionUtil.notEmpty(beanFactoryAwareList)) { + beanFactoryAwareList.forEach(beanFactoryAware -> beanFactoryAware.setBeanFactory(applicationBeanFactory)); + } this.applicationBeanFactory.init(); } catch (Exception e) { log.error("应用上下文初始化异常!", e); @@ -138,10 +142,6 @@ public class ApplicationContext implements LifeCycle { */ public void run() { init(); - List beanFactoryAwareList = this.applicationBeanFactory.getBeanList(BeanFactoryAware.class); - if (CollectionUtil.notEmpty(beanFactoryAwareList)) { - beanFactoryAwareList.forEach(beanFactoryAware -> beanFactoryAware.setBeanFactory(applicationBeanFactory)); - } List applicationRunnerList = this.applicationBeanFactory.getBeanList(ApplicationRunner.class); if (CollectionUtil.notEmpty(applicationRunnerList)) { applicationRunnerList.forEach(applicationRunner -> applicationRunner.run(this.environment.getMainArgs())); diff --git a/neutrino-core/src/main/java/fun/asgc/neutrino/core/db/mapper/SqlMapperInterceptor.java b/neutrino-core/src/main/java/fun/asgc/neutrino/core/db/mapper/SqlMapperInterceptor.java index ba03488b..a86dbdf8 100644 --- a/neutrino-core/src/main/java/fun/asgc/neutrino/core/db/mapper/SqlMapperInterceptor.java +++ b/neutrino-core/src/main/java/fun/asgc/neutrino/core/db/mapper/SqlMapperInterceptor.java @@ -23,6 +23,7 @@ package fun.asgc.neutrino.core.db.mapper; import fun.asgc.neutrino.core.annotation.Autowired; import fun.asgc.neutrino.core.annotation.Component; +import fun.asgc.neutrino.core.annotation.Init; import fun.asgc.neutrino.core.annotation.NonIntercept; import fun.asgc.neutrino.core.aop.Invocation; import fun.asgc.neutrino.core.aop.interceptor.Interceptor; @@ -52,6 +53,11 @@ public class SqlMapperInterceptor implements Interceptor { private JdbcTemplate jdbcTemplate; private static final Cache paramsCache = new MemoryCache<>(); + @Init + public void a() { + System.out.println("11"); + } + @Override public void intercept(Invocation inv) throws Exception { Assert.notNull(jdbcTemplate, "JdbcTemplate未注入,调用失败!");