Java Spring注释:如何为带有参数的静态/非静态方法创建autowire注释
使用注释;如何将参数值传递给方法Java Spring注释:如何为带有参数的静态/非静态方法创建autowire注释,java,spring,annotations,Java,Spring,Annotations,使用注释;如何将参数值传递给方法 下面代码中的示例“当自动连接confProps实例时,如何通过注释传递loadProperties API方法的参数(2个字符串)值” 我可以在方法参数级别使用@javax.inject.Named;但是,在Spring中,在方法参数级别上是否可以使用任何等价物呢?我无法在参数级别使用@Component 我可以用这些@Value(#{XXX})或@Qualifier(“”)来解决我的问题吗?在方法参数级别支持这两种方法 如果我在这里犯了任何其他配置错误,也请纠
下面代码中的示例“当自动连接confProps实例时,如何通过注释传递loadProperties API方法的参数(2个字符串)值”
@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;
}
和任何技术一样,Spring也有它的嘲讽和限制。从您的示例中,您已经开始尝试使用Spring做所有事情(即使是最简单的事情)。看看你是如何到达那里的,这也许有道理,但你最终还是在一个角落里 或者换一种说法:仅仅因为你可以,并不意味着这样做是明智的 以下是您问题的解决方案:
@Configuration
Class StreamContollerConfig {
@Bean
public Properties streamControllerProperties() throws Exception {
return Utilities.loadPropertiesFile("some/fixed/name");
}
}
尽量避免运行时“可配置”bean。它们给您的产品增加了很多复杂性,但通常没有什么好处
使用最后一个“配置”块从块构建应用程序,该块将所有内容连接并编织在一起。这样,每个块都保持独立和简单。我不知道如何通过注释实现这一点。目前,我正在遵循XML的方式
public class Utilities {
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;
}
<beans>
<bean id="loadProperties" class="com.pactolus.Utilities"
factory-method="loadPropertiesFile">
<constructor-arg index="0" value="sc_beans.xml"/>
<constructor-arg index="1" value="CC"/>
</bean>
</beans>
公共类实用程序{
公共静态属性loadProperties(String propsFileName,String类型)引发异常{
返回Utilities.loadPropertiesFile(p_propsFileName);
}
}
@服务
@范围(值=BeanDefinition.Scope\u SINGLETON)
@限定符(“strmContMgr”)
公共类StreamingControllerManager{
@自动连线
@限定符(“加载属性”)
道具属性;
}
不能使用带弹簧的静态Autowire@SRT_KP@SRT_KP如果你说问题是由于静电引起的;你能举一些非静态方法的例子吗谢谢!!但我不想硬编码“some/fixed/name”;它应该从属性文件中读取;或beans.xml;你能帮忙吗!!我知道你真的很努力使你的项目失败。我只能建议不要这样做。在这条道路上,只有痛苦等待着你。你知道你可以拥有几十个@Configuration
bean,并且你可以轻松地在它们之间切换吗?最后,您将拥有一个主bean,它将引入您想要的所有其他配置。+1好的,我不太了解,但无论如何;非常感谢您的支持!!亚伦。