Using
Spring @Autowired annotation, we can inject all java beans of specific type to
a collection like List.
For
example, In the below snippet, resources collection contains all the Resource
beans created by spring.
@Component public class ResourceManager { @Autowired private List<Resource> resources; ...... ...... }
Find the
below working application.
package com.sample.app.model; public class Resource { public int id; public String name; public Resource(int id, String name) { this.id = id; this.name = name; } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public void closeMe() { System.out.println("Closing the resource : [" + id + " , " + name + "]"); } }
BeanConfiguration.java
package com.sample.app.config; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import com.sample.app.model.Resource; @Configuration public class BeanConfiguration { @Bean public Resource fileResource() { return new Resource(1, "File Resource"); } @Bean public Resource dbResource() { return new Resource(2, "Database Resource"); } @Bean public Resource printerResource() { return new Resource(3, "Printer Resource"); } }
ResourceManager.java
package com.sample.app.cleanup; import java.util.List; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; import com.sample.app.model.Resource; @Component public class ResourceManager { @Autowired private List<Resource> resources; public void closeResources() { for(Resource resource: resources) { resource.closeMe(); } } }
App.java
package com.sample.app; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.CommandLineRunner; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.context.ConfigurableApplicationContext; import com.sample.app.cleanup.ResourceManager; @SpringBootApplication public class App implements CommandLineRunner { @Autowired private ResourceManager resourceManager; public static void main(String args[]) { ConfigurableApplicationContext context = SpringApplication.run(App.class, args); //System.out.println("Closing all the beans managed by spring boot"); context.close(); } @Override public void run(String... args) throws Exception { resourceManager.closeResources(); } }
pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>springbootCore</groupId> <artifactId>springbootCore</artifactId> <version>1</version> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.1.6.RELEASE</version> </parent> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> </dependencies> </project>
Total
project structure looks like below.
Run
App.java, you can see below messages in console.
Closing the resource : [1 , File Resource] Closing the resource : [2 , Database Resource] Closing the resource : [3 , Printer Resource]
No comments:
Post a Comment