2013年8月26日星期一

spring injection

 

 

1 may encounter problems:

 

exception information NoSuchBeanDefinitionException: No matching bean of type [...] or NoSuchBeanDefinitionException: No unique bean of type [...] specific differences between the two temporarily to the bottom, but The reason is they can not find a particular type of bean, back talk anymore.

 

exception expected single matching bean but found 2, well understood, can not be found until now is to find more than one, spring does not know how to do.

 

... to be added

 

 

2 injection and pouring out

 

This is without textual argument, I think the area is divided into both easier to understand after injection problems. Strictly speaking, the relationship between injection and injection are described in various ways in the beanDefinition inside, similar to the consumers and producers. Their behavior often occurs in spring container initialization, unless specified for prototype.

 

troubleshoot the problem from two angles, the injection is not wrong, note that is not right.

 

 

2.1 spout common way

 

1. we must understand the most simple and direct way, that

 
  
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate" /> 
 
 

it this way also another factory in terms of ways and means, which is also divided into static and instance factory plant, do not go into detail

 

 

2. follow-up in the spring, after the introduction of annotations in the class applied @ Service @ Component @ Controller @ Repository annotation of these four (they are on the same role in the spout), and configure packet scanning:

 
  
<context:component-scan base-package="com.mypackage"/> 
 
 

configure both annotated classes to sweep the surface of the package within the path. Note that the default annotation of the bean id and name is the name of the class first letters to lowercase

 

 

2.2 common injection method

 

notify spring injection refers to which your bean has a bean instance or called dependency needs spring do "fill."

 

2.2. 1. Explicit injection : setter injection (also constructor injection, similar to this):

 
  
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"> 
<property name="dataSource">
<ref>dataSource</ref>
</property>
</bean>
 
 

setter injection needs to have a corresponding setter method, Please note camelCasing, or can not find setter throws an exception . constructor injection requires corresponding constructor.

 

 

2.2. 2. automated assembly

 

default-autowire = "byName / byType"

 

special attention to this thing, and @ autowire no half dime, generally appear in the spring file header, which tags. This setting will intelligently scan which each bean setter method, similar to the first and add the property. That there is this configuration is not necessary after the above pins. The difference is explicitly configured freely specify the bean, default-autowire only by name, type of match.

 

 

2.2. 3. annotation injection

 

first start annotation injection, mainly by AutowiredAnnotationBeanPostProcessor, CommonAnnotationBeanPostProcessor, PersistenceAnnotationBeanPostProcessor and requiredAnnotationBeanPostProcessor This four class is responsible for detecting and achieve injection annotations, so spring container needs to get these kind of bean, there are two ways:

 
      
  • explicit
  •  
 
  
<bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor" /> 
 
 

 

use annotations to which register corresponding PostProcessor, if the use of autowired, to use this on it. Other specific correspondence between your own research.

 
      
  • lazy style
  •  
 
  
<context:annotation-config />  
 
 

this configuration will implicitly registered to Spring container AutowiredAnnotationBeanPostProcessor, CommonAnnotationBeanPostProcessor, PersistenceAnnotationBeanPostProcessor and equiredAnnotationBeanPostProcessor this ; 4 BeanPostProcessor. And above component-scan This configuration also includes automatic entry actually injected into the processor function, so when using , you can < / span> removed. This is because the former is included by default with such a property: annotation-config = "true" , if we are to be explicitly specified as false, then they would have another Add configuration.

 

 
 

start annotation after injection, of course, to use comments:

 

@ autowired

 

@ autowired injection annotations, the default is injected by type, but you can use @ Qualifier ("abc") instead byname.

 
  
    Usage:       
  
    1.     written in the setter in as:   
  
       
    
     @Autowired  
   setAbc(@Qualifier("abc")Abc abc)
   
   

proven method written in front of the autowire can ignore the method name, such as

  
  
   
    
     @Autowired 
public void aaa(QuestionMarkDao questionMarkDao) {
super.setBaseDao(questionMarkDao);
}
   
   

way, and @ postConstruct annotation on relatively close, difference is that the former can take an argument, the container will automatically find the corresponding bean < / p>   

  
       
    (2) can also be written in the variable before, you can omit the setter, such as    
  
  
       
    
   @Autowired @Qualifier("abc") 
  MyClass myClass;
   
   

   

@ Resource

   

to inject annotations by name, can not find the time to try by type injection. byName refers priority byName, if you can not find the same name with the property when the bean and then follow type to find. Here there will be two exceptions mentioned above, the first one is not to find the same name, nor the same type (No unique bean of type); second is to have the same name but different types (...... ); third is not found the same name, but the same type have multiple (expected single matching bean but found 2)

  
  
    small details, in subclasses need to inject a property of a base class are not able to resource injection because if the use of sub-class, then there is no problem using a sub-class version of the attribute a, if the code is run to the base class code, calling to a property, this time using a base class a, injection does not make sense, easy nullpointer. So there is property in the inheritance relationship hidden, it is best to use explicit injection method, choose to only inject a base class, you can only inject subclass, or are injected.   
  
      
  
    use (you can omit the setter):   
  
       
    
@Resource 
MyClass myClass

@Resource(name="otherName")
MyClass myClass
   
   

  
  
   
  
  

other stuff:

  
      
  
    with the id of the bean:   
  
   
     two bean with the same id: In the same file will complain of. But if through    
   
     different spring file default-autowire:    
   
     Affect each other, I have not studied, someone knows please tell me    
   
     p tag:    
   
    
      p tag is property     
    
     
      
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate" 
p:dataSource-ref="dataSource" />
     
     

need to introduce xmlns and schemaLocation

     

value and ref difference:

     

former is injected into a string value, which is injected into the container of bean (id value for the ref bean)

    
   
   
        
  
    
 

 

没有评论:

发表评论