基本的Spring帮助

时间:2009-02-07 00:07:05

标签: java spring

我正在尝试我的第一个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”时,我会得到“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();   

    }
}

2 个答案:

答案 0 :(得分:5)

这种情况下使用final on参数会有所帮助。

您可以将Eclipse设置为将“参数”添加为“保存操作”。

请注意 - 你不会两次犯同样的错误!

答案 1 :(得分:2)

没关系,这真的很蠢。我的二传手不正确 - 这就是我使用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;
}

}