C ++ The difference between the position order of the position of the global variable and the local variable storage

2023-01-23   ES  

@Autowired

Spring2.1, users are allowed to mark the attribute variables of the Bean through @Autowired annotation. The attribute setter method and the constructor can be marked, and the automatic configuration of the bean is completed with AutowIRDANNOTIONPROCESOR. Use the @Autowired comment to inject bytype.

Add to ApplicationContext.xml:

[xml] 
view plain
 copy
  1. <! -ThebeanpostProcessor will automatically mark the bean of @Autowired to inject->  
  2. <bean class=“org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor”/>  

Use @Autowired to eliminate SET and get methods.

[xml] 
view plain
 copy
  1. @Autowired  
  2. private UserDao userdao;  

This can delete Set, get method and related preparation in Spring.

[java] 
view plain
 copy
  1. <bean id=“userDao” class=“…”/>  
  2. <bean id=“userService” class=“…”>  
  3.     <property name=“userDao”>  
  4.       <ref bean=“userDao”/>  
  5.     </property>  
  6. </bean>  

The attributes in the parent class are injected into the attributes of the parent class through the setter method of @Autowired.

[java] 
view plain
 copy
  1. @Autowired  
  2. public void setDataSource(DataSource dataSource)  
  3. {  
  4.     super.setDataSource(dataSource);  
  5. }  

@Autowired(required = false)

When you cannot be sure that you must have a certain class in the Spring container, you can use the @Autowired (required = false) where you need to automatically inject this class. No error.

Of course, under normal circumstances, the place where @Autowired needs to be injected with bean, and the situation that is automatically injected and allowed to not be injected will generally only encounter in the development or test period (for example, to quickly start the Spring container, Only the Spring configuration file of some modules is introduced), so @Autowired (required = false) will be rarely used.

@Qualifier

Use @AUTOWIRED Notes to inject Bytype. If Byname (byname is identified by ID), add @qualifier notes. Generally, @qualifier comments should be added when the number of candidates is not 1.

When the @Autowired comment is used for automatic injection by default, the number of candidates matching in the Spring container must be available and only one. When you can’t find a matching bean, the Spring container will throw it out

Beancausenexception is abnormal and pointed out that it must have at least one matching Bean.

The opposite error that cannot find a type matching Bean is: If the Spring container has multiple candidates Bean, the Spring container will also throw the BeanCreationException exception when starting.

Spring allows us to specify the name injected by bean through the @qualifier notes, so that the ambiguity is eliminated, and the exception can be solved by the following method:

[java] 
view plain
 copy
  1. @Autowired  
  2. public void setOffice(@Qualifier(“office”)Office office)  
  3. {  
  4.     this.office =office;  
  5. }  

can also be injected directly to the attribute:

[java] 
view plain
 copy
  1. @Autowired   
  2. @Qualifier(“office”)   
  3. private Office office;  

@qualifier (“Office”) Office is the name of Bean, so when @Autowired and @qualifier are used in combination, the automatic injection strategy has transformed from Bytype to Byname.

@Autowired can comment on member variables, methods, and constructors, while @qualifier’s labeling objects are member variables, methods into parameters, and constructor enters. It is because of the different comments objects that Spring does not unify @Autowired and @qualifier into a comment class.

@qualifier can only be used with @Autowired, which is a beneficial supplement to @Autowired.

Generally speaking,@Qualifier’s comments on the signature of the method will reduce the readability of the code, while the notes of member variables are relatively better.

@Resource

Spring not only supports the @Autowired annotation of its own definition, but also supports several annotations defined by the JSR-250 specifications. They [email protected],@PostConStruct, and @predestroy.

@Resource is equivalent to @Autowired, but @Autowired is automatically injected by Bytype, and @Resource is automatically injected by Byname by default. @Resource has two attributes that are more important. The division is name and type. Spring analyzes the name attribute of @Resource annotation to the name of Bean, and the Type attribute is parsed into the type of Bean. So if you use the name property, use Byname’s automatic injection strategy, and use the Bytype to automatically inject the strategy when using the Type attribute. If you are neither specified Name nor the Type attribute, then use Byname to automatically inject the strategy through the reflection mechanism.

@Resource assembly sequence

1. If Name and Type are specified at the same time, find the only matching bean from the Spring context for assembly.

2. If the name is specified, the bean that is matched with the name (ID) matching of the name (ID) is specified for assembly.

3. If Type is specified, find the only bean that match the type matching in the context for assembly. If you can’t find or find multiple, you will throw an exception

4. If there is neither specified name nor Type, the assembly is automatically assembled according to Byname; if there is no matching, the retreat will be matched into a original type. If the matching

@Component、@Repository、@Service、@Controller

Spring 2.5 In addition to @Component notes, there are also several comments with special semantics, which are: @repository, @service, and @Controller.

In the current Spring version, these three annotations and @Component are equivalent, but from the naming of the annotation class, it is easy to see that these three comments and lasting layers, business layers, and control layers (web layers (web layers (web layers )Corresponding.

Although these three notes are not new than @Component, Spring will add special features to them in the future versions.

So, if the web application uses a classic three -layered structure, it is best to use the above -mentioned annotations to comment on the above -mentioned comments in the long -lasting layer, business layer, and control layer.

@Service is used to label business layer components

@Controller is used to mark the control layer component (such as Action in Struts)

@Repository is used to label data access components, that is, DAO component

@component refers to components. When the components are not classified, we can use this annotation to mark.

[java] 
view plain
 copy
  1. @Service  
  2. public class VentorServiceImpl implements iVentorService {     
  3. }  
  4. @Repository  
  5. public class VentorDaoImpl implements iVentorDao {   
  6. }  

In a slightly larger project, if the component is defined by XML bean to configure, it will obviously increase the volume of the configuration file, and it is not convenient to find and maintain.

Spring2.5 introduced the automatic scanning mechanism of the component for us. He searched for the class that marked the above annotations under the class and incorporated these classes into the Spring container management.

It is the same as when using the Bean node configuration component in XML files. To use the automatic scanning mechanism, we need to open the following configuration information:

[xml] 
view plain
 copy
  1. <?xml version=“1.0” encoding=“UTF-8” ?>   
  2. <beans xmlns=“http://www.springframework.org/schema/beans”  
  3.     xmlns:xsi=“http://www.w3.org/2001/XMLSchema-instance”  
  4.     xmlns:context=“http://www.springframework.org/schema/context”  
  5.     xsi:schemaLocation=”http://www.springframework.org/schema/beans  
  6. http://www.springframework.org/schema/beans/spring-beans-2.5.xsd  
  7. http://www.springframework.org/schema/context  
  8.         http://www.springframework.org/schema/context/spring-context-2.5.xsd”>   
  9.     <context:annotation-config />  
  10.     <context:component-scan base-package=”com.iteedu.spring”>     
  11. </beans>  

Annotation-config is a [email protected],@AutoWired,@PostConStruct,@predestroy,@resourceRef,@ejb,@PersistenceContext,@PersistenceNit.

Base-Package is a bag (including all subcuns) that needs to be scanned, and is responsible for scanning those classes.

can specify the initialization method and destroy method in the following method:

[java] 
view plain
 copy
  1. @PostConstruct  
  2. public void init() {   
  3. }   
  4. @PreDestroy  
  5. public void destory() {   
  6. }  

source

Related Posts

The smallest generation tree || The shortest path is adjacent to the matrix to implement the PRIM algorithm C ++ code

Design and realize a Servlet that receives the data of the client table

Spring Cloud appears java.net.unknownhostexception processing

Learning rate (adjustment step) ITER =

C ++ The difference between the position order of the position of the global variable and the local variable storage

Random Posts

GLIDE as a picture cache, clear the reasonable method of cache

Android compilation system Makefile (Android.mk) writing

Transform, Translate, Translaton, Animation Animation

shell script -memory monitoring automatic email alarm

1140 Look-And-Say Sequence-PAT Grade A