ERLEDIGT
NEIN
NEIN
ANTWORTEN
2
2
ZUGRIFFE
1100
1100
EMPFEHLEN
-
04.06.07 22:53 #1
- Registriert seit
- Mar 2005
- Beiträge
- 743
Hallo Leute,
gibts eine Möglichkeit, wo ich dann sagen kann, bevor bestimmte Klassen Methode-/n ausgeführt werden, sollte zuerst ein andere Methode ausgeführt werden. D.h. ruf zuerst die Methode auf bevor du den anderen aufrufst.
Gruß
erkan
-
04.06.07 23:14 #2
- Registriert seit
- Jun 2002
- Ort
- Saarbrücken (Saarland)
- Beiträge
- 9.886
- Blog-Einträge
- 29
Hallo,
das kann man ganz einfach mit Dynamic Proxies realisieren:
Code java:1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98
/** * */ package de.tutorials; import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; import java.lang.reflect.InvocationHandler; import java.lang.reflect.Method; import java.lang.reflect.Proxy; /** * @author Tom */ public class DynamicProxyExample { /** * @param args */ public static void main(String[] args) throws Exception { IService service = (IService) Proxy.newProxyInstance(DynamicProxyExample.class.getClassLoader(), new Class[] { IService.class }, new TargetAwareSecureMethodInvocationHandler<IService>(new Service())); service.businessOperation(); System.out.println("########"); service.anotherBusinessOperation(); } @Retention(RetentionPolicy.RUNTIME) @Target( { ElementType.METHOD, ElementType.TYPE }) static @interface Secure { } static interface IService { void businessOperation(); @Secure void anotherBusinessOperation(); } static class Service implements IService { public void anotherBusinessOperation() { System.out.println("anotherBusinessOperation"); } public void businessOperation() { System.out.println("businessOperation"); } } static class TargetAwareSecureMethodInvocationHandler<TTarget> implements InvocationHandler { TTarget target; /** * @param target */ public TargetAwareSecureMethodInvocationHandler(TTarget target) { super(); this.target = target; } public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { if (method.isAnnotationPresent(Secure.class)) { checkAccess(method); } return method.invoke(getTarget(), args); } private void checkAccess(Method method) { System.out.println("perform access check for: " + method); } /** * @return the target */ public TTarget getTarget() { return target; } /** * @param target the target to set */ public void setTarget(TTarget target) { this.target = target; } } }
Ausgabe:
Code :1 2 3 4
businessOperation ######## perform access check for: public abstract void de.tutorials.DynamicProxyExample$IService.anotherBusinessOperation() anotherBusinessOperation
Eine weitere Variante wäre auch die Verwendung von AspectJ
Gruß TomJava rocks!
How to become a good Java Programmer?
Does IT in Java and .Net
The only valid measurement of code quality: WTFs / minute
Blog
Xing
Twitter
-
06.06.07 18:37 #3
- Registriert seit
- Mar 2005
- Beiträge
- 743
Danke Thomas.
Es hat funktioniert



Ähnliche Themen
-
Methode in andere Klasse aufrufen?
Von jbjb im Forum Java GrundlagenAntworten: 2Letzter Beitrag: 07.01.10, 21:49 -
Mittels JS eine C# Methode aufrufen
Von WaZZkeSS im Forum .NET Web und KommunikationAntworten: 2Letzter Beitrag: 11.06.09, 20:25 -
Mit VB.Net eine PHP-Methode aufrufen
Von codeman im Forum .NET Web und KommunikationAntworten: 0Letzter Beitrag: 23.11.07, 15:26 -
Methode nach einer bestimmten zeit aufrufen
Von Y05h1 im Forum JavaAntworten: 3Letzter Beitrag: 26.10.05, 12:16 -
Access Violation beim Aufruf einer Methode in DLL
Von exsa im Forum C/C++Antworten: 3Letzter Beitrag: 29.01.05, 17:55





Zitieren

Login





