看看NHibernate中创建/更新列的所有可能性我主要(Stackoverflow问题,Ayende Rahien)看到了Listeners的解决方案.
在我公司编程的程序员使用Interceptor来实现同样的目的.
这两种解决方案有什么区别吗?(其中一个已经过时,是其中一个首选,有哪些优点和/或缺点)
我一直在摆弄CXF上的服务器端拦截器.但似乎实现简单的传入和传出拦截器并不是一项简单的任务,这些拦截器为我提供了包含SOAP XML的纯字符串.
我需要在拦截器中使用纯XML,以便我可以将它们用于特定的日志记录任务.标准的LogIn和LogOut拦截器不能胜任任务.是否有人愿意分享一些关于如何实现一个简单的传入拦截器的例子,它能够获取传入的SOAP XML和一个传出的拦截器来再次获取SOAP XML?
我有一个Angular拦截器工作:
factory('myHttpInterceptor', function ($q, $location, $rootScope) {
// do something
return function (promise) {
return promise.then(function (response) {
// do something
return response;
}, function (response) {
// do something
return $q.reject(response);
});
};
})
Run Code Online (Sandbox Code Playgroud)
和一个包含模板的大html文件<script type="text/ng-template" id="home-template">.不幸的是,我的HTTP拦截器不仅拦截加载HTTP请求,还拦截加载模板(已经加载到html文件中),用于定义为的控制器when('/', {controller:MainController, templateUrl:'home-template'}).有没有办法如何使拦截器只拦截HTTP请求或如何识别我是从服务器加载某些东西还是只是模板?
如何在$resource通话中添加拦截器?
假设我有一个叫做的资源工厂Users,就像这样;
app.factory('Users', ['$resource', 'resourceInterceptor',
function ($resource, resourceInterceptor) {
return $resource(
'users/:user_id',
{
user_id: '@id'
},
{
query: {
method: 'GET', // Not changing the default method, just adding an interceptor
interceptor: resourceInterceptor // Is there any better way to do this.. like globally?
},
save: {
method: 'POST', // Same here
interceptor: resourceInterceptor // Again...
},
..., // And so on
}
);
}]);
Run Code Online (Sandbox Code Playgroud)
我的resourceInterceptor服务看起来像;
app.factory('resourceInterceptor', ['$rootScope',
function ($rootScope) {
return {
request: …Run Code Online (Sandbox Code Playgroud) 我是角度(和编程)的新手,这是一个看似简单的问题,但我无法弄清楚.
一些教程建议使用$httpProvider.interceptors.push('interceptorName')来操纵http请求和响应.
我想了解更多关于拦截器的事情所以我看一下官方文档,但我找不到任何与拦截器相关的东西,只有一个方法(useApplyAsync([value]);)和一个属性(默认值)$httpProvider(docs)).
我从其他教程中知道拦截器是一个常规服务工厂,我知道如何使用它,但我的问题是:因为语法是$httpProvider.interceptors.push('interceptorName'),然后我希望我会找到一个名为"拦截器"的属性$httpProvider,但实际上我可以"T.我想念这个混乱的东西吗?或者我的概念从底部是完全错误的?
我有点问题.我需要调用此拦截器中的每个请求postHandle方法:
public class DiMenuInterceptor extends HandlerInterceptorAdapter {
@Autowired
private IDiCategoryService categoryService;
@Override
public void postHandle(HttpServletRequest request,
HttpServletResponse response, Object handler,
ModelAndView modelAndView) throws Exception {
modelAndView.addObject("category", categoryService.getCategoryInTree());
}
}
Run Code Online (Sandbox Code Playgroud)
所以我把servlet配置到这行,一切正常.
<bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping" p:interceptors-ref="menuInterceptor" />
<bean id="menuInterceptor" class="cz.cosi.DiMenuInterceptor" />
Run Code Online (Sandbox Code Playgroud)
但现在我必须改变配置和使用 <mvc:interceptors>
使用这种配置,我在postHandle方法中的modelAndView上获得了一系列空指针异常,因为每个请求多次调用postHandle方法.
<mvc:interceptors>
<bean class="cz.cosi.DiMenuInterceptor" />
</mvc:interceptors>
Run Code Online (Sandbox Code Playgroud)
使用此配置,它正在工作,但仅适用于请求serverAdress /任何内容.对于请求serverAdress/anything/something是postHandle not called.
<mvc:interceptors>
<mvc:interceptor>
<mvc:mapping path="/*" />
<bean class="cz.cosi.DiMenuInterceptor" />
</mvc:interceptor>
</mvc:interceptors>
Run Code Online (Sandbox Code Playgroud)
servlet配置的一部分
<mvc:annotation-driven />
<bean id="messageSource"
class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
<property name="basename" value="classpath:messages" />
<property name="defaultEncoding" value="UTF-8" />
</bean>
<mvc:resources mapping="/css/**" location="/css/" />
<mvc:resources …Run Code Online (Sandbox Code Playgroud) 我正在寻找在我的控制之外远程调用服务的方法,直到连接成功为止.我也不想简单地设置一个定时器,每隔n秒/分钟执行一次动作,直到成功为止.经过一系列研究后,似乎断路器模式非常适合.
我发现了一个实现,它使用温莎城堡拦截弹,这看起来真棒.唯一的问题是我不知道如何使用它.从几篇文章,我发现关于该主题的唯一使用的例子,我能找到的就是简单地使用断路器只调用一个动作一次,这似乎不是很有用.从那看起来我需要简单地在while(true)循环中使用断路器来运行我的动作.
我如何使用Windsor拦截器执行一个动作来调用外部服务,直到它成功而不会关闭我们的服务器?
有人可以填写丢失的部分吗?
while(true)
{
try
{
service.Subscribe();
break;
}
catch (Exception e)
{
Console.WriteLine("Gotcha!");
Thread.Sleep(TimeSpan.FromSeconds(10));
}
}
Console.WriteLine("Success!");
public interface IService
{
void Subscribe();
}
public class Service : IService
{
private readonly Random _random = new Random();
public void Subscribe()
{
var a = _random.Next(0, 10) % 2421;
if(_random.Next(0, 10) % 2 != 0)
throw new AbandonedMutexException();
}
}
Run Code Online (Sandbox Code Playgroud)
基于此,我想我现在理解这个概念以及如何应用它.
c# design-patterns dependency-injection interceptor circuit-breaker
我在我的Web应用程序中使用Spring-MVC和Spring Security.它包括用户注册页面和私人用户面板.我现在设置了以下网址模式:
whatever/myapp/login 用户登录whatever/myapp/register?step=1 开始注册whatever/myapp/account/** 私人区域视图(页面)whatever/myapp/pending 注册后流程完成后显示的视图whatever/myapp/blocked 帐户被屏蔽的视图whatever/myapp/register/retry 如果注册失败,请允许重试 基本上,下面的这些URL应该要求用户身份验证,即需要登录:
whatever/myapp/account/** (私人区域页面)whatever/myapp/pending (此页面设置了一个定时器,可以重定向到/ account/home)whatever/myapp/register/retry使用Spring安全性非常简单.但是,无论通过Spring安全性进行用户身份验证,私有区域页面都应该是可访问的,具体取决于用户的当前帐户状态(存储在我的数据库中).
更具体地说:如果用户试图访问私有区域(/account/**)中的任何内容,则应根据状态向他显示相应的视图(重定向到适当的页面).我定义了这些状态:
suspended - 涉及待审视图enabled - 允许完全访问disabled - 这里不相关retry_allowed- 涉及重试视图blocked - 涉及帐户被阻止的视图目前,我有一个MVC拦截器设置/account/**,它检查用户状态,并重定向到适当的页面,但不知何故,我感觉这不是真正理想或适当的解决方案,因为我面临奇怪的行为,如多个控制器调用...而且我不太确定何时返回true/ false在preHandle()方法内.这是拦截器的代码片段:
@Override
public boolean preHandle(
HttpServletRequest request,
HttpServletResponse response,
Object arg2)
throws Exception {
IPanelUser pUser = (IPanelUser) SecurityContextHolder.getContext()
.getAuthentication().getPrincipal();
// check principal first and then load from DB
// "suspended" is …Run Code Online (Sandbox Code Playgroud) 我在Android应用程序中使用Retrofit,这反过来意味着我使用OkHttp.我刚刚去了Alpha,并在我的崩溃报告中看到了许多非致命的异常被记录.所有这些都源于我的okhttp拦截器,然后记录的异常似乎都是在网络可能不稳定或连接丢失等情况下有效的事情.
我怎样才能做到这一点,以便这些例外不会记录到崩溃关系中,从而混淆了我对应用程序中出现的异常的看法?
一些例外的例子:
> Non-fatal Exception: javax.net.ssl.SSLHandshakeException
Connection closed by peer
okhttp3.internal.connection.RealConnection.connectTls (RealConnection.java:281)
okhttp3.internal.connection.RealConnection.establishProtocol (RealConnection.java:251)
okhttp3.internal.connection.RealConnection.connect (RealConnection.java:151)
okhttp3.internal.connection.StreamAllocation.findConnection (StreamAllocation.java:192)
okhttp3.internal.connection.StreamAllocation.findHealthyConnection (StreamAllocation.java:121)
okhttp3.internal.connection.StreamAllocation.newStream (StreamAllocation.java:100)
okhttp3.internal.connection.ConnectInterceptor.intercept (ConnectInterceptor.java:42)
okhttp3.internal.http.RealInterceptorChain.proceed (RealInterceptorChain.java:92)
okhttp3.internal.http.RealInterceptorChain.proceed (RealInterceptorChain.java:67)
okhttp3.internal.cache.CacheInterceptor.intercept (CacheInterceptor.java:93)
okhttp3.internal.http.RealInterceptorChain.proceed (RealInterceptorChain.java:92)
okhttp3.internal.http.RealInterceptorChain.proceed (RealInterceptorChain.java:67)
okhttp3.internal.http.BridgeInterceptor.intercept (BridgeInterceptor.java:93)
okhttp3.internal.http.RealInterceptorChain.proceed (RealInterceptorChain.java:92)
okhttp3.internal.http.RetryAndFollowUpInterceptor.intercept (RetryAndFollowUpInterceptor.java:120)
okhttp3.internal.http.RealInterceptorChain.proceed (RealInterceptorChain.java:92)
okhttp3.internal.http.RealInterceptorChain.proceed (RealInterceptorChain.java:67)
MY_INTERCEPTOR.intercept (AuthenticationInterceptor.java:30)
Run Code Online (Sandbox Code Playgroud)
和
> Non-fatal Exception: javax.net.ssl.SSLException
Read error: ssl=0xdee45cc0: I/O error during system call, Software caused connection abort
okio.Okio$2.read (Okio.java:139)
okio.AsyncTimeout$2.read (AsyncTimeout.java:237)
okio.RealBufferedSource.indexOf (RealBufferedSource.java:345)
okio.RealBufferedSource.readUtf8LineStrict (RealBufferedSource.java:217)
okio.RealBufferedSource.readUtf8LineStrict (RealBufferedSource.java:211)
okhttp3.internal.http1.Http1Codec.readResponseHeaders (Http1Codec.java:189)
okhttp3.internal.http.CallServerInterceptor.intercept (CallServerInterceptor.java:75)
okhttp3.internal.http.RealInterceptorChain.proceed (RealInterceptorChain.java:92) …Run Code Online (Sandbox Code Playgroud) 我想拦截Javascript中的fetch API请求和响应.
例如:在发送请求之前要拦截请求URL并且一旦获得响应就想拦截响应.
以下代码用于拦截所有XMLHTTPRequest的响应.
(function(open) {
XMLHttpRequest.prototype.open = function(XMLHttpRequest) {
var self = this;
this.addEventListener("readystatechange", function() {
if (this.responseText.length > 0 && this.readyState == 4 && this.responseURL.indexOf('www.google.com') >= 0) {
Object.defineProperty(self, 'response', {
get: function() { return bValue; },
set: function(newValue) { bValue = newValue; },
enumerable: true,
configurable: true
});
self.response = 'updated value' //Intercepted Value
}
}, false);
open.apply(this, arguments);
};
})(XMLHttpRequest.prototype.open);
Run Code Online (Sandbox Code Playgroud)
我想为Fetch()API实现相同的功能.
提前致谢..