Spring Injection: Configuration and annotations

Source: Internet
Author: User
Tags constructor

I had a little bit of spring in front of a business company, but I didn't know much about it, but I knew it was there. Now in a company internship, only a systematic look at spring, the framework of the online explanation are many, but the main will introduce the spring injection this point, in fact, Spring also has interception function, and spring itself can be used by annotations, You can also use the configuration file, more complex, here is a rough introduction of the spring injection layer, may later write a blog about spring MVC. At the same time this article will also attach oneself in the process of self-study some good information ~ ~

Introduction to Injection

Spring injection can be understood as the initialization of an object, that is, to omit the new step, similar to Factory mode, through a factory to create this object, if you encounter changes, only need to change one place. There are generally two ways to implement spring injection, configuration files, or annotations. Each has its own different roles.

Note: This article is involved so the example is added to the spring package by default, if you do not join the spring package can be Baidu to use Maven or manually download spring's dependency package (this is not a blog, or should do it yourself)


Configuration injection:

Do not repeat the wheel, it has been explained very clearly, introduced two ways, set injection and construction injection, and are very detailed. As for the third type mentioned in this article: interface injection, I small white point of view, should be injected with interface, in the use of MyBatis framework is generally exposed, injected here I do not introduce this, because it involves mybatis another framework, more complex.

PS: The author of the spring series of articles can certainly refer to, I also benefited a lot.

http://blog.csdn.net/lishuangzhe7047/article/details/20740835

Annotation Injection:

Automatic Assembly

Before understanding the annotation injection, you need to understand the concept of automatic assembly , this blog is more detailed, you can refer to this to know some concepts, because this blog does not give a very detailed example, so here I will give an example.

http://blog.csdn.net/lishuangzhe7047/article/details/20742427

First introduce the file structure, main to debug the run, the bean one of the objects, Test,bean depends on an object (that is, the Bean member has a Test object), as shown:

Main.class

Bean.class

Test.class

Here is the configuration file

As can be seen here, there is a autowrite in the bean, this field is used for automatic assembly. Can see in <property> this label I did not write a reference to test, this is automatic assembly, can be based on the name (byname), or depending on the type (Bytype). So here's the output as follows

If I remove the Autowirte field here, because I did not reference test in <property>, the test here will be empty, and will error, because the bean test is not initialized, so the test inside the GETN can not call , it will nullpointer, as shown in the figure below.

Here, based on the removal of the autowrite, I added a <property> test reference, which became manually assembled, just like the first blog above. The configuration file is as follows

So in fact, auto-assembly is actually a dependency on class B in Class A (that is, a member of Class B, remember the concept), and then you can initialize Class B with automatic annotations (but still need to write Class B <bean> tags in the configuration file)


Annotation Injection

Understanding the automatic assembly, you can understand the annotation injection, because it is based on the concept of automatic assembly, the difference is that the annotation injection is not configured in the configuration file so much, only need to configure a little bit, and all the rest with annotations to achieve.

Or just the example, change the configuration file, remove those <bean> tags, add a tag

Where Base-package is its own package name, if there are multiple package names that can be separated by semicolons (semicolons in quotation marks)

Then add some annotations to the target class, as shown in

Bean.class

Just add an annotation to the class, here the annotations can actually write @controller or @service, these comments from the injection effect is not any difference, but some logic is different, it can be understood as a variable can declare an int number, you can declare an int n , just a different name. Then in the test that place added a @resource, in fact, can also write @autowirte, but the official seems to recommend some @resource, in fact, two of the role is the same. The @resource here can be understood as the Autowirte field in the configuration file, where the @repository can be understood as the <bean> tag inside the configuration file.

Test.class

Main.class not change

Then run it like this, as shown in figure

The string object in the Discovery Bean is the Null,int value is 0, and the test int is also 0. This is actually not the initialization of these fields. But the Bean object and test are initialized (to be held to the JVM, the bean and test both request a piece of memory). It can also be understood that the default constructor for the bean and test is called, so the default constructor is empty, so nothing is initialized. You can also override the default constructor yourself, for example by removing the comment from the default constructor inside the Test.class, and then running the result as follows:

This is the annotation injection


Configuration injection vs Annotation injection

In fact, it can be seen that the essence of these two is to call the constructor, but the set injection in the configuration injection is called after the constructor is read from the configuration file and then set in, the construction of the injection is selected to construct a specific constructor. Annotation injection is called the default constructor. If you want to initialize the values in the object member (such as the value of the bean's string object in this example) in the annotation injection, you need to initialize it in the default constructor.

Benefits of choosing a configuration injection

Configuration injection is virtually the same as the factory model, but the advantage of choosing a configuration injection is that you can separate values and code for easy administration, and you can assign values in configuration files

The benefits of choosing annotation Injection

Although annotation and code are mixed with annotations, it is also convenient to match the configuration and class, and the configuration is relatively simple, write a note on the line. But if you want to configure the initialization value with annotations, you can only write in the default constructor.

So what is commonly used is that if you need to inject the value, generally with the configuration injection, such as the database connection class, you can also initialize the user name and password, if there is no specific value of the class, such as the only method of the class, like those controllers and those service, although there are some objects, However, these objects do not need to give any value, as long as the new object is OK, this is usually injected with annotations.

because the predecessors, can be higher

1, Spring Series study first (the article mentioned in the blog is the second and Fourth of the series)

http://blog.csdn.net/lishuangzhe7047/article/details/20740209

2, the servlet under the spring injection, and mentioned the author stepped on the pit, as well as some of the necessary dependency packages

http://hafuokas.iteye.com/blog/2229869

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.