Spring Combat Assembly Bean

Source: Internet
Author: User

Optional Scenarios for 1.1Spring configuration

The spring container is responsible for creating the beans in the application and reconciling the relationships between these objects through DI. However, as a developer, you need to tell spring which beans to create and how to assemble them together. When describing how a bean is assembled

, Spring offers a great degree of flexibility, providing three main assembly mechanisms:

1. Explicit configuration in XML.
2. Explicitly configure in Java.
3. Implicit bean discovery mechanism and auto-assembly

1.2 Automated Assembly Beans

* Component Scan (component scanning): Spring automatically discovers the beans created in the app context.

* Automatic assembly (autowiring): Spring automatically satisfies the dependencies between beans.

The combination of component scanning and automated assembly gives you a powerful power to
Your explicit configuration is reduced to a minimum.

1.3 Compactdisc Implementation class with @component annotations Sgtpeppers

you should note that the @component annotation is used on the Sgtpeppers class. This simple note shows that the class acts as a component class and tells spring to create a bean for the class. It is not necessary to explicitly configure Sgtpeppersbean because this class uses the

@Component annotations, so spring will handle things for you. However, component scanning is not enabled by default. We also need to explicitly configure spring to command it to find a class with @component annotations and create a bean for it

  

The configuration class in listing 2.3 shows the simplest configuration to accomplish this task.

However, now we just have to look at the Cdplayerconfig class and not explicitly declare any beans, except that it uses the @componentscan annotation, which enables component scanning in spring. If there is no other configuration, @ComponentScan will scan the same package as the configuration class by default. Because the Cdplayerconfig class is in the Soundsystem package, Spring will scan the package and all the sub-packages under the package to find classes with @component annotations. In this way, you can find Compactdisc, and

and automatically creates a bean for it in spring.

If you prefer to use XML to enable component scanning, you can use the <context:component-scan> element of the spring context namespace. Listing 2.4 shows the most concise XML configuration for enabling component scanning.

1.4Spring Test Start-up

The annotation @contextconfiguration tells it that the configuration needs to be loaded in cdplayerconfig. Because the Cdplayerconfig class contains the @componentscan,

This final application context should contain Compactdiscbean.

1.5 Naming the bean scanned by the component

All beans in the Spring app context will be given an ID. In the previous example, although we did not explicitly set an ID for Sgtpeppersbean, spring assigns it an ID based on the class name. Specifically, the bean is given an ID of sgtpeppers, which means the first letter of the class name becomes lowercase. If you want to set a different ID for this bean, all you have to do is pass the desired ID as a value to the @component annotation. For example, if you want to identify this bean as lonelyheartsclub, you need to configure the @component annotation for the Sgtpeppers class as follows:

There is another way to name the bean, which does not use @component annotations, but instead uses the Java Dependency Injection specification (Java Dependency injection)

@named annotations to set the ID for the bean:

Spring supports @named as an alternative to @component annotations. There are some subtle differences between the two, but in most scenarios they can be replaced with each other.

That being said, I like @component annotations more strongly, and for @named ...

1.6 Set up the base package for component scanning

To specify a different base package, all you need to do is to indicate the package name in the @componentscan Value property:

If you want to make it more clear that you are setting up a basic package, you can configure it with the Basepackages property

You may have noticed that the Basepackages attribute uses the plural form. If you are wondering if this means that you can set up multiple base packages, then congratulations are right. If you want to do this, just set the Basepackages property to scan the package

An array of:

@ComponentScan also provides another way to designate it as a class or interface that is contained in a package:

As you can see, the Basepackages property is replaced with Basepackageclasses. At the same time, we are not using the string type name to specify the package, and the array set for the Basepackageclasses property contains the class. The packages in which these classes are located will be the base package for the component scan. Although in the sample, I set the component class for basepackageclasses, you might consider creating an empty markup Interface (Markernterface) in the package to be used for scanning. By marking the interface, you can still maintain a friendly interface reference to the refactoring, but avoid referencing any actual application code (in later refactoring, the application code may be removed from the package you want to scan).

In your application, if all the objects are independent and have no dependencies on each other, like Sgtpeppersbean, you may need to scan the components. However, many objects rely on other objects to complete the task. In this case, we need a way to assemble the beans that the component scans and their dependencies. To do this, we need to look at another aspect of spring automation configuration, which is automatic assembly.

1.7 Automatic assembly by adding annotations to beans

Simply put, automatic assembly is a way for spring to automatically satisfy bean dependencies, and in the process of satisfying dependencies, it looks for other beans that match a bean's requirements in the context of the spring application. In order to declare automatic assembly, we can take advantage of Spring's @autowired annotations. Consider, for example, the CDPlayer class in listing 2.6 of the program. The @autowired annotation is added to its constructor, which indicates that when spring creates Cdplayerbean, it is instantiated by this constructor and passed in a set to

Compactdisc type of bean.

@Autowired annotations can be used on any method of the class. Assuming that the CDPlayer class has a Insertdisc () method, then @autowired can play a full role, as in Setcompactdisc ()

The same effect:

If there is no matching bean, spring throws an exception when the application context is created. To avoid the appearance of exceptions, you can set the required property of @autowired to False:

When you set the Required property to false, spring attempts to perform an automatic assembly, but if there is no matching bean, spring will leave the bean in an unbound state. However, when you set the Required property to False, you need to be cautious. If you do not have a null check in your code, the NullPointerException property is likely to appear in the non-assembled state.

@Autowired is a spring-specific annotation. If you don't want to use spring's specific annotations everywhere in your code to accomplish automated assembly tasks, you might consider replacing them with @inject:

@Inject annotations are derived from the Java Dependency Injection specification, which also defines @named annotations for us. In automatic assembly, spring supports both @inject and @autowired. Although there are some subtle differences between @inject and @autowired, in most scenarios, they can be replaced with each other. In @inject and @autowired, I don't have a particularly strong bias. In fact, in some projects, I will find that I use both annotations at the same time. However, in the example of this book, I will always use @autowired, and you can choose any one of them according to your own situation.

Spring Combat Assembly Bean

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.