- There are two ways to create a bean using annotation
- To create a bean in a configuration class (a configuration class is a class labeled @configuration), each method of creating a bean in a configuration class should be labeled @bean, and you can specify the bean name in @bean this annotation. If not specified, the name of the method is used by default;
Note that a bean created by one method in the configuration class will refer to the bean created by another method, and the method can be called directly;
- Label the Java class as @Component, @Repository, @Service, any of the @Controller; Bean name can be specified in these annotation constructors, if the bean name is not specified, Spring By default sets the Bean name to this class name (lowercase first letter):
- @Repository used to decorate the persistence layer;
- @Service used to decorate the service layer;
- @Controller used to decorate the control layer;
- If a class does not fit any of the above, it is decorated with @component;
regardless of the method above, we need to instruct Spring IoC container to scan these classes labeled annotation, with the following methods:
- If you are using Genericxmlapplicationcontext to create the context, you need to add the following in the XML configuration file:
<context:component-scan base-package= "Package name"/>&NBSP;
Note,<context:component-scan> is implicitly enabled <context:annotation-config>; so uses < Context:component-scan> There's no need to specify <context:annotation-config>; you can specify scan by excluding or adding some classes by using <context: Component-scan> <context:include-filter> and <context:exclude-filter> sub-nodes;
- You can also use Annotationconfigapplicationcontext to create a context whose scan () method allows you to specify the package to scan; The register () method allows you to specify the class to scan;
- For a bean defined by @component, @Repository, @Service, @Controller, if it wants to autowire other beans, it can be in the bean field, setter method, constructor or an arbitrary method to add @autowired; use @autowired annotation Note the points:
- By default, the property marked as @autowired is required, and if spring cannot find a matching bean, an exception is thrown; You can set the required attribute of @autowired to false;
- When @autowired is used on a method, or a constructor, spring autowired the method or all the parameters of the constructor function;
- @Autowired default is Autowire by type
- If @autowired is used on a field of an array field or a generic collection type, spring will find all the matching types of beans, and construct the array or collection, set to that field;
- If @autowired is set to a field on a map type with the key type string, spring will find all the matching types of beans and insert the bean's bean name as key into the map;
- For a autowire field, if more than one matching type of bean is found, spring throws an exception, and there are two ways to resolve it:
- Mark @primary Annotation on one of the beans, indicating that if more than one bean of the matching type is found, the bean will have the highest priority;
- Add @qualifier annotation to this field, specifying the name of the bean to be autowire;
- If a configuration class is to refer to a bean in another configuration class, you can add @import annotation to the configuration class to refer to the other configuration classes;
- Spring provides @scope annotation to define the Scope of the Bean where it can be used with @bean, @Component, @Repository, @Service, @Controller;
- In order to reference the values in a properties file, we need to perform the following three steps:
- Add @propertysource annotation to the configuration class and specify the location of the properties file;
- Create a bean of type propertysourcesplaceholderconfigurer in the configuration class;
- After completing the two steps above, we can use @value ("${key:default_value}") to refer to the property in the properties file;
- In spring, you can use a resource to read an external resource, and we just need to use @value to inject the address of the resource into a resource field as a string. Spring will use the pre-registered Resourceeditor to convert the string into a resource object and inject it into this field;
Note: We can use the propertiesloaderutils.loadproperties () Method converts a resource object loaded with the properties file into a Properties object;
- To support multi-lingual, we need to create a bean named Messagesource in the configuration file and the type is Reloadableresourcebundlemessagesource, but the method that created the bean must be marked as static , because Reloadableresourcebundlemessagesource is a bean Post Processor;
- If you want to perform some tasks before or after the initialization method of each bean in spring container is called, we have two ways:
- If the Bean is created in a configuration file, we can do so by setting the Initmethod and Destorymethod two properties of the @bean annotation;
- If the bean is not created in the configuration file, the initialization method and the destructor can be indicated in the Bean's class using @postconstruct and @predestory;
- In general, all beans in spring context are initialized immediately after spring container is started, and in order to avoid the creation of a bean that consumes time and resources, we can label the class where the bean is located @lazy annotation , or the bean-created method in the configuration class is labeled @lazy annotation;
- If the creation of a bean relies on the creation of one or more other beans, we need to use @dependson annotation;
- A bean post processor allows spring container to perform some tasks before or after all of the bean's initialization methods are called, such as the @required method is a post-processor built into spring Requiredannotationbeanpostprocessor used to determine that all the beans in the spring container are marked as @required, the values have been set;
If you want to create a custom bean post Processor, you must implement the Beanpostprocessor interface and implement two methods: Postprocessbeforeinitialization () and Postprocessafterinitialization (), and mark the class as @component annotation;
Note: Postprocessbeforeinitialization () and the Postprocessafterinitialization () method returns the incoming bean, or returns an instance of the same type as the incoming bean;
- If we want to define multiple profiles, we need to create a configuration class for each profile, and then label the configuration class on @profile annotation, where you can specify one or more profile name in the constructor of the profiles;
In order to load beans from the correct profile, we need to activate one or more profiles, the correct way is to Annotationconfigapplicationcontext scan a package, Call the AnnotationConfigApplicationContext.getEnvironment.setActiveProfiles () method, you can also set the default Yes profile, The procedure is to call the AnnotationConfigApplicationContext.getEnvironment.setDefaultProfiles () method;
Spring Note Three----spring configuration based on annotation