Spring零散笔记

ImportAware的使用
  1. 编写注解类
    @Retention(value = java.lang.annotation.RetentionPolicy.RUNTIME)
    @Target(value = { java.lang.annotation.ElementType.TYPE })
    @Documented
    // 这里使用@Import导入ImportAware的实现类WebSecurityConfiguration
    @Import({ WebSecurityConfiguration.class,SpringWebMvcImportSelector.class })
    @EnableGlobalAuthentication
    @Configuration
    public @interface EnableWebSecurity {
        boolean debug() default false;
    }
    
  2. 编写ImportAware实现类
    @Configuration
    public class WebSecurityConfiguration implements ImportAware, BeanClassLoaderAware {
        public void setImportMetadata(AnnotationMetadata importMetadata) {
            Map<String, Object> enableWebSecurityAttrMap = importMetadata
                    .getAnnotationAttributes(EnableWebSecurity.class.getName());
            AnnotationAttributes enableWebSecurityAttrs = AnnotationAttributes
                    .fromMap(enableWebSecurityAttrMap);
            debugEnabled = enableWebSecurityAttrs.getBoolean("debug");
            if (webSecurity != null) {
                webSecurity.debug(debugEnabled);
            }
        }
    }
    
生命周期函数
  1. SmartInitializingSingleton 最晚的执行机会
确保对象只被创建一次
  1. 编写公用父类
    public abstract class AbstractSecurityBuilder<O> implements SecurityBuilder<O> {
        private AtomicBoolean building = new AtomicBoolean();
        private O object;
        public AbstractSecurityBuilder() {
        }
        public final O build() throws Exception {
            if (this.building.compareAndSet(false, true)) {
                this.object = this.doBuild();
                return this.object;
            } else {
                throw new AlreadyBuiltException("This object has already been built");
            }
        }
        public final O getObject() {
            if (!this.building.get()) {
                throw new IllegalStateException("This object has not been built");
            } else {
                return this.object;
            }
        }
        protected abstract O doBuild() throws Exception;
    }
    
DelegatingFilterProxy与Shiro的Filter整合
  1. DelegatingFilterProxy源码
    public class DelegatingFilterProxy extends GenericFilterBean {
        @Nullable
        private String contextAttribute;
        @Nullable
        private WebApplicationContext webApplicationContext;
        @Nullable
        private String targetBeanName;
        private boolean targetFilterLifecycle;
        @Nullable
        private volatile Filter delegate;
        private final Object delegateMonitor;
        public DelegatingFilterProxy() {
            this.targetFilterLifecycle = false;
            this.delegateMonitor = new Object();
        }
        public DelegatingFilterProxy(Filter delegate) {
            this.targetFilterLifecycle = false;
            this.delegateMonitor = new Object();
            Assert.notNull(delegate, "Delegate Filter must not be null");
            this.delegate = delegate;
        }
        public DelegatingFilterProxy(String targetBeanName) {
            this(targetBeanName, (WebApplicationContext)null);
        }
        public DelegatingFilterProxy(String targetBeanName, @Nullable WebApplicationContext wac) {
            this.targetFilterLifecycle = false;
            this.delegateMonitor = new Object();
            Assert.hasText(targetBeanName, "Target Filter bean name must not be null or empty");
            this.setTargetBeanName(targetBeanName);
            this.webApplicationContext = wac;
            if (wac != null) {
                this.setEnvironment(wac.getEnvironment());
            }
        }
        protected void initFilterBean() throws ServletException {
            synchronized(this.delegateMonitor) {
                if (this.delegate == null) {
                    if (this.targetBeanName == null) {
                        this.targetBeanName = this.getFilterName();
                    }
                    WebApplicationContext wac = this.findWebApplicationContext();
                    if (wac != null) {
                        this.delegate = this.initDelegate(wac);
                    }
                }
            }
        }
        public void doFilter(ServletRequest request, ServletResponse response, FilterChain filterChain) throws ServletException, IOException {
            Filter delegateToUse = this.delegate;
            if (delegateToUse == null) {
                synchronized(this.delegateMonitor) {
                    delegateToUse = this.delegate;
                    if (delegateToUse == null) {
                        WebApplicationContext wac = this.findWebApplicationContext();
                        if (wac == null) {
                            throw new IllegalStateException("No WebApplicationContext found: no ContextLoaderListener or DispatcherServlet registered?");
                        }
                        delegateToUse = this.initDelegate(wac);
                    }
                    this.delegate = delegateToUse;
                }
            }
            this.invokeDelegate(delegateToUse, request, response, filterChain);
        }
        public void destroy() {
            Filter delegateToUse = this.delegate;
            if (delegateToUse != null) {
                this.destroyDelegate(delegateToUse);
            }
        }
        @Nullable
        protected WebApplicationContext findWebApplicationContext() {
            if (this.webApplicationContext != null) {
                if (this.webApplicationContext instanceof ConfigurableApplicationContext) {
                    ConfigurableApplicationContext cac = (ConfigurableApplicationContext)this.webApplicationContext;
                    if (!cac.isActive()) {
                        cac.refresh();
                    }
                }
                return this.webApplicationContext;
            } else {
                String attrName = this.getContextAttribute();
                return attrName != null ? WebApplicationContextUtils.getWebApplicationContext(this.getServletContext(), attrName) : WebApplicationContextUtils.findWebApplicationContext(this.getServletContext());
            }
        }
        protected Filter initDelegate(WebApplicationContext wac) throws ServletException {
            String targetBeanName = this.getTargetBeanName();
            Assert.state(targetBeanName != null, "No target bean name set");
            Filter delegate = (Filter)wac.getBean(targetBeanName, Filter.class);
            if (this.isTargetFilterLifecycle()) {
                delegate.init(this.getFilterConfig());
            }
            return delegate;
        }
        protected void invokeDelegate(Filter delegate, ServletRequest request, ServletResponse response, FilterChain filterChain) throws ServletException, IOException {
            delegate.doFilter(request, response, filterChain);
        }
        protected void destroyDelegate(Filter delegate) {
            if (this.isTargetFilterLifecycle()) {
                delegate.destroy();
            }
        }
         public void setContextAttribute(@Nullable String contextAttribute) {
            this.contextAttribute = contextAttribute;
        }
    
        @Nullable
        public String getContextAttribute() {
            return this.contextAttribute;
        }
        public void setTargetBeanName(@Nullable String targetBeanName) {
            this.targetBeanName = targetBeanName;
        }
        @Nullable
        protected String getTargetBeanName() {
            return this.targetBeanName;
        }
        public void setTargetFilterLifecycle(boolean targetFilterLifecycle) {
            this.targetFilterLifecycle = targetFilterLifecycle;
        }
        protected boolean isTargetFilterLifecycle() {
            return this.targetFilterLifecycle;
        }
    }
    
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring是一个开源的Java框架,用于构建企业级应用程序。它提供了一种轻量级的、非侵入式的开发方式,通过依赖注入和面向切面编程等特性,简化了Java应用程序的开发过程。 以下是关于Spring学习的一些笔记: 1. IoC(控制反转):Spring通过IoC容器管理对象的创建和依赖关系的注入。通过配置文件或注解,将对象的创建和依赖关系的维护交给Spring容器来管理,降低了组件之间的耦合度。 2. DI(依赖注入):Spring通过依赖注入将对象之间的依赖关系解耦。通过构造函数、Setter方法或注解,将依赖的对象注入到目标对象中,使得对象之间的关系更加灵活和可维护。 3. AOP(面向切面编程):Spring提供了AOP的支持,可以将与业务逻辑无关的横切关注点(如日志、事务管理等)从业务逻辑中分离出来,提高了代码的可重用性和可维护性。 4. MVC(模型-视图-控制器):Spring提供了一个MVC框架,用于构建Web应用程序。通过DispatcherServlet、Controller、ViewResolver等组件,实现了请求的分发和处理,将业务逻辑和视图展示进行了分离。 5. JDBC和ORM支持:Spring提供了对JDBC和ORM框架(如Hibernate、MyBatis)的集成支持,简化了数据库访问的操作,提高了开发效率。 6. 事务管理:Spring提供了对事务的支持,通过声明式事务管理和编程式事务管理,实现了对数据库事务的控制和管理。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值