Reference: The spring basics of Java review
Annotation locating bean Note points
XML file beans tag content, Xmlns:context, xsi:schemalocation properties. These two properties are missing the BENMS configuration file will error
<beans xmlns= "Http://www.springframework.org/schema/beans"
Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"
xmlns:context= "Http://www.springframework.org/schema/context"
Xsi:schemalocation= "Http://www.springframework.org/schema/beans
Http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
Http://www.springframework.org/schema/context
Http://www.springframework.org/schema/context/spring-context-3.0.xsd
Http://www.springframework.org/schema/tx
Http://www.springframework.org/schema/tx/spring-tx-3.0.xsd ">
<context:annotation-config/>
<!--specifies the class package that needs to be scanned--
<context:component-scan base-package= "Com.bueryang.dao"/>
Web. xml file
Contextloader Listener, BENMS configuration file
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:applicationContext1.xml</param-value>
</context-param>
Annotation mainly includes: @Resource/@Autowried/@Qualifier.
@Resource (name= "Userdaoimpl")
Private Userdao UserDao1;
The annotation on the field is populated userDao1 using a bean named "Userdaoimpl" defined in the XML configuration file.
@Autowired (Required=false) @Qualifier ("Userdaoimpl") private Userdao UserDao3;
The second type of annotation,autowired and qualifier are used together, qualifier to set the bean name, autowired to set the behavior when the bean is not found, and required to true throws an exception. Null is returned when required is false.
On the setter of the annotation, @Resource can not specify the name parameter, then spring will first follow the name and then the data type of the way to locate the bean.
Using annotation to locate beans