我怎样才能Spring.NET的注入中的方法呢? [英] How can I spring.net inject in to methods?

查看:202
本文介绍了我怎样才能Spring.NET的注入中的方法呢?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我贴到下面的spring.net论坛,但也希望我可能会得到一些价值的观点在这里:



我在看一些建议,我怎么可能达到下列要求。



首先一些背景 - 我使用Spring.NET实现IOC depdenecny注射到我的asp.net C#多层次的Web应用程序。注射是通过用我所有的开发由对接口开发并在必要时在一个接口实现类注入实现spring.net xml配置文件来实现的。这一切对我来说工作正常。我的知识是中等水平,我相信。



我在的问题,并希望实现它的解决方案运行。



要采取例如我有几种方法,他们都是从数据库中检索,被缓存一段时间数据的类。



我想有自由在注入到每个方法缓存如的特性。时间就住在缓存等,因此,而不是在课堂注入到需要它的地方,我也希望能够到方法注入值。



我可以沿着类每种方法创建属性的办法去,但这种变messay,或注射在设置上我的课。我的选择是控制通过Spring注入每个方法。



因此,如何,我希望它的东西比其他人可能都遇到过这样就可以实现任何想法。



一个可能的想法,我想出是以某种方式通过弹簧与在该方法中如缓存持续时间等所需的值具有属性的特性注入到每个方法的属性这是一个可行的解决方案?如果是这样,任何人都可以帮助我在正确配置。



或者,如果任何人有任何其他想法,这将是巨大的。

解决方案

如果你把bbaia的做法,它可能看起来像下面的例子。
这是一个有点简单,但是你可以将其应用到您的情况。
假设我们有一个接口 IDoWorkForSomeTime 用一个参数时间

 公共接口IDoWorkForSomeTime 
{
无效工作(INT时间);
}



它是由实施 TimeBoundWorker AdvisedTimeBoundWorker

 公共类TimeBoundWorker:IDoWorkForSomeTime 
{
公共无效工作(INT时间)
{
Console.WriteLine(工作{0}小时的时间);
}
}

公共类AdvisedTimeBoundWorker:IDoWorkForSomeTime
{
/ * ***注意***属性* /
[ ChangeParameter]
公共无效工作(INT时间)
{
Console.WriteLine(时间{0}小时工作);
}
}



然后,我们可以配置一个AOP代理更改时间在运行参数,
这样,当我们运行以下程序:

 类节目
{
静态无效的主要(字串[] args)
{
IApplicationContext接口CTX =新XmlApplicationContext(objects.xml);
VAR工人=(IDoWorkForSomeTime)ctx.GetObject(plainWorker);
VAR advisedWorker =(IDoWorkForSomeTime)ctx.GetObject(advisedWorker);

worker.Work(4);
advisedWorker.Work(4);
}
}



它输出:



<预类=郎无prettyprint-覆盖> 工作4小时
8小时$ b $工作b

所以,虽然我与价值4调用它, advisedWorker 使用值8,我在Spring配置文件中配置。 Spring容器看到​​ [ChangeParameter] 属性,并从我的配置,它有应用以下方法拦截器都知道:

 公共类ChangeParamInterceptor:IMethodInterceptor 
{
公众诠释的NewValue {搞定;组; } //春季cofig

公共对象调用(IMethodInvocation调用)
{
invocation.Arguments设置[0] =的NewValue; //改变参数
对象RVAL = invocation.Proceed();
返回RVAL;
}
}



这需要在 objects.xml :



<预类=郎咸平的XML prettyprint-覆盖> < XML版本= 1.0编码=UTF-8>?;
<对象的xmlns =http://www.springframework.net>

<对象ID =plainWorker
型=Examples.Aop.Shared.TimeBoundWorker,Examples.Aop.Shared
单=真正的>
< /对象>

<对象ID =advisedWorker
型=Examples.Aop.Shared.AdvisedTimeBoundWorker,Examples.Aop.Shared
单=真正的>
< /对象> !

< - AOP配置: - >

<对象ID =changeParamAdvice
型=Examples.Aop.Shared.ChangeParamInterceptor,Examples.Aop.Shared>
< - !啊!有8 - >
<属性名=的NewValue值=8/>
< /对象>

<对象ID =attributePointcutTYPE =Spring.Aop.Support.AttributeMatchMethodPointcut,Spring.Aop>
<属性名=属性VALUE =Examples.Aop.Shared.ChangeParameterAttribute,Examples.Aop.Shared/>
< /对象>

<对象ID =changeParamAspectTYPE =Spring.Aop.Support.DefaultPointcutAdvisor,Spring.Aop>
<属性名=切入点REF =attributePointcut/>
<属性名=忠告REF =changeParamAdvice/>
< /对象>

<对象ID =ProxyCreator
型=Spring.Aop.Framework.AutoProxy.DefaultAdvisorAutoProxyCreator,Spring.Aop/>
< /对象>

您必须配置AOP和应用建议,如 ChangeParamInterceptor <更多选项/ code>。详情请阅读 Spring.NET文档上AOP


I posted to following on the spring.net forum but also hoped I may get some value views here:

I am looking at some advice as to how I might achieve the following requirement.

Firstly some background - I am using Spring.NET to achieve IOC depdenecny injection in to my asp.net c# multi tier web application. Injection is achieved via spring.net xml configuration file with all my development achieved by developing against interfaces and injecting in an interface implemented class where required. This all works fine for me. My knowledge would be intermediate level I believe.

I have ran in to the problem and looking to achieve a solution for it.

To take for example I have a class with several methods where they all retrieve from a database, data being cached for a period of time.

I want to have freedom to inject in to each method the characteristics of the caching eg. time for it to live in cache etc. So rather than inject in class to where its required, I also want to be able to inject values in to methods.

I could go along the approach of creating properties on the class for each method but this gets messay, or injecting in settings to my class. My preference is to control each method via spring injection.

So any ideas how this can be achieved as I expect it something than others may have encountered.

One possible idea that I have come up with is to somehow inject an attribute on to each of the method via spring with attribute having properties with the values that required in the method eg cache duration etc. Is this a feasible solution? If so, could anyone assist me with configuring such.

Or if anyone has any other ideas, it would be great.

解决方案

If you take bbaia's approach, it might look like the example below. It's a bit simplified, but you can apply it to your situation. Let's assume we have an interface IDoWorkForSomeTime with a single parameter time:

public interface IDoWorkForSomeTime
{
    void Work(int time);
}

It's implemented by TimeBoundWorker and AdvisedTimeBoundWorker:

public class TimeBoundWorker : IDoWorkForSomeTime
{
    public void Work(int time)
    {
        Console.WriteLine("Working for {0} hours", time);
    }
}

public class AdvisedTimeBoundWorker : IDoWorkForSomeTime
{
    /* *** Note The Attribute *** */
    [ChangeParameter]
    public void Work(int time)
    {
        Console.WriteLine("Working for {0} hours", time);
    }
}

Then we can configure an AOP proxy to change the time parameter at runtime, so that when we run the following program:

class Program
{
    static void Main(string[] args)
    {
        IApplicationContext ctx = new XmlApplicationContext("objects.xml");
        var worker = (IDoWorkForSomeTime)ctx.GetObject("plainWorker");
        var advisedWorker = (IDoWorkForSomeTime)ctx.GetObject("advisedWorker");

        worker.Work(4);
        advisedWorker.Work(4);
    }
}

It outputs:

Working for 4 hours
Working for 8 hours

So although I call it with value 4, advisedWorker uses the value 8, which I configured in my spring config file. The spring container sees the [ChangeParameter] attribute and knows from my configuration that it has to apply the following method interceptor:

public class ChangeParamInterceptor : IMethodInterceptor
{
    public int NewValue { get; set; }  // set in spring cofig

    public object Invoke(IMethodInvocation invocation)
    {
        invocation.Arguments[0] = NewValue; // change the argument
        object rval = invocation.Proceed();
        return rval;
    }
}

It requires this spring configuration in objects.xml:

<?xml version="1.0" encoding="utf-8" ?>
<objects xmlns="http://www.springframework.net">

  <object id="plainWorker"
          type="Examples.Aop.Shared.TimeBoundWorker, Examples.Aop.Shared"
          singleton="true">
  </object>

  <object id="advisedWorker"
          type="Examples.Aop.Shared.AdvisedTimeBoundWorker, Examples.Aop.Shared"
          singleton="true">
  </object>

  <!-- AOP configuration: -->

  <object id="changeParamAdvice"
          type="Examples.Aop.Shared.ChangeParamInterceptor, Examples.Aop.Shared">
    <!-- AH! there's the 8 -->
    <property name="NewValue" value="8" />
  </object>

  <object id="attributePointcut" type="Spring.Aop.Support.AttributeMatchMethodPointcut, Spring.Aop">
    <property name="Attribute" value="Examples.Aop.Shared.ChangeParameterAttribute, Examples.Aop.Shared" />
  </object>

  <object id="changeParamAspect" type="Spring.Aop.Support.DefaultPointcutAdvisor, Spring.Aop">
    <property name="Pointcut" ref="attributePointcut" />
    <property name="Advice" ref="changeParamAdvice"/>
  </object>

  <object id="ProxyCreator" 
          type="Spring.Aop.Framework.AutoProxy.DefaultAdvisorAutoProxyCreator, Spring.Aop" />     
</objects>

You have many more options for configuring AOP and applying advices such as ChangeParamInterceptor. Read more in the Spring.NET documentation on AOP.

这篇关于我怎样才能Spring.NET的注入中的方法呢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆