我的项目结构如下 [1]: http : //i.stack.imgur.com/T1jvh.png
我有一个在create-user中定义的类和接口
UserGeneratorInterface
package com.credit.userGenerator; public interface UserGeneratorInterface { public String userIdGenerator(); }RandomUserGenerator.java
public class RandomUserGenerator implements UserGeneratorInterface { public static enum Mode { ALPHA, ALPHANUMERIC, NUMERIC } public static String generateRandomString(int length, Mode mode) throws Exception { code logic } public String userIdGenerator(){ code logic } }我已经在da-web中定义了应用程序上下文,并且想要获得RandomUserGenerator.java的服务
public class ApplicationSignManager implements IApplicationSignInterface { public UserGeneratorInterface iUserGeneratorInterface; public UserGeneratorInterface getiUserGeneratorInterface() { return iUserGeneratorInterface; } public void setiUserGeneratorInterface( UserGeneratorInterface iUserGeneratorInterface) { this.iUserGeneratorInterface = iUserGeneratorInterface; } }AppContext.xml
<bean id="userGenerator" class="com.credit.userGenerator.RandomUserGenerator" ></bean> <bean id="appSign" class="com.*****.service.ApplicationSignManager"> <description>List of Dao in ApplicationLogin Manager</description> <property name="userGeneratorInterface" ref="userGenerator"/></bean>但我得到以下例外
Bean属性“iUserGeneratorInterface”不可写或具有无效的setter方法。 setter的参数类型是否与getter的返回类型匹配?
I have a project structure as follows [1]: http://i.stack.imgur.com/T1jvh.png
I have a class and interface defined in create-user
UserGeneratorInterface
package com.credit.userGenerator; public interface UserGeneratorInterface { public String userIdGenerator(); }RandomUserGenerator.java
public class RandomUserGenerator implements UserGeneratorInterface { public static enum Mode { ALPHA, ALPHANUMERIC, NUMERIC } public static String generateRandomString(int length, Mode mode) throws Exception { code logic } public String userIdGenerator(){ code logic } }I have defined application context in da-web and wan to get service of RandomUserGenerator.java
public class ApplicationSignManager implements IApplicationSignInterface { public UserGeneratorInterface iUserGeneratorInterface; public UserGeneratorInterface getiUserGeneratorInterface() { return iUserGeneratorInterface; } public void setiUserGeneratorInterface( UserGeneratorInterface iUserGeneratorInterface) { this.iUserGeneratorInterface = iUserGeneratorInterface; } }AppContext.xml
<bean id="userGenerator" class="com.credit.userGenerator.RandomUserGenerator" ></bean> <bean id="appSign" class="com.*****.service.ApplicationSignManager"> <description>List of Dao in ApplicationLogin Manager</description> <property name="userGeneratorInterface" ref="userGenerator"/></bean>But I am getting the following exception
Bean property 'iUserGeneratorInterface' is not writable or has an invalid setter method. Does the parameter type of the setter match the return type of the getter?
最满意答案
正如注释中所注意到的那样,使用带有大写“I”的setIUserGeneratorInterface()和getIUserGeneratorInterface()。 使用'property name =“iUserGeneratorInterface”ref =“userGenerator”' - 注意前导'i'。请注意,在命名类和字段时始终建议使用完善的实践。 在您的情况下,将'UserGeneratorInterface'命名为'UserGenerator'并命名字段'iUserGeneratorInterface',只需'userGenerator'就会更有意义。 另请注意,您已将此字段声明为public(尽管这与错误无关)。
As noticed in the comments, use setIUserGeneratorInterface() and getIUserGeneratorInterface() with capital 'I'. Use 'property name="iUserGeneratorInterface" ref="userGenerator"' - note the leading 'i'.Note that it is always advisable to use well-established practices when naming your classes and fields. In your case naming 'UserGeneratorInterface' simply 'UserGenerator' and naming the field 'iUserGeneratorInterface' simply 'userGenerator' would make more sense. Note also that you have declared this field public (although this has nothing to do with the errors).
更多推荐
发布评论