简体   繁体   中英

How to inject constructor parameters from properties in Spring 3.0 with the @Named annotation?

I'm having trouble putting it all together:

<?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>

Main is this:

@Named
@Singleton
public class MySpringMain {
    @Inject
    public MySpringMain(final AReallyCool component) {
        component.runForAWhile();
    }

    public static void main(final String... args) {
        new ClassPathXmlApplicationContext(args);
    }
}

Component is this:

@Named
public class AReallyCool {
    @Inject
    public AReallyCool(@Named("whoAmI") final String whoAmI) {
        // do something here
    }
}

And properties is:

whoAmI=Who is anyone, really?

Naturally (for me) Spring dies the death:

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)}

Questions:

  • Is this even a reasonable approach? I'm trying to avoid Spring-specific annotations.
  • How would you make this work?

A couple of Spring specific examples may help. As always the documentation at http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html is quite useful.

To read from a properties file look for the @Value annotation. Example:

@Component
@Scope("prototype")
@ImportResource("classpath:spring/app-config.xml")
public class RancidService {

    private String filepath;
    private String filename;

    /**
     * Default constructor
     *
     * @param pathname
     */
    @Autowired
    public RancidService(@Value("#{ nccProperties['rancid.path']}") String filepath) {

        this.filepath = filepath;
    }

Here is an example of a main function @Autowired in

@Component
public class GetCurrentMetric {


    /**
     * @param args
     */
    public static void main(String[] args) {

        ApplicationContext context = new ClassPathXmlApplicationContext("classpath:spring/app-config.xml");

        GetCurrentMetric p = context.getBean(GetCurrentMetric.class);
        p.start(args);

    }

    @Autowired
    private WhipService service;
    private void start(String[] args) {

        if (args.length != 2) {
            System.out.println("Usage: GetCurrentMetric <device> <interface>    Example: GetCurrentMetric cr1.lax1 p9/2");
        } else {
            String device = args[0];
            String iface = args[1];

            Map<String, String> map = service.getCurrentMetric(device, iface);

            if (map.size() == 2) {
                System.out.println("Level: " + map.get("level"));
                System.out.println("Metric: " + map.get("metric"));
            }
        }
    }

}

EDIT: Missed one important thing, for the properties file example at the top you'll need something in your application context file to tie it together. Example for above:

<!-- define the properties file to use --> 
<util:properties id="nccProperties" location="classpath:spring/ncc.properties" />

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM