question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Injection of List<Resource> is inconsistent with Resource[]

See original GitHub issue

Normally it’s safe to change type from array to list, but it’s not for Resource.

@Value("${resources:file:/tmp/*.text}")
Resource[] resourceArray;  // multiple FileSystemResource

@Value("${resources:file:/tmp/*.text}")
List<Resource> resourceList; // only one FileUrlResource

here is the full test

import static org.junit.Assert.assertEquals;

import java.io.File;
import java.io.IOException;
import java.util.List;

import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.Resource;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.TestPropertySource;
import org.springframework.test.context.junit4.SpringRunner;

import test.ResourceInjectionTest.TestConfiguration;

@RunWith(SpringRunner.class)
@TestPropertySource(properties = { "resources=file:/tmp/*.text" })
@ContextConfiguration(classes = TestConfiguration.class)
public class ResourceInjectionTest {

	private static File[] files;

	@Autowired
	private TestConfiguration testConfiguration;

	@BeforeClass
	public static void setup() throws IOException {
		files = new File[2];
		files[0] = new File("/tmp", "a.text");
		files[1] = new File("/tmp", "b.text");
		for (File f : files)
			f.createNewFile();
	}

	@AfterClass
	public static void cleanup() {
		for (File f : files)
			f.delete();
	}

	@Test
	public void testInjection() {
		assertEquals(2, testConfiguration.resourceArray.length); // two FileSystemResource
		assertEquals(2, testConfiguration.resourceList.size()); // one FileUrlResource
	}

	static class TestConfiguration {
		
		@Value("${resources}")
		Resource[] resourceArray;

		@Value("${resources}")
		List<Resource> resourceList;
		
	}
}

Issue Analytics

  • State:open
  • Created 3 years ago
  • Comments:6 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
sbrannencommented, Apr 2, 2020

Indeed, the difference is that a ResourceArrayPropertyEditor is used to handle the array case; whereas, a CustomCollectionEditor is used to handle the list case.

The former internally uses a ResourcePatternResolver to expand the pattern into the 2 files in the filesystem; whereas, the latter simply treats the pattern as a simple string without expansion.

0reactions
sbrannencommented, May 11, 2020

Team Decision: Assigned to 5.x backlog as a potential enhancement.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Type of related field hr.employee.employee_id is inconsistent ...
hi all , iam trying to add a extra field in hr_emp module , but when i add the server is not starting...
Read more >
license-manager CLI docs inconsistent : r/aws - Reddit
But in my AWS CLI, I see only these commands: create-license-configuration. delete-license-configuration. get-license-configuration. get ...
Read more >
Extending Angular's $resource Service for a Consistent API
With this method, we can now inject our api service as a dependency and register our resource models with an easy one-line call:...
Read more >
gcloud beta resource-config list-resource-types
Accelerate your digital transformation; Whether your business is early in its journey or well on its way to digital transformation, Google Cloud can...
Read more >
Maven Websphere JNDI Resource Binding - Forums - IBM
InjectionProc E CWNEN0044E: A resource reference binding could not be found for ... in others it is codecoverageUI, I am making them consistent...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found