您好我正在开发一个spring boot
应用程序,它依赖于另一个项目B
。 B
的依赖jar包含所有bean注入的配置文件。现在,当我在项目中的项目B
中创建类的bean时,必须添加依赖bean的定义,因此在我的配置文件中重复项目B
中的配置。为了避免这种情况,我只是将项目B
配置文件添加到我的类路径中。
项目B
有一个类ClassEX
,其contructor
参数为argA
和argB
。当我试图使用我的占位符配置将值传递给它们时,它无法读取它并将异常抛出。
Cannot cast null to int type
所以基本上这个值是空的。
我尝试通过将它放入我的项目中来创建相同类的bean,它正常工作并读取占位符值。
以下示例projectAbean
已创建,但projectBbean
失败。
任何人都可以说出原因。我的配置是:
项目A config.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]