Spring注解:如何创建自动装配注释与参数静态/非静态方法 [英] Spring Annotations: How to create autowire annotation for static/non static method with arguments

查看:768
本文介绍了Spring注解:如何创建自动装配注释与参数静态/非静态方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


  1. 使用注释;我如何传递参数值的方法?

    在下面的code我怎么能通为loadProperties API方法的参数(2串)值,通过注释时,confProps实例的自动装配的例子吗?


  2. 我可以在方法的参数级别使用@ javax.inject.Named;但有春季任何同等这在方法参数级别使用?我不能在参数级别使用@Component。


  3. 我能否使用这些@Value(#{} XXX)或@Qualifier(),以解决我的问题?这两个在方法参数级别的支持。


也请纠正我,如果任何其他配置错误我在这里做了。

  @Configuration
公用事业类{    @Bean(NAME =loadProperties)
    @Scope(原型)
    公共静态属性loadProperties(字符串propsFileName,字符串类型)抛出异常{
        返回Utilities.loadPropertiesFile(p_propsFileName);
    }}
@服务
@Scope(值= BeanDefinition.SCOPE_SINGLETON)
@Qualifier(strmContMgr)
公共类StreamingControllerManager {    @Autowired
    @Qualifier(loadProperties)
    属性confProps;
}


解决方案

任何技术一样,春天有它的嘲弄和限制。从你的榜样,你已经开始尝试做使用Spring一切(即使是最简单的事情)。看着你如何到达那里,这可能是有意义的,但你仍然在一个角落里结束了。

或者换一种说法:仅仅因为你的可以并不意味着它的智能

下面是您问题的解决方案:

  @Configuration
类StreamContollerConfig {    @豆
    公共属性streamControllerProperties()抛出异常{
        返回Utilities.loadPropertiesFile(一些/固定/名称);
    }
}

尽量避免运行时的配置豆。他们有很多的复杂性添加到您的产品,往往很少获益。

从构建一个最终的配置块电线和共同编织的一切块应用程序。这样一来,每个块保持独立和简单的。

  1. Using annotations; how do i pass arguments values to a method ?
    Example in the below code "How can i pass arguments (2 strings) values for loadProperties API method" through annotations when Autowiring of confProps instance?

  2. I can use @javax.inject.Named at method argument level; but is there any equivalent for this in Spring to use at method argument level? I am not able to use @Component at the argument level.

  3. Can i use these @Value("#{XXX}") OR @Qualifier("") to resolve my issue? These two are supported at method argument level.

Also please correct me if any other configuration mistakes i did it here.

@Configuration
Class Utilities {

    @Bean(name = "loadProperties")
    @Scope("prototype")
    public static Properties loadProperties(String propsFileName, String type) throws Exception {
        return Utilities.loadPropertiesFile(p_propsFileName);
    }

}


@Service
@Scope(value = BeanDefinition.SCOPE_SINGLETON)
@Qualifier("strmContMgr")
public class StreamingControllerManager {

    @Autowired
    @Qualifier("loadProperties")
    Properties confProps;
}

解决方案

Like any technology, Spring has it's taunts and limits. From your example, you have started to try to do everything (even the most simple things) using Spring. Looking at how you got there, that might make sense but you still ended up in a corner.

Or to put it another way: Just because you can doesn't mean it's smart to do.

Here is the solution for your problem:

@Configuration
Class StreamContollerConfig {

    @Bean
    public Properties streamControllerProperties() throws Exception {
        return Utilities.loadPropertiesFile("some/fixed/name");
    }
}

Try to avoid runtime "configurable" beans. They add a lot of complexity to your product with often little benefit.

Build your application from blocks with one final "configuration" block that wires and weaves everything together. That way, each block stays independent and simple.

这篇关于Spring注解:如何创建自动装配注释与参数静态/非静态方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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