Основная весенняя помощь - PullRequest
       9

Основная весенняя помощь

0 голосов
/ 07 февраля 2009

Я пробую свой первый проект Spring и, должно быть, делаю что-то действительно глупое, потому что не могу понять, как заставить работать следующий простой фрагмент кода:

Вот мой файл определения:

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

И мои две простые бобы:

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

}

Когда я запускаю следующий фрагмент, я получаю «ноль», когда ожидаю «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();   

    }
}

Ответы [ 2 ]

5 голосов
/ 07 февраля 2009

Это тот случай, когда использование final on параметров помогло бы.

Вы можете настроить Eclipse для добавления Final к параметрам в качестве действия сохранения.

Имейте в виду - вы не сделаете одну и ту же ошибку дважды!

2 голосов
/ 07 февраля 2009

Неважно, это было что-то действительно глупое. Мои сеттеры были не правы - это то, что я получаю за использование автоматического генерирования eclipse.

Исправлено:

публичный класс 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;
}

}

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...