Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6819123
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T21:16:06+00:00 2026-05-26T21:16:06+00:00

this is my first and also I am beginner in Spring.NET and also AOP.

  • 0

this is my first and also I am beginner in Spring.NET and also AOP.

I would like use Aspect for Exception Hadling for replacing, wrap and modify my custom exceptions.

First I defined some entity and DAO. From method Save in DAO I will throw my exception.

FYI: This is silly sample

Entity:

namespace ExceptionHandlingTutorial.Entities
{
    public class Customer
    {
        public long Id { get; set; }
        public string Name { get; set; }
    }
}

DAO:

namespace ExceptionHandlingTutorial.Dao
{
    public interface ICustomerDao
    {
        void Save(Customer customer);
    }

    public class CustomerDao:ICustomerDao
    {
        #region Implementation of ICustomerDao

        public void Save(Customer customer)
        {
            throw new CustomerException(
                string.Format("Customer with id {0} already exist in repository",customer.Id));
        }

        #endregion
    }
}

CustomException class definition is here:

namespace ExceptionHandlingTutorial
{
    public class CustomerException : Exception
    {
        public CustomerException(string msg)
            : base(msg)
        {

        }
    }
}

In app.config I defined CustomerDao object and ExceptionHandlerAdvice object which only replace CustomerException for System.ArgumentException.

I am not sure if ExceptionHandlerAdvice is auto-proxy and also I don’t how it identified targets.

I believe that it uses SpEL for define rules and when there is some exception it throws check list.
Ok this type of exception is in list some I will apply advice.

Can anybody explain to me how this aspect identified targets? For example I would like apply this aspect only for a few objects not all.

I use ref documentation Chapter 14.3 Exception handling but I couldnt find these information.

Here is app.config:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>

  <configSections>
    <sectionGroup name="spring">
      <section name="context" type="Spring.Context.Support.ContextHandler, Spring.Core"/>
      <section name="objects" type="Spring.Context.Support.DefaultSectionHandler, Spring.Core"/>
    </sectionGroup>
  </configSections>

  <spring>

    <context>
      <resource uri="config://spring/objects"/>
    </context>

    <objects xmlns="http://www.springframework.net">

      <object id="customerDao" 
              type="ExceptionHandlingTutorial.Dao.CustomerDao, ExceptionHandlingTutorial"/>

      <object id="exceptionHandlerAdvice" 
              type="Spring.Aspects.Exceptions.ExceptionHandlerAdvice, Spring.Aop">

        <property name="ExceptionHandlers">
          <list>
            <value>on exception name CustomerException replace System.ArgumentException 'Something'</value>
          </list>
        </property>

      </object>

    </objects>

  </spring>
</configuration>

My main problem is that if I call method Save on DAO it throw exception type of CustomerException this exception is not replaced. Why?

try
            {
                var context = ContextRegistry.GetContext();
                var customerDao = (ICustomerDao)context["customerDao"];

                customerDao.Save(new Customer { Id = 1, Name = "Customer_1" });
            }
            catch (Exception ex)
            {

                Console.WriteLine(string.Format("Exception type: {0}\nException message: {1}\n",
                    ex.GetType(),ex.Message));
            }

Thrown exception is type of CustomerException not ArgumentException,

Also I tried use DSL for defined rules when advice apply:

on exception (#e is T(ExceptionHandlingTutorial.CustomerException) translate new System.ArgumentException('XChange, Method Name'+ #method.Name, #e))

But still is throw exception type of CustomerException.

Thank you for help.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T21:16:07+00:00Added an answer on May 26, 2026 at 9:16 pm

    Spring.NET aop dynamically creates a proxy for the objects you want to apply advice to. Spring.NET returns this proxy when you do var customerDao = (ICustomerDao)context["customerDao"];. So when configured correctly, you don’t get a CustomerDao instance, but an aop proxy from Spring implementing the ICustomerDao interface. This proxy intercepts the call to Save(...) and applies your exception handling advice.

    However, you haven’t configured a ProxyFactory, so you will not get a proxy, but a CustomerDao instance when calling var customerDao = (ICustomerDao)context["customerDao"];. You can check this in the debugger; inspect the (runtime) type of customerDao.

    There are several methods to configure the proxy factory; I’d advise you to use an AdvisorAutoProxy for your case, but you can also configure it manually for each object using a plain ProxyFactoryObject or another AutoProxy.

    When using an AdvisorAutoProxy, you have to add the following object definitions to your configuration:

    <object id="ExceptionAdvisorForSaveMethods" 
            type="Spring.Aop.Support.RegularExpressionMethodPointcutAdvisor, Spring.Aop">
      <property name="advice" ref="exceptionHandlerAdvice"/>
      <property name="patterns">
        <list>
          <value>.*Save.*</value>
        </list>
      </property>
    </object>
    
    <object id="ProxyCreator" 
            type="Spring.Aop.Framework.AutoProxy.DefaultAdvisorAutoProxyCreator, Spring.Aop" />
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm a beginner and this is my first time encountering problem like this. I'm
I have a JSON data like this: { hello: { first:firstvalue, second:secondvalue }, hello2:
I'm a beginner at this. I'm using mysql connector .net to connect vb.net with
I'm a (near complete) beginner, and this is my first foray into encryption -
This is NOT a homework. I'm a beginner in programming, and also this is
hi all this is my first question. I am beginner with web apps totally.
I am a beginner at Python, and this is my first post, so don't
This first bit works: $my_id = 617; $post_id_7 = get_post($my_id); $title = $post_id_7->post_excerpt; echo
Saw this piece of code in a Ruby on Rails book. This first one
problem euler #5 i found the solution but i don't know why this first

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.