Java_oa Management System (iv): annotation based component scanning __SPRINGMVC

Source: Internet
Author: User
Tags class definition
General Statement about:This article is and (three) next to the Brother article, the Code is also (c) those.

content (This is just a beginner's entry, please look at the next step outside)

1 What is Component Scan 2 Specify scan class path
3 annotation marks for auto scan
4 Auto Scan annotation naming
5 specifying the scope of a component
6 specifying initialization and destruction methods
7 Specifying Dependency Injection relationships

8 An expression that injects spring

1 What is a component scan specifies a package path, Spring automatically scans the package and its child packages for all component classes, when the component class definition is found
When a specific annotation tag is used, it is incorporated into the spring container, equivalent to the original XML configuration Bean
The function. 2 Specify scan classpath using component scans, you first need to specify the scan parent package path in the XML configuration, and the container will automatically scan
Com.throne Package and all components under its own package, and instantiate the bean

<context:component-scan base-package= "Com.throne" ></context:component-scan>

And the Java packages in our (c) are all under that path;
3 automatically scanned annotation flags Specify the scanned classpath, and not all components under that path will be scanned into the spring container.
The spring container is scanned only if there is a token before the component class definition.


4 Auto Scan annotation naming (need to import WEBMVC jar package)
When a component is detected during a scan, a default ID value is generated and the default ID value is lowercase
The name of the class that begins, or you can customize the ID in the note. Note: If you want to import a jar package, you will naturally import the jar package;

Like our previous book Class

@Component (the "book") the Public
class book implements serializable{

//attribute--> is also annotated to inject
       Value ("M")        
        private String bookprice;
        @Value ("The Hadoop Authority Guide")
        private String bookname;

Getter and Setter

}

//As for fetching, the ID book

b1=ac.getbean ("book", Book.class) added when the annotation is changed; The class name for the default lowercase first letter (the specification for the class: initial capital)
System.out.println (S.getbook (). Getbookname ());
System.out.println (S.getbook (). Getbookid ());

Results:
     Hadoop Authority guide
     100

Then you do not need to configure the bean in the XML file, so you can easily simplify the programming process by replacing the original XML file configuration with more convenient annotations.

5 specifying the scope of a component Spring-managed components, the default scope is "singleton" (single case mode), if additional scopes are required
You can also use the @scope annotation, as long as you provide the name of the scope in the note, where the value of the property is the same as what is said in (b).

Like our previous book Class

@Component (the "book") the Public
class book implements serializable{

//attribute--> is also annotated to inject
       Value ("M")        
        private String bookprice;
        @Value ("The Hadoop Authority Guide")
        private String bookname;

Getter and Setter

}

//As to fetch, the ID book

b1=ac.getbean ("book", Book.class) added when the annotation is changed;
Book B2=ac.getbean ("book", Book.class);


System.out.println (B1==B2);

Result:
     true
and
@Component ("book")
@Scope ("prototype") the public
class book implements serializable{
}

System.out.println (B1==B2);

At this point, the result is:
       false

6 Specify initialization and destruction methods (understanding) @PostConstruct Specify initialization methods
@PreDestroy Specify the method of destruction

Note: However, a single case can be closed when multiple cases are not closed

Abstractapplicationcontext is destroyed, he is the father of ApplicationContext, (ii) mentioned;

        @PostConstruct public
        void init () {
           System.out.println ("book is initialized");
        }
        @PreDestroy public
        Void Destroy () {
           System.out.println ("book was destroyed");
        }


7 specifying Dependency injection relationshipsA dependency bean object that can be injected into a relationship using any of the following annotations
The value injection of the base type can use the @value () tag


@Component (common annotations, you can try this when you don't know what to use)

A dependency bean object that can be injected into a relationship using any of the following annotations
Note: Setter injection is recommended for use with the @resource constructor @autowired


1 @Autowired/@Qualifier can handle constructor injection and setter injection
@Autowired written in front of the constructor, stating that you need to inject the bean
@Qualifier write in front of the arguments, stating the ID of the bean that needs to be injected

This will reduce the amount of code and simplify the writing process.

You can read this article in detail (the code in this article is more detailed):

spring@autowired Annotation and automatic assembly


@Autowired write on the property, only the assignment of the constructor is executed.
If @Qualifier in a single case pattern can omit

2 @Autowired can also be written on the Set method
@Qualifier can be omitted when injecting a single instance of an object. At this point, spring matches the parameters by type.
@Autowired can also be written on attributes that are similar to those written on the set method, but only execute
Assignment statement

3 Using @resouce annotation
Can only be used on set methods and properties, in the Set method, and all the method bodies are executed (

Like what

Add a phrase to the setter: System.out.println ("I was executed by @resouce in set.") ");

Then, when the Zhegeset is executed, the statement is printed on the console.

), used on attributes
To execute only the assignment of a method

     If it is written in a class (such as a store), it will be @resource, written in the name of the class name, and the previous @Resource private book book;
       At this point, the class will get the method String str= "Applicationcontext.xml";
      ApplicationContext ac=new classpathxmlapplicationcontext (str);
              Store S=ac.getbean ("store", Teacher.class);

System.out.println (S.getbook (). GetName ());
	@Component ("book")//This is at this point in this case the implements serializable{@Value ("M") private String BookID;
	@Value ("The Hadoop Authority Guide") private String bookname;
	Public book () {System.out.println ("I created");
	Public String Getbookid () {return bookid;
	} public void Setbookid (String bookid) {this.bookid = BookID;
	Public String Getbookname () {return bookname;
	} public void Setbookname (String bookname) {this.bookname = BookName;
	@PostConstruct public void init () {System.out.println ("I was initialized");
	@PreDestroy public void Destroy () {System.out.println ("I was destroyed"); }/* Will output: I created//create book I was initialized//init () HadoopAuthoritative guide//<span style= "font-family:arial, Helvetica, Sans-serif;" >.getbook (). GetName () */</span>

Often used to reduce the amount of code and to inject objects.

Note the point:

1 @Resouce is Java EE, it is recommended that you reduce the coupling with spring, and @autowired is in spring.

2 @Autowired Lookup type is "Bytype", @Resouce is "ByName", @Qualifier ("XXX") and @autowired can convert Bytype to byname, where xxx is name;










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.