Using annotation to locate beans

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.