why does my aspect code not run when exception is thrown?2019 Community Moderator ElectionHow do you assert that a certain exception is thrown in JUnit 4 tests?Why does Java have transient fields?Spring, Aspect J, multiple AfterThrowing advice applied to the same pointcutIntelliJ inspection gives “Cannot resolve symbol” but still compiles codeSpring Aspect not executed when defined in other JARWhy does this code using random strings print “hello world”?Why is executing Java code in comments with certain Unicode characters allowed?AspectJ and Spring: Exclude @After method execution when method throws an exceptionHow to log exception details in Spring AOP with try with empty catch block?java exception - why does it catch?
Was this cameo in Captain Marvel computer generated?
What exactly is the meaning of "fine wine"?
Short story about cities being connected by a conveyor belt
Short story about an infectious indestructible metal bar?
Should we avoid writing fiction about historical events without extensive research?
How to distinguish easily different soldier of ww2?
Is this a crown race?
How would an energy-based "projectile" blow up a spaceship?
Is it a Cyclops number? "Nobody" knows!
What is the orbit and expected lifetime of Crew Dragon trunk?
Is it appropriate to ask a former professor to order a library book for me through ILL?
How do you make a gun that shoots melee weapons and/or swords?
Insult for someone who "doesn't know anything"
Draw this image in the TIKZ package
Unfamiliar notation in Diabelli's "Duet in D" for piano
Are small insurances worth it?
Unidentified signals on FT8 frequencies
What is the purpose of a disclaimer like "this is not legal advice"?
A running toilet that stops itself
Will the concrete slab in a partially heated shed conduct a lot of heat to the unconditioned area?
How can I paste into a layer with a layer mask?
What can I do if someone tampers with my SSH public key?
How strong is the axiom of well-ordered choice?
How can I have x-axis ticks that show ticks scaled in powers of ten?
why does my aspect code not run when exception is thrown?
2019 Community Moderator ElectionHow do you assert that a certain exception is thrown in JUnit 4 tests?Why does Java have transient fields?Spring, Aspect J, multiple AfterThrowing advice applied to the same pointcutIntelliJ inspection gives “Cannot resolve symbol” but still compiles codeSpring Aspect not executed when defined in other JARWhy does this code using random strings print “hello world”?Why is executing Java code in comments with certain Unicode characters allowed?AspectJ and Spring: Exclude @After method execution when method throws an exceptionHow to log exception details in Spring AOP with try with empty catch block?java exception - why does it catch?
In my spring project I've added two dependencies:
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>1.9.2</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.9.2</version>
</dependency>
and then I've created a class:
package com.my.company.package.handling;
@Aspect
public class MyAspect
@AfterThrowing(pointcut = "execution(* com.my.company.package.*(..))", throwing = "ex")
public void logAfterThrowing(Exception ex)
System.out.println("exception "+ex.getLocalizedMessage())
Now in some other class (stored in package: com.my.company.package.someOtherPackage) I'm throwing exception:
throw new IOException("here comes error");
but then I don't see the printout from my aspect method in the console. What am I missing here?
java spring aspectj
add a comment |
In my spring project I've added two dependencies:
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>1.9.2</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.9.2</version>
</dependency>
and then I've created a class:
package com.my.company.package.handling;
@Aspect
public class MyAspect
@AfterThrowing(pointcut = "execution(* com.my.company.package.*(..))", throwing = "ex")
public void logAfterThrowing(Exception ex)
System.out.println("exception "+ex.getLocalizedMessage())
Now in some other class (stored in package: com.my.company.package.someOtherPackage) I'm throwing exception:
throw new IOException("here comes error");
but then I don't see the printout from my aspect method in the console. What am I missing here?
java spring aspectj
1
isn't there missing a.*for the method name? I don't use AspectJ but documentation states: "execution of any method defined in the service package:execution(* com.xyz.service.*.*(..))" (assuming that yoursomeOtherPackageis a class), or: " execution of any method defined in the service package or a sub-package:execution(* com.xyz.service..*.*(..))"
– Carlos Heuberger
2 days ago
@CarlosHeuberger just realised the same thing, I've updated my answer.
– Essex Boy
2 days ago
add a comment |
In my spring project I've added two dependencies:
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>1.9.2</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.9.2</version>
</dependency>
and then I've created a class:
package com.my.company.package.handling;
@Aspect
public class MyAspect
@AfterThrowing(pointcut = "execution(* com.my.company.package.*(..))", throwing = "ex")
public void logAfterThrowing(Exception ex)
System.out.println("exception "+ex.getLocalizedMessage())
Now in some other class (stored in package: com.my.company.package.someOtherPackage) I'm throwing exception:
throw new IOException("here comes error");
but then I don't see the printout from my aspect method in the console. What am I missing here?
java spring aspectj
In my spring project I've added two dependencies:
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>1.9.2</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.9.2</version>
</dependency>
and then I've created a class:
package com.my.company.package.handling;
@Aspect
public class MyAspect
@AfterThrowing(pointcut = "execution(* com.my.company.package.*(..))", throwing = "ex")
public void logAfterThrowing(Exception ex)
System.out.println("exception "+ex.getLocalizedMessage())
Now in some other class (stored in package: com.my.company.package.someOtherPackage) I'm throwing exception:
throw new IOException("here comes error");
but then I don't see the printout from my aspect method in the console. What am I missing here?
java spring aspectj
java spring aspectj
asked 2 days ago
randomuser1randomuser1
1,07431539
1,07431539
1
isn't there missing a.*for the method name? I don't use AspectJ but documentation states: "execution of any method defined in the service package:execution(* com.xyz.service.*.*(..))" (assuming that yoursomeOtherPackageis a class), or: " execution of any method defined in the service package or a sub-package:execution(* com.xyz.service..*.*(..))"
– Carlos Heuberger
2 days ago
@CarlosHeuberger just realised the same thing, I've updated my answer.
– Essex Boy
2 days ago
add a comment |
1
isn't there missing a.*for the method name? I don't use AspectJ but documentation states: "execution of any method defined in the service package:execution(* com.xyz.service.*.*(..))" (assuming that yoursomeOtherPackageis a class), or: " execution of any method defined in the service package or a sub-package:execution(* com.xyz.service..*.*(..))"
– Carlos Heuberger
2 days ago
@CarlosHeuberger just realised the same thing, I've updated my answer.
– Essex Boy
2 days ago
1
1
isn't there missing a
.* for the method name? I don't use AspectJ but documentation states: "execution of any method defined in the service package: execution(* com.xyz.service.*.*(..))" (assuming that your someOtherPackage is a class), or: " execution of any method defined in the service package or a sub-package: execution(* com.xyz.service..*.*(..))"– Carlos Heuberger
2 days ago
isn't there missing a
.* for the method name? I don't use AspectJ but documentation states: "execution of any method defined in the service package: execution(* com.xyz.service.*.*(..))" (assuming that your someOtherPackage is a class), or: " execution of any method defined in the service package or a sub-package: execution(* com.xyz.service..*.*(..))"– Carlos Heuberger
2 days ago
@CarlosHeuberger just realised the same thing, I've updated my answer.
– Essex Boy
2 days ago
@CarlosHeuberger just realised the same thing, I've updated my answer.
– Essex Boy
2 days ago
add a comment |
1 Answer
1
active
oldest
votes
Assuming everything else is correct you need a @Component annotation also, you also need another * in the execution string for any class.
@Aspect
@Component
public class MyAspect
@AfterThrowing(pointcut = "execution(* com.my.company.package.*.*(..))", throwing = "ex")
public void logAfterThrowing(Exception ex)
System.out.println("exception "+ex.getLocalizedMessage())
Here's a working example
Thanks, however when I used it - I'm not seeing the message fromlogAfterThrowing. I tried to invoke it in my code by throwing e.g.throw new ParseException("msg", 0)orthrow new NullPointerException("msg");but message from Aspect does not show up
– randomuser1
2 days ago
@randomuser1 it works for me in the test, something else wrong?
– Essex Boy
2 days ago
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55023224%2fwhy-does-my-aspect-code-not-run-when-exception-is-thrown%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
Assuming everything else is correct you need a @Component annotation also, you also need another * in the execution string for any class.
@Aspect
@Component
public class MyAspect
@AfterThrowing(pointcut = "execution(* com.my.company.package.*.*(..))", throwing = "ex")
public void logAfterThrowing(Exception ex)
System.out.println("exception "+ex.getLocalizedMessage())
Here's a working example
Thanks, however when I used it - I'm not seeing the message fromlogAfterThrowing. I tried to invoke it in my code by throwing e.g.throw new ParseException("msg", 0)orthrow new NullPointerException("msg");but message from Aspect does not show up
– randomuser1
2 days ago
@randomuser1 it works for me in the test, something else wrong?
– Essex Boy
2 days ago
add a comment |
Assuming everything else is correct you need a @Component annotation also, you also need another * in the execution string for any class.
@Aspect
@Component
public class MyAspect
@AfterThrowing(pointcut = "execution(* com.my.company.package.*.*(..))", throwing = "ex")
public void logAfterThrowing(Exception ex)
System.out.println("exception "+ex.getLocalizedMessage())
Here's a working example
Thanks, however when I used it - I'm not seeing the message fromlogAfterThrowing. I tried to invoke it in my code by throwing e.g.throw new ParseException("msg", 0)orthrow new NullPointerException("msg");but message from Aspect does not show up
– randomuser1
2 days ago
@randomuser1 it works for me in the test, something else wrong?
– Essex Boy
2 days ago
add a comment |
Assuming everything else is correct you need a @Component annotation also, you also need another * in the execution string for any class.
@Aspect
@Component
public class MyAspect
@AfterThrowing(pointcut = "execution(* com.my.company.package.*.*(..))", throwing = "ex")
public void logAfterThrowing(Exception ex)
System.out.println("exception "+ex.getLocalizedMessage())
Here's a working example
Assuming everything else is correct you need a @Component annotation also, you also need another * in the execution string for any class.
@Aspect
@Component
public class MyAspect
@AfterThrowing(pointcut = "execution(* com.my.company.package.*.*(..))", throwing = "ex")
public void logAfterThrowing(Exception ex)
System.out.println("exception "+ex.getLocalizedMessage())
Here's a working example
edited 2 days ago
answered 2 days ago
Essex BoyEssex Boy
4,6781816
4,6781816
Thanks, however when I used it - I'm not seeing the message fromlogAfterThrowing. I tried to invoke it in my code by throwing e.g.throw new ParseException("msg", 0)orthrow new NullPointerException("msg");but message from Aspect does not show up
– randomuser1
2 days ago
@randomuser1 it works for me in the test, something else wrong?
– Essex Boy
2 days ago
add a comment |
Thanks, however when I used it - I'm not seeing the message fromlogAfterThrowing. I tried to invoke it in my code by throwing e.g.throw new ParseException("msg", 0)orthrow new NullPointerException("msg");but message from Aspect does not show up
– randomuser1
2 days ago
@randomuser1 it works for me in the test, something else wrong?
– Essex Boy
2 days ago
Thanks, however when I used it - I'm not seeing the message from
logAfterThrowing. I tried to invoke it in my code by throwing e.g. throw new ParseException("msg", 0) or throw new NullPointerException("msg"); but message from Aspect does not show up– randomuser1
2 days ago
Thanks, however when I used it - I'm not seeing the message from
logAfterThrowing. I tried to invoke it in my code by throwing e.g. throw new ParseException("msg", 0) or throw new NullPointerException("msg"); but message from Aspect does not show up– randomuser1
2 days ago
@randomuser1 it works for me in the test, something else wrong?
– Essex Boy
2 days ago
@randomuser1 it works for me in the test, something else wrong?
– Essex Boy
2 days ago
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55023224%2fwhy-does-my-aspect-code-not-run-when-exception-is-thrown%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
isn't there missing a
.*for the method name? I don't use AspectJ but documentation states: "execution of any method defined in the service package:execution(* com.xyz.service.*.*(..))" (assuming that yoursomeOtherPackageis a class), or: " execution of any method defined in the service package or a sub-package:execution(* com.xyz.service..*.*(..))"– Carlos Heuberger
2 days ago
@CarlosHeuberger just realised the same thing, I've updated my answer.
– Essex Boy
2 days ago