Spring learns the definition of bean with the configuration of nine----------Bean and the annotation implementation of its scope

Source: Internet
Author: User

Common annotations for Spring beans

@Component: usually annotations, available for any bean

@Repository: Typically used to annotate the DAO layer, which is the persistence layer

@Service: Typically used to annotate service tiers, or services tiers

@Controller: Typically used to annotate the controller layer, which is the control layer

Automatic detection of classes and the registration of beans

<context:component-scan base-package= ""/>: Automatically scans for classes under Base-package defined packages or their sub-packages, and will have @component, @Controller, @Service , the classes of annotations such as @Repository are automatically registered in the IOC container.

<CONTEXT:ANNOTATION-CONFIG/>: Implicitly registers the autowiredannotationbeanpostprocessor with the spring container, Commonannotationbeanpostprocessor, Persistenceannotationbeanpostprocessor and Requiredannotationbeanpostprocessor These four beanpostprocessor.

<context:component-scan/> contains <context:annotation-config/>, so use <context:component-scan/> will not be used <context:annotation-config/>

Defining beans

The bean name is generated by Beannamegenerator (@Component, @Controller, @Service, @Repository has a name property for the specified bean name to display, and the default is the class name first letter lowercase )

You can also use the Name-generator in <context:component-scan/> to customize the bean's naming policy, but implement the Beannamegenerator interface and include a parameterless constructor

Scope

@Scope annotation identifies the scope of the bean. The default is singleton.

Instance

1. Project structure

2.pom.xml

<project xmlns= "http://maven.apache.org/POM/4.0.0" xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance" xsi: schemalocation= "http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd" ><modelversion >4.0.0</modelversion><groupid>org.spring</groupid><artifactid>spring-beanannotation </artifactid><packaging>jar</packaging><version>0.0.1-snapshot</version><name >spring-beanannotation Maven webapp</name><url>http://maven.apache.org</url><properties ><spring.version>4.3.7.RELEASE</spring.version></properties><dependencies><!-- JUnit relies on--><dependency><groupid>junit</groupid><artifactid>junit</artifactid> <version>4.12</version><scope>test</scope></dependency><!--Spring Core dependency-- <dependency> <groupId>org.springframework</groupId> <artifactid>spring-core</artifaCtid> <version>${spring.version}</version></dependency><dependency> <groupId> Org.springframework</groupid> <artifactId>spring-beans</artifactId> <version>${ Spring.version}</version></dependency><dependency> <groupid>org.springframework</ Groupid> <artifactId>spring-context</artifactId> <version>${spring.version}</version> </dependency></dependencies><build><finalName>Spring-BeanAnnotation</finalName> </build></project>

3.spring-beanannotation.xml

<?xml version= "1.0" encoding= "UTF-8"? ><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.xsd        Http://www.springframework.org/schema/context        Http://www.springframework.org/schema/context/spring-context.xsd ">        <!--automatically scan the bean under the package and register it in the IOC container--    <context:component-scan base-package= "Org.spring.annotation.bean"/>    </beans>

4.beanannotation.java

Package Org.spring.annotation.bean;import Org.springframework.context.annotation.scope;import Org.springframework.stereotype.Component, @Scope ("Prototype") @Componentpublic class Beanannotation {public void Say ( {System.out.println ("Get success in annotation mode");} public void Hascode () {System.out.println ("beanannotation:" + this.hashcode ());}}

5.testbase.java

Package Org.spring.annotation.test;import Org.junit.after;import Org.junit.before;import Org.springframework.context.support.classpathxmlapplicationcontext;import org.springframework.util.StringUtils; public class Testbase {private Classpathxmlapplicationcontext context;private String xmlpath;/** * No parameter constructor */public Testbas E () {}/** * with parameter constructor, initialize profile path *  * @param xmlpath * Profile path */public testbase (String xmlpath) {this.xmlpath = Xmlpath;} /** * Initialize Spring's IOC container */@Beforepublic void before () {if (Stringutils.isempty (Xmlpath)) {//config file default path Xmlpath = "Classpath: Spring-*.xml ";} Load the config file into the spring container in context = new Classpathxmlapplicationcontext (Xmlpath.split ("[, \\s]+"));//Start IOC container Scontext.start ( );} /** * Destroy container */@Afterpublic void after () {if (context! = null) {Context.destroy ();}} /** * Gets the Bean object based on the Bean ID */public object Getbean (String ID) {return context.getbean (ID);}}

6.testbeanannotation.java

Package Org.spring.annotation.test;import Org.junit.test;import Org.spring.annotation.bean.beanannotation;public Class Testbeanannotation extends Testbase {/** * constructor incoming Spring profile path */public testbeanannotation () {Super ("Classpath: Spring-beanannotation.xml ");} /** * Test annotation method Gets the Bean object */@Testpublic void testbeanannotation () {beanannotation bean = (beanannotation) Super.getbean (" Beanannotation "); Bean.say ();} /** * The scope of the bean that tests the annotation method */@Testpublic void Testbeanscope () {beanannotation bean = (beanannotation) Super.getbean (" Beanannotation "); Bean.hascode (); Beanannotation bean2 = (beanannotation) super.getbean ("Beanannotation"); Bean2.hascode ();}}

7. Preview of effects

Reference: http://www.imooc.com/video/4030

Spring learns the definition of bean with the configuration of nine----------Bean 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.