SPRING与设计模式--单件模式
单体模式是一种常用的模式,顾名思义就是一个类只允许有一个实例。springsecurity大都使用饿汉模式,在类加载时就创建好了实例。其他模式见:https://www.jianshu.com/p/c7ca51d2816e
AnyRequestMatcher源码:
package org.springframework.security.web.util.matcher;
import java.util.Arrays;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.security.web.util.matcher.RequestMatcher;
import org.springframework.util.Assert;
/**
* {@link RequestMatcher} that will return true if all of the passed in
* {@link RequestMatcher} instances match.
*
* @author Rob Winch
* @since 3.2
*/
public final class AndRequestMatcher implements RequestMatcher {
private final Log logger = LogFactory.getLog(getClass());
private final List<RequestMatcher> requestMatchers;
/**
* Creates a new instance
*
* @param requestMatchers the {@link RequestMatcher} instances to try
*/
public AndRequestMatcher(List<RequestMatcher> requestMatchers) {
Assert.notEmpty(requestMatchers, "requestMatchers must contain a value");
if (requestMatchers.contains(null)) {
throw new IllegalArgumentException(
"requestMatchers cannot contain null values");
}
this.requestMatchers = requestMatchers;
}
/**
* Creates a new instance
*
* @param requestMatchers the {@link RequestMatcher} instances to try
*/
public AndRequestMatcher(RequestMatcher... requestMatchers) {
this(Arrays.asList(requestMatchers));
}
public boolean matches(HttpServletRequest request) {
for (RequestMatcher matcher : requestMatchers) {
if (logger.isDebugEnabled()) {
logger.debug("Trying to match using " + matcher);
}
if (!matcher.matches(request)) {
logger.debug("Did not match");
return false;
}
}
logger.debug("All requestMatchers returned true");
return true;
}
@Override
public String toString() {
return "AndRequestMatcher [requestMatchers=" + requestMatchers + "]";
}
}
工厂模式用于对象生成,是整个spring框架的核心模式,借助配置信息获取要生成的对象的信息,开发者要使用的对象都从spring容器中获取。在工厂模式会做更具体的描述讲解