First, Spring Aware
The biggest highlight of spring dependency injection is that all of your beans are unconscious of the presence of the spring container. That is, you can replace your container with other containers, such as Google Guice, when the coupling between the beans is very low.
But in a real project, you inevitably use the functionality of the spring container itself, and your Bean must be aware of the spring container to invoke the resources provided by spring, the so-called spring Aware. Spring Aware, in fact, is originally spring designed for use within the framework, and if you use Spring Aware, your Bean will be coupled to the spring framework.
Spring provides the Aware interface:
- Beannameaware gets the name of the bean in the container
- Beanfactoryaware gets the current bean factory so that it can invoke the container's service
- Applicationcontextaware Gets the current application context so that the container service can be called
- Messagesourceaware gets the message source so that it can get text information
- Applicationeventpublisheraware Application Event Publisher, you can publish events, the aforementioned Demopulisher can also implement this interface to publish events
- Resourceloaderaware get the resource loader, you can get the external resource file
The purpose of spring aware is to let the Bean get the service of the Spring container. Because the ApplicationContext interface integrates the Messagesource interface, the Applicationeventpublisher interface, and the Resourceloader interface, the Bean inherits Applicationcontextaware can get all the services of the spring container, but in principle the O is still using what interface to implement what interface.
Example:
1. Prepare. Create a new test.txt under the aware file, free content, load the following external resources using
2.Spring Aware Demo Bean
PackageCom.ecworking.aware;Importorg.apache.commons.io.IOUtils;ImportOrg.springframework.beans.factory.BeanNameAware;ImportOrg.springframework.context.ResourceLoaderAware;ImportOrg.springframework.core.io.Resource;ImportOrg.springframework.core.io.ResourceLoader;ImportOrg.springframework.stereotype.Service;Importjava.io.IOException; @Service Public classAwareserviceImplementsbeannameaware,resourceloaderaware{//implement Beannameaware and Resourceloaderaware interfaces to get the bean name and resource Load service PrivateString Beanname; PrivateResourceloader Loader; @Override Public voidSetbeanname (String beanname) {//implementing Beannameaware requires overriding the Setbeanname method This. Beanname =Beanname; } @Override Public voidSetresourceloader (Resourceloader loader) {//implementing Resourceloaderaware requires overriding the Setresourceloader method This. Loader =loader; } Public voidOutputresult () {System.out.println (The name of the Bean is: "+beanname); Resource Resource= Loader.getresource ("Classpath:com/ecworking/aware/test.txt"); Try{System.out.println ("The contents of the file loaded by Resourceloader are:" +ioutils.tostring (Resource.getinputstream ())); } Catch(IOException e) {e.printstacktrace (); } }}
3. Configuration Classes
Package Com.ecworking.aware; Import Org.springframework.context.annotation.ComponentScan; Import org.springframework.context.annotation.Configuration, @Configuration @componentscan (" Com.ecworking.aware ")publicclass awareconfig {}
4. Running
package Com.ecworking.aware; import Org.springframework.context.annotation.AnnotationConfigApplicationContext; public class Main { public static void main (string[] args) {annotationconfigapplicationcontext context = new annotationconfigapplicationcontext (awareconfig. = Context.getbean (Awareservice. Class
Operation Result:
Spring Boot Combat Note (v)--Spring Advanced topic (Spring Aware)