簡體   English   中英

春季基本幫助

[英]Basic Spring help

我正在嘗試我的第一個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>

還有我的兩個簡單的bean:

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”時,我得到“ null”

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

    }
}

在這種情況下,使用final on參數會有所幫助。

您可以設置Eclipse以將Final添加到參數中作為Save Action。

提醒您-您不會兩次犯同樣的錯誤!

沒關系,這真的很愚蠢。 我的設置員不正確-這就是我使用eclipse自動生成所得到的。

固定:

公共類AWSProperty {

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

}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM