View Full Version : Step by step
allanfagner
10-03-2006, 07:56 PM
Hi there people!
I'm a new one on AOP and I'm having a hard time trying to configure and run Spring. Can anyone help me out?
I'd like a simple exemple to how to use Spring (configuring app.config, defining interface, classe, how to use classes and interfaces, etc).
I've read the principal concepts (join points, pointcuts, advices...) but I couldn't figure out how they work together.
If anyone could help, I'd appriciate! :D
Bruno Baia
10-03-2006, 11:43 PM
Hi,
For a simple example, you can take a look to the "Spring.Examples.AopQuickStart" sample from the distribution and the associated documentation :
Chapter 17. AOP Guide (http://www.springframework.net/doc/reference/html/aop-quickstart.html)
Hope this helps,
Bruno
allanfagner
10-04-2006, 01:48 PM
Thanks, Bruno.
I checked that out. What I still can't understand how the whole thing works. I mean, what each config line does, how can I reuse the code.
I can't see how take advantage of Spring and AOP.
By the way, I've seen this on documentation:
ICommand command = (ICommand) ctx["myServiceObject"];
command.Execute();
What should I do to work on that way?
Thanks!
allanfagner
10-04-2006, 05:33 PM
Hi there again.
I'm having another problem. Let's say I have a class and two methods.
class Perstist : IPerstist
{
public sub SaveData()
{
...
}
public sub DeleteData()
{
...
}
}
To execute methods, I do like that:
ProxyFactory factory = new ProxyFactory(new Perstist());
factory.AddAdvice(new InsertBeforeAdvice());
factory.AddAdvice(new InsertAfterAdvice());
factory.SaveData();
// or factory.DeleteData();
That works, but, the whole point is that I have diferent advices to SaveData and DeleteData. How can I do that?
Is there any way to configure an advice to run after or before several methods? e.g.: I want to star a transaction (by advice) and call a commit or rollback (also by advice) every time I call a method called save[entity]. Any idea?
Bruno Baia
10-04-2006, 08:57 PM
Hi,
Spring.AOP can be used in 2 different manners :
- With the Spring's IoC container (configuration files, IApplicationContext, etc.)
- Programmatically with the ProxyFactory class like u did
To apply advices to specific methods, you need to specify a poincut.
You can use a Advisor (Advice + Poincut) to do that.
Here is an example by programmation using Spring.Aop.Support.NameMatchMethodPointcutAdvisor that matches method on which to apply the advice by name :
NameMatchMethodPointcutAdvisor consoleLoggingAroundAdvisor = new NameMatchMethodPointcutAdvisor();
consoleLoggingAroundAdvisor.MappedName = "Do*";
consoleLoggingAroundAdvisor.Advice = new ConsoleLoggingAroundAdvice();
ProxyFactory factory = new ProxyFactory(new ServiceCommand());
factory.AddAdvisor(consoleLoggingAroundAdvisor);
ICommand command = (ICommand) factory.GetProxy();
command.Execute();
command.DoExecute();
-Bruno
allanfagner
10-05-2006, 02:24 PM
Thanks Bruno! That worked just fine!
But, I still have some doubts (sorry keep bugging you :( )
As I said, the example has worked just fine! Now, I'd like to know if there's a way to do that in some place (in xml configuration file, i guess) and apply that to whole application. Got it? I'm working on a web application.
How can I manage transaction using AOP? Can I pass the transaction object as a parameter to BeforeAdvice, AfterAdvice and ThrowAdvice methods? How can i do that?
And, at least, is Spring free? Can I distribute it with my application without costs?
Aleks Seovic
10-05-2006, 03:31 PM
Hi,
Yes, it is possible to do what you are trying to do using declarative approach, by configuring necessary advices and proxies in the Spring config file.
There is extensive documentation (http://www.springframework.net/doc/reference/html/aop.html) on how to use Spring AOP within your application, as well as a number of AOP samples that ship with Spring that you can review.
And to answer your last question, yes, Spring is free and you can ship it with your application with no restrictions.
Regards,
Aleks
allanfagner
10-05-2006, 03:53 PM
Thanks Aleks!
I've read the documentation, but I still can't figure it out how to do that. The file's name is Spring.config. That's it?
Could you, or anyone, put the example above posted by bruno in a (complete) config file? I'd appreciate!
And, how about to how manage transctions using AOP?
Again, sorry keep bugging all of you, but I'm a fresh one, and I'm full of doubts. :(
Bruno Baia
10-06-2006, 07:09 PM
Hi,
Could you, or anyone, put the example above posted by bruno in a (complete) config file? I'd appreciate!
I've used the example provided in the AOP Guide documentation :
Using Pointcuts - the basics (http://www.springframework.net/doc/reference/html/aop-quickstart.html#aop-quickstart-basics-pointcuts)
Here is the complete app.config file for the AopQuickStart example :
<?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">
<description>An example that demonstrates simple AOP functionality.</description>
<object id="consoleLoggingAroundAdvice" type="Spring.Aop.Support.RegularExpressionMethodPointcut Advisor">
<property name="pattern" value="Do"/>
<property name="advice">
<object type="Spring.Examples.AopQuickStart.ConsoleLoggingAround Advice"/>
</property>
</object>
<object id="myServiceObject"
type="Spring.Aop.Framework.ProxyFactoryObject">
<property name="target">
<object id="myServiceObjectTarget"
type="Spring.Examples.AopQuickStart.ServiceCommand"/>
</property>
<property name="interceptorNames">
<list>
<value>consoleLoggingAroundAdvice</value>
</list>
</property>
</object>
</objects>
</spring>
</configuration>
and the code using it :
IApplicationContext ctx = ContextRegistry.GetContext();
ICommand command = (ICommand)ctx["myServiceObject"];
command.DoExecute();
command.Execute();
I've read the documentation, but I still can't figure it out how to do that. The file's name is Spring.config. That's it?
In the example below, Spring uses default .NET configuration infrastructure(app.config or Web.config files) to get an instance of the IApplicationContext via ContextRegistry.GetContext.
And, how about to how manage transctions using AOP?
Spring.Data provides transaction management based on AOP, there is no documentation at this moment, but you can have a look to this thread to see how it looks :
http://forum.springframework.net/showthread.php?t=728
Hope this helps,
Bruno
allanfagner
10-09-2006, 06:40 PM
Got it, Bruno! Thanks! :mrgreen:
I realize now how to configurate the xml file and work with context!
But, I've taken a look at documentation for xml schema and haven't found anything.
Wich schema shoud I use?
PS: Sorry about the stupid doubts.
allanfagner
10-09-2006, 06:43 PM
BTW, I'm getting this error message:
Unrecognized configuration section spring.
allanfagner
10-09-2006, 07:15 PM
I've found the xsd file and put it on its proper place.
But, I'm still getting that error message. :(
Bruno Baia
10-10-2006, 04:50 AM
Hi,
About the xsd schema :
Chapter 15. Visual Studio.NET Integration (http://www.springframework.net/doc/reference/html/vsnet.html)
About the "Unrecognized configuration section spring" error, be sure that your configuration file is well-formed :
<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">
...
</objects>
</spring>
</configuration>
Hope this helps,
Bruno
vBulletin® v3.7.3, Copyright ©2000-2008, Jelsoft Enterprises Ltd.