基本的Spring帮助

Ish*_*Ish 0 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>
Run Code Online (Sandbox Code Playgroud)

还有我的两个简单豆子:

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

}
Run Code Online (Sandbox Code Playgroud)

当我运行以下代码片段时,当我期待"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();   

    }
}
Run Code Online (Sandbox Code Playgroud)

For*_*ner 5

这是使用final on参数会有所帮助的情况.

您可以将Eclipse设置为将"参数"添加为"保存操作".

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