简体   繁体   中英

Basic Spring help

I'm trying out my first Spring project and must be doing something really stupid because I can't figure out how to get the following simple snippet of code to work:

Here is my definition file:

 <?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

    <bean id="AWSProperties" class="com.addy.server.queue.AWSProperties" scope="singleton">
        <property name="awsAccessKey" value="test1"/>
        <property name="awsSecretKey" value="test2"/>
        <property name="awsSQSQueueName" value="testqueue"/>
    </bean>

    <bean id="QueueService" class="com.addy.server.queue.QueueService">
     <constructor-arg ref="AWSProperties"/>
    </bean>

</beans>

And my two simple beans:

public class AWSProperties {

    private String awsAccessKey;
    private String awsSecretKey;
    private String awsSQSQueueName;


    public void setAwsAccessKey(String awsAccessKey) {
        awsAccessKey = awsAccessKey;
    }

    public String getAwsAccessKey() {
        return awsAccessKey;
    }

    public void setAwsSecretKey(String awsSecretKey) {
        awsSecretKey = awsSecretKey;
    }

    public String getAwsSecretKey() {
        return awsSecretKey;
    }

    public void setAwsSQSQueueName(String awsSQSQueueName) {
        awsSQSQueueName = awsSQSQueueName;
    }

    public String getAwsSQSQueueName() {
        return awsSQSQueueName;
    }

}

public class QueueService {

    private AWSProperties properties;



    public QueueService(AWSProperties properties)
    {
        this.properties = properties;
    }


    public void receiveMessage()
    {
        System.out.println(properties.getAwsAccessKey());
    }

}

When I run the following snippet I get "null" when I am expecting "test1"

   public class VMMConsumer {





    public static void main(String[] args) 
    {


        ApplicationContext context = new ClassPathXmlApplicationContext(new String[] {"VMMConsumer.xml"});


        QueueService service = (QueueService)context.getBean("QueueService");

        service.receiveMessage();   

    }
}

This is a case where using final on parameters would have helped.

You can set Eclipse to add Final to parameters as a Save Action.

Mind you - you won't make the same mistake twice!

Nevermind it was something really dumb. My setters weren't correct -- thats what I get for using the eclipse auto generate.

Fix:

public class AWSProperties {

private String awsAccessKey;
private String awsSecretKey;
private String awsSQSQueueName;


public void setAwsAccessKey(String awsAccessKey) {
    this.awsAccessKey = awsAccessKey;
}

public String getAwsAccessKey() {
    return awsAccessKey;
}

public void setAwsSecretKey(String awsSecretKey) {
   this.awsSecretKey = awsSecretKey;
}

public String getAwsSecretKey() {
    return awsSecretKey;
}

public void setAwsSQSQueueName(String awsSQSQueueName) {
    this.awsSQSQueueName = awsSQSQueueName;
}

public String getAwsSQSQueueName() {
    return awsSQSQueueName;
}

}

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