在Ruby和PHP(我猜其他语言)中,只要设置了属性,就会调用一些实用程序方法.(*instance_variable_set*表示Ruby,*__ set*表示PHP).
所以,假设我有一个像这样的C#类:
public class Person
{
public string FirstName { get; set; }
public string LastName { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
现在,假设如果Person调用类中的任何属性设置器,我想先调用另一个方法,然后继续使用setter的默认行为,这同样适用于属性setter.
这可能吗?
编辑: 我想在不定义支持字段的情况下执行此操作.
我在我的应用程序中创建了一个检测会话丢失的拦截器(服务器发送HTTP 419).在这种情况下,我需要从服务器请求一个新会话,然后我想再次自动发送原始请求.
也许我可以在请求拦截器中保存请求,然后再次发送它,但可能有一个更简单的解决方案.
请注意,我必须使用特定的Web服务来创建会话.
angular.module('myapp', [ 'ngResource' ]).factory(
'MyInterceptor',
function ($q, $rootScope) {
return function (promise) {
return promise.then(function (response) {
// do something on success
return response;
}, function (response) {
if(response.status == 419){
// session lost
// create new session server-side
// Session.query();
// then send current request again
// ???
}
return $q.reject(response);
});
};
}).config(function ($httpProvider) {
$httpProvider.responseInterceptors.push('MyInterceptor');
});
Run Code Online (Sandbox Code Playgroud) 我想添加spring mvc拦截器作为Java配置的一部分.我已经有一个基于xml的配置,但我正在尝试转向Java配置.对于拦截器,我知道可以从弹簧文档中这样做 -
@EnableWebMvc
@Configuration
public class WebConfig extends WebMvcConfigurerAdapter {
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(new LocaleInterceptor());
}
}
Run Code Online (Sandbox Code Playgroud)
但我的拦截器正在使用一个自动装入其中的弹簧豆,如下所示 -
public class LocaleInterceptor extends HandlerInterceptorAdaptor {
@Autowired
ISomeService someService;
...
}
Run Code Online (Sandbox Code Playgroud)
SomeService类如下所示 -
@Service
public class SomeService implements ISomeService {
...
}
Run Code Online (Sandbox Code Playgroud)
我正在使用注释@Service来扫描bean,并且没有在配置类中指定它们@Bean
据我所知,由于java配置使用new来创建对象,因此spring不会自动将依赖项注入其中.
我如何添加这样的拦截器作为java配置的一部分?
是否可以使用注释连接Spring MVC Interceptor,如果是这样,有人可以提供一个如何操作的示例吗?
通过注释通过注释,我指的是尽可能少地在XML配置中做.例如,我在http://www.vaannila.com/spring/spring-interceptors.html找到了这个配置文件;
<bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping" p:interceptors-ref="loggerInterceptor" />
<bean id="loggerInterceptor" class="com.vaannila.interceptor.LoggerInterceptor" />
Run Code Online (Sandbox Code Playgroud)
你有多少配置可以逃脱?我想是一个@Autowired将删除在第2行显式声明bean的需要,但是也可以用注释去掉第1行吗?
什么是$ httpProvider.responseInterceptors的替代品,因为它在AngularJS V1.3中已停止使用?
我使用Angular JS 1.2的拦截器现在不能使用1.3版本
var angularErrorHandling = angular.module('xx-http-error-handling', []);
angularErrorHandling.config(function ($provide, $httpProvider, $compileProvider) {
var elementsList = $();
push function to the responseInterceptors which will intercept
the http responses of the whole application
$httpProvider.responseInterceptors.push(function ($timeout, $q) {
return function (promise) {
return promise.then(function (successResponse) {
// if there is a successful response on POST, UPDATE or DELETE we display
// a success message with green background
if (successResponse.config.method.toUpperCase() == 'GET') {
var length = successResponse.data.length;
if (length == …Run Code Online (Sandbox Code Playgroud) 我试图在Spring中使用拦截器.我想在一些方法上实现一个拦截器来处理调用这些方法时的特定逻辑.我也希望与使用Web框架分开,因为我倾向于使用Spring作为后端,没有任何标头.
搜索之后,我认为Spring方法被称为Aspects,你能提一下这方面的最佳实践吗?
Java大师,
我很新,annotations并没有搜索过这个,所以请耐心等待...
我想实现Custom Annotation这将intercept一个方法调用.从非常基本的东西开始,它可以只打印方法名称和参数,以便我可以避免使用该logger语句.
像这样的示例调用:
public MyAppObject findMyAppObjectById(Long id) throws MyCustomException {
log.debug("in findMyAppObjectById(" + id + ")");
//....
}
Run Code Online (Sandbox Code Playgroud)
可以转换成:
@LogMethodCall(Logger.DEBUG)
public MyAppObject findMyAppObjectById(Long id) throws MyCustomException {
//....
}
Run Code Online (Sandbox Code Playgroud)
我可以得到一些关于此的提示吗?
我一直在尝试实现拦截器(OkHttp 3.2和Retrofit 2),以便在作为响应返回之前编辑JSON响应.我们请求数据的服务器返回成功或错误的不同数据依赖性,这使得难以映射对象.
我试图通过将拦截器添加到Retrofit作为NetworkInterceptor来实现,但返回的字符串没有格式.
@Override
public Response intercept(Chain chain) throws IOException {
Request request = chain.request();
Response response = chain.proceed(request);
try {
final String responseString = new String(response.body().bytes() );
LOGD("OkHttp-NET-Interceptor", "Response: " + responseString);
String newResponseString = editResponse( responseString );
LOGD("OkHttp-NET-Interceptor", "Response edited: " + newResponseString);
return response.newBuilder()
.body(ResponseBody.create(response.body().contentType(), newResponseString))
.build();
}catch (Exception ex){
return response;
}
}
Run Code Online (Sandbox Code Playgroud)
responseString有一个没有任何可理解格式的字符串.
在更改为普通拦截器之后,字符串具有能够转换为JSONObject的格式.
可以告诉我某些回复之间的差异吗?
为什么这行新的String(response.body().bytes()); 返回不同的内容?
真的,过滤器和拦截器之间有什么区别?我意识到拦截器会在动作之前和之后触发,递归,并且过滤器可以配置为触发动作和某些url模式.但是你怎么知道何时使用每一个?
在我正在阅读Struts 2的书中,似乎拦截器正在被推动,我甚至按照教程编写了一个Authentication Interceptor以确保用户已登录.但是,如果用户试图访问一个不能访问的URL没有与之关联的动作,拦截器没有抓住它,这意味着我必须将动作与我想要保护的每个jsp相关联.这似乎不对.
我可以创建一个处理URL的身份验证过滤器,这样我就不必这样做了,但那么,拦截器有什么意义呢?
我有几个软件包(A,B和C)部署到OSGi容器,每个包含一个CamelContext和一些路由.我有另一个包(M),CamelContext带有一个路由(用于收集监控数据)和一个InterceptStrategybean.我希望InterceptStrategyM中的bean自动应用于CamelContext容器中的所有其他s(即A,B和C中的那些),而不必修改其他bundle.
最终,目标是将每个数据从CamelContextM中窃听到路线中,而不必对A,B或C进行任何更改以明确路由Exchange.这种方法或类似的方法是否可行?
所有CamelContexts都是使用Spring XML配置的.
更新:附加上下文
捆绑包A,B和C包含负责处理数据的核心产品.Bundle M包含一个可选的监控工具,用于测量流经A,B和C的数据的某些参数.目前,添加可选工具需要更改A,B和C中的路由以添加额外的Processors来丰富Exchange使用监控数据并在<to />端点之前读取监控数据.
目标是能够将Bundle M放入已经过验证的A,B和C系统; 并使其自动应用于现有路由,而无需修改现有和工作捆绑包的配置.这是可以接受的进行修改,以A,B和C,以支持这一点,只要改变不会导致A,B和C依靠M上运行(即ABC仍然必须运行不M).
如果有比使用拦截器更好的方法,我对此持开放态度.主要目标是:
interceptor ×10
java ×3
spring ×3
angularjs ×2
annotations ×2
spring-mvc ×2
android ×1
apache-camel ×1
aspects ×1
autowired ×1
c# ×1
filter ×1
javascript ×1
logging ×1
okhttp3 ×1
osgi ×1
osgi-bundle ×1
retrofit2 ×1
struts2 ×1