2
私はすべて一緒にそれを置くのトラブルを抱えている:Spring 3.0のプロパティから@Namedアノテーションを使用してコンストラクタパラメータを挿入する方法は?
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<context:property-placeholder location="classpath:some-useful.properties"/>
<context:component-scan base-package="scan.me.scotty"/>
</beans>
メインはこれです:
@Named
public class AReallyCool {
@Inject
public AReallyCool(@Named("whoAmI") final String whoAmI) {
// do something here
}
}
とプロパティは次のとおりです:
whoAmI=Who is anyone, really?
@Named
@Singleton
public class MySpringMain {
@Inject
public MySpringMain(final AReallyCool component) {
component.runForAWhile();
}
public static void main(final String... args) {
new ClassPathXmlApplicationContext(args);
}
}
コンポーネントがこれです
当然のことながら(私にとっては)春は死を死ぬ:
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No matching bean of type [java.lang.String] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@javax.inject.Named(value=whoAmI)}
質問:
- これも、合理的なアプローチですか?私はSpring固有の注釈を避けようとしています。
- この作品をどのように作成しますか?
答えが長すぎます。 'Named'の代わりに' Value'を使うべきだと指定することができます。とにかく+1。しかし、 '@ ImportResource'を取り除いてください。これは必要ではなく、主に '@Configuration'クラスで使用されるはずです。 – Bozho
@Valueでうまく動作します。 @Namedで解決策を探しています。 – binkley
@Bozho私は長さについて同意しない、私は常に有用な例を見つけた。 @ImportResourceに関するコメントをいただき、ありがとうございます。 – Bill