嗨,我正在开发一个spring boot
依赖于另一个项目的应用程序B
。的依赖 jarB
具有所有 bean 注入的配置文件。现在,当我B
在我的项目中创建项目中的类 bean 时,必须添加依赖 bean 的定义,因此B
在我的配置文件中重复项目中的配置。为了避免这种情况,我只是将项目B
配置文件添加到我的类路径中。
ProjectB
有一个类 say ClassEX
,它的contructor
参数为argA
和argB
。当我尝试使用占位符配置将值传递给这些时,它无法读取它并将异常抛出为 .
Cannot cast null to int type
所以基本上这个值是空的。
我尝试通过将其放入我的项目中来创建同一类的 bean,它工作正常并读取占位符值。
对于下面的示例projectAbean
正在创建但projectBbean
失败。
谁能说出可能是什么原因。以下是我的配置:项目Aconfig.xml
<context:property-placeholder location="classpath:/ce.env.properties" />
<import resource="classpath:/projectB-config.xml" />
<bean id="projectAbean" class="com.ex.projecta.ClassEX">
<constructor-arg type="String" value="${argA}" />
<constructor-arg type="String" value="${argB}" />
</bean>
properties file:
argA=20
argB=10
projectB-config.xml
<bean id="projectBbean" class="com.ex.projectb.ClassEX">
<constructor-arg type="String" value="${argA}" />
<constructor-arg type="String" value="${argB}" />
</bean>
exception:
Unsatisfied dependency expressed through constructor parameter 0:
Could not convert argument value of type [null] to required type [int]:
Failed to convert value of type [null] to required type [int]; nested
exception is java.lang.IllegalArgumentException: Cannot convert value of
type [null] to required type [int]: PropertyEditor
[org.springframework.beans.propertyeditors.CustomNumberEditor] returned
inappropriate value of type [null] at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:359) ~[spring-beans-4.3.2.RELEASE.jar:4.3.2.RELEASE]
at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveValueIfNecessary(BeanDefinitionValueResolver.java:108) ~[spring-beans-4.3.2.RELEASE.jar:4.3.2.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1481) ~[spring-beans-4.3.2.RELEASE.jar:4.3.2.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1226) ~[spring-beans-4.3.2.RELEASE.jar:4.3.2.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:543) ~[spring-beans-4.3.2.RELEASE.jar:4.3.2.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:482) ~[spring-beans-4.3.2.RELEASE.jar:4.3.2.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306) ~[spring-beans-4.3.2.RELEASE.jar:4.3.2.RELEASE]
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230) ~[spring-beans-4.3.2.RELEASE.jar:4.3.2.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302) ~[spring-beans-4.3.2.RELEASE.jar:4.3.2.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197) ~[spring-beans-4.3.2.RELEASE.jar:4.3.2.RELEASE]
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:776) ~[spring-beans-4.3.2.RELEASE.jar:4.3.2.RELEASE]