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
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
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.
@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>
@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)
@Resource
MyClass myClass
@Resource(name="otherName")
MyClass myClass
other stuff:
<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)
没有评论:
发表评论