带String参数的@Bean方法 - 异常

时间:2013-03-04 08:40:51

标签: java spring

这是一个带有@Bean注释的注释驱动类。当我只有placeholder()没有任何String参数 - 它工作,但我需要占位符(字符串属性),以动态更改位置。

@Configuration
public class TestPlacehodler {


    @Value("${string}")
    public static String value;

    @Bean
    public static PropertySourcesPlaceholderConfigurer placeholder(String property) {
        PropertySourcesPlaceholderConfigurer placeholder = new PropertySourcesPlaceholderConfigurer();
        placeholder.setIgnoreUnresolvablePlaceholders(true);
        placeholder.setLocation(new ClassPathResource(property));

        return placeholder;
    }

    public static void main(String[] args) {
        AbstractApplicationContext ctx = new AnnotationConfigApplicationContext(TestPlacehodler.class);
        PropertySourcesPlaceholderConfigurer placeholder = placeholder("test.properties");
        placeholder.postProcessBeanFactory(ctx.getBeanFactory());
        System.out.println(value);

    }

我有这样的例外:

Exception in thread "main" org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'placeholder' defined in class TestPlacehodler: Unsatisfied dependency expressed through constructor argument with index 0 of type [java.lang.String]: : No qualifying bean of type [java.lang.String] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {}; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [java.lang.String] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {}
    at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:730)
    at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:461)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateUsingFactoryMethod(AbstractAutowireCapableBeanFactory.java:1029)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:925)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:490)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:461)
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:295)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:223)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:292)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:198)
    at org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(AbstractApplicationContext.java:657)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:461)
    at org.springframework.context.annotation.AnnotationConfigApplicationContext.<init>(AnnotationConfigApplicationContext.java:73)
    at TestPlacehodler.main(TestPlacehodler.java:26)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:120)

    }

1 个答案:

答案 0 :(得分:0)

你的辩护     public static PropertySourcesPlaceholderConfigurer placeholder(String property) 说春天要为方法提供一些String类型的bean。 您需要删除String property参数并从其他位置获取其值。

此外,调用PropertySourcesPlaceholderConfigurer placeholder = placeholder("test.properties");是不正确的。方法将由Spring容器以自动方式执行(但目前不可能,因为Spring不知道get参数property)。