The definition of bean in spring and the annotation implementation of its scope

Source: Internet
Author: User

classpath Scanning and component management:

starting with Spring3.0, the Spring Javaconfig project offers a number of features, including the use of Java rather than the XML definition bean, which refers to annotations

@Configuration, @Bean, @Import, @DependsOn

@Component is a generic annotation that can be used with any bean

@Repository: Commonly used to annotate DAO classes, which are persistent layers

@Service: Typically used for annotation service classes, which are services tiers

@Controller: Typically used for controller classes, or control layer MVC

meta annotations (meta-annotations)

meta-Annotations are annotated annotations, and many spring-provided annotations can be used as their own code, the "metadata annotations", a simple annotation that can be applied to another annotation

The following defines service annotations, decorated with @component annotations, @Service features that have @component annotations:

@Target (Elementtype.type) @Retention (retentionpolicy.runtime) @Documented @component  //Spring would see this and Treat @Service in the same as @Componentpublic @interface Service {    //...}

We can also define annotations ourselves.

automatic detection of classes and the registration of beans

spring can automatically detect classes and register beans in ApplicationContext.

Example: @Service,@Component,@Repository to register to the class (annotations of the Class), There are also annotations that are registered on methods like @autowired, which can be automatically detected by the

Registered on the class, it can be automatically registered as a bean in the ApplicationContext .

To be able to detect these classes and register the appropriate beans, you need to configure the following in the XML file:

<context:component-scan base-package= "org.example"/>

automatically scan the class under the Org.example package

<context:component-scan> contains <context:annotation-config, usually after the former is used, the latter is no longer used. Because after using the former, The full functionality of the latter is already included. The former is usually used

use filters for custom scans

By default, classes are automatically discovered and registered with a bean by using @component, @Repository, @Service, @Controller annotations, or custom annotations that use @component annotations

the above behavior can be modified through a filter.

such as: ignoring all @repository annotations and substituting "Stub" for

<beans>    <context:component-scan base-package= "Org.example" >          <context:include-filter type= "Regex" expression= ". *stub.*repository"/>        <context:exclude-filter type= "annotation" expression= " Org.springframework.stereotype.Repository "/>    </context:component-scan></beans>

type: annotation based on annotations, assignable based on a class or interface, ASPECTJ based on Aspectj,regex based on regular expressions, custom based customization

You can also disable Autodiscover and registration using use-default-filters= "false"

Defining Beans

The component is automatically detected during the scan, then the bean name is generated by Beannamegenerator (@Component, @Repository, @Service, @Controller will have a Name property to display Set Bean Name)

@Service ("Mymovielister") public class Simplemovielister {    //...}

you can also generate the bean name yourself, which is named the first letter of the class name lowercase.

You can also customize the Bean naming policy, implement the Beannamegenetator interface, and be sure to include a parameterless constructor

<beans>    <context:component-scan base-package= "Org.example" name-generator= " Org.example.MyNameGenerator "/></beans>

in the

Name-generator= "Org.example.MyNameGenerator"
specifying the implementation of a naming convention

Scope

use annotation @scope to indicate scope

You can also customize the scope policy to implement the Scopemetadataresolver interface and provide a parameterless constructor

<beans>    <context:component-scan base-package= "Org.example" scope-resolver= " Org.example.MyNameGenerator "/></beans>

Agent Mode

You can use the Scoped-proxy property to specify a proxy with three values to choose from: No,interfaces,targetclass

<beans>    <context:component-scan base-package= "Org.example" scoped-proxy= "Interfaces"/></beans >



The definition of bean in spring and the annotation implementation of its scope

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.