Spring-bean (annotation mode-management and Dependency injection)

Source: Internet
Author: User

Bean Management (Annotation method)

1. Add a dependency package for annotations: Spring-aop.jar

2, the introduction of the configuration of spring XML file (check the official source)

3. Open the scanning of annotations

<context:component-scan base-pachage= "file directory path required for annotation scanning"/>

4, in the injected class using annotation injection: @Component ("id")

To distinguish between three-tier architectures using different annotations (the effect is consistent, just to differentiate)

@Repository:d AO layer

@Service: Service Layer

@Controller: Controller Layer

5. Using annotations: Consistent with XML

Attribute injection (annotation mode)

Simple properties

@Value ("attribute value")-------if no setter method can be added directly to the property, if there is a setter method that needs to be added to the setter method

Bean Injection

@Autowired

@Qualifier ("name")

autowired automatically injected by type, if there are two types of the same type by name injection requires another annotation to work with

@Resource (name= "name")--------provides support for JSR-250, consistent with the previous two annotations

Initialization and destruction method annotations

@PostConstruct---------Initialization

@PreDestroy--------Destruction (singleton mode Scope=singleton valid)

Scope of Action annotations

@Scope-------Default Singleton

Spring-bean (annotation mode-management and Dependency injection)

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.