gpt4 book ai didi

java - 创建 bean 时出错

转载 作者:行者123 更新时间:2023-12-02 00:04:36 25 4
gpt4 key购买 nike

这是我得到的错误跟踪:

 org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'DefaultPG' defined in file [C:\Users\ali\workspace\tms-payment\PaymentGatewayConfigurations.xml]: Error setting property values; nested exception is org.springframework.beans.NotWritablePropertyException: Invalid property 'paymentGatewayName' of bean class [me.tms.payment.common.DefaultPaymentGateway]: Bean property 'paymentGatewayName' is not writable or has an invalid setter method. Does the parameter type of the setter match the return type of the getter?
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1361)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1086)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:517)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:456)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:310)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:190)
at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1075)
at me.tms.payment.common.DefaultPaymentGateway.getPaymentGatewayName(DefaultPaymentGateway.java:12)
at me.tms.payment.test.DefaultPaymentGatewayTest.test(DefaultPaymentGatewayTest.java:16)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.junit.internal.runners.TestMethod.invoke(TestMethod.java:59)
at org.junit.internal.runners.MethodRoadie.runTestMethod(MethodRoadie.java:98)
at org.junit.internal.runners.MethodRoadie$2.run(MethodRoadie.java:79)
at org.junit.internal.runners.MethodRoadie.runBeforesThenTestThenAfters(MethodRoadie.java:87)
at org.junit.internal.runners.MethodRoadie.runTest(MethodRoadie.java:77)
at org.junit.internal.runners.MethodRoadie.run(MethodRoadie.java:42)
at org.junit.internal.runners.JUnit4ClassRunner.invokeTestMethod(JUnit4ClassRunner.java:88)
at org.junit.internal.runners.JUnit4ClassRunner.runMethods(JUnit4ClassRunner.java:51)
at org.junit.internal.runners.JUnit4ClassRunner$1.run(JUnit4ClassRunner.java:44)
at org.junit.internal.runners.ClassRoadie.runUnprotected(ClassRoadie.java:27)
at org.junit.internal.runners.ClassRoadie.runProtected(ClassRoadie.java:37)
at org.junit.internal.runners.JUnit4ClassRunner.run(JUnit4ClassRunner.java:42)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
Caused by: org.springframework.beans.NotWritablePropertyException: Invalid property 'paymentGatewayName' of bean class [me.tms.payment.common.DefaultPaymentGateway]: Bean property 'paymentGatewayName' is not writable or has an invalid setter method. Does the parameter type of the setter match the return type of the getter?
at org.springframework.beans.BeanWrapperImpl.setPropertyValue(BeanWrapperImpl.java:1024)
at org.springframework.beans.BeanWrapperImpl.setPropertyValue(BeanWrapperImpl.java:900)
at org.springframework.beans.AbstractPropertyAccessor.setPropertyValues(AbstractPropertyAccessor.java:76)
at org.springframework.beans.AbstractPropertyAccessor.setPropertyValues(AbstractPropertyAccessor.java:58)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1358)
... 30 more

这是报告的类(class):

package me.tms.payment.common;

import me.tms.payment.configurations.ConfigurationsUtility;
import me.tms.payment.configurations.PaymentGatewayConfigurations;

public class DefaultPaymentGateway {

private static String paymentGatewayName;

@SuppressWarnings("static-access")
public static String getPaymentGatewayName() {
DefaultPaymentGateway defaultPaymentGateway = (DefaultPaymentGateway)ConfigurationsUtility.getBeanConfigurationContext(PaymentGatewayConfigurations.configurationsFile).getBean("DefaultPG");
setPaymentGatewayName(defaultPaymentGateway.paymentGatewayName);
return paymentGatewayName;
}

public static void setPaymentGatewayName(String paymentGatewayName) {
DefaultPaymentGateway.paymentGatewayName = paymentGatewayName;
}

}

这是 bean :

<!-- Default payment gateway properties -->
<bean id="DefaultPG" class="me.tms.payment.common.DefaultPaymentGateway" scope="prototype">
<property name="paymentGatewayName" value="VPC"></property>
</bean>

问题是什么?

最佳答案

Bean 不能是静态类。它们需要由Spring内部实例化。而静态类显然不能被实例化。改用这个:

public class DefaultPaymentGateway {

private static String paymentGatewayName = "VPN";

public String getPaymentGatewayName() {
return paymentGatewayName;
}

public void setPaymentGatewayName(String paymentGatewayName) {
paymentGatewayName = paymentGatewayName;
}

}

您不应该在 getter/setter 方法中做太多事情。 Getter 应该只返回属性,而 setter 应该只设置它。从它们调用其他 getter/setter 会带来麻烦。

关于java - 创建 bean 时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14117471/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com