org.springframework.beans.TypeMismatchException when using @Value annotation

1. Problem Description.

After I added @Value annotation to inject properties value in a POJO, when I started the application server and access a page, encountered this error:

Could not autowire field: private int com.company.system.module.service.AccountControlConfiguration.bookieDefault; nested exception is org.springframework.beans.TypeMismatchException: Failed to convert value of type 'java.lang.String' to required type 'int'; nested exception is java.lang.NumberFormatException: For input string: "${frequency.bookie.default}"

Properties file:

frequency.bookie.default = 1000
frequency.1.default = 1000
frequency.2.default = 500
frequency.2.placeorder = 100
frequency.2.fetchodd = 100
frequency.2.bethistory = 100
frequency.2.betlist = 100
frequency.2.balance = 100
frequency.2.betstatus = 100
frequency.5.default = 1000
frequency.27.default = 1000
frequency.18.default = 1000
applicationContext-service.xml

POJO:

@Component
public class AccountControlConfiguration {

	@Value("${frequency.bookie.default}")
	private int bookieDefault;

		
	@PostConstruct
	public void test(){
		System.out.println("Injected Successfully: " + bookieDefault);
	}

2. Analysis

At first, I checked the variable spelling and the properties file path, and other details, no joy in the end.

Second round, I added a post construct method as shown previously, with this method, I saw that the injection works fine during Jetty start. Then, I guess spring framework tries to instantiate this bean twice.

So, I checked all application context files, and I found that 2 application context files include this line which would impact our AccountControlConfiguration:

In addition, I added the in just one application context file, therefore, I got the NumberFormatException.

3. Solution

Comment out redundant  declaration.

4. Lesson

Be careful when add , don't repeat them in different files, which would cause unpleasant unexpected errors.





你可能感兴趣的:(J2EE)