2009-02-07 76 views
0

我想我的第一个春天项目,必须做一些非常愚蠢的,因为我无法弄清楚如何让下面的代码片段的简单工作:基本春天帮助

这里是我的定义文件:

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

} 

当我运行下面的代码片段,我得到“空”时,我期待“测试1”

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

    } 
} 
+0

如果你能解释你遇到的问题的确切性质,这可能会有所帮助。汇编?告诉我们错误。值不正确?告诉我们他们是什么......等等。 – AAA 2009-02-07 00:13:10

+0

正如我所指出的,我得到的是null值而不是test1值,但在我的问题中看到我的回答在 – Ish 2009-02-07 00:14:53

回答

5

这是最后使用的参数会有所帮助的情况下。

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

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

2

没关系,这真是愚蠢。我的setters是不正确的 - 这就是我得到的使用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; 
} 

}