Assembly Spring Bean (a)

3 ways dependent injected

1. constructor injection

Less time parameters easy to use, more convenient

2.setter injection:

Spring in the most mainstream of injection manner, there is no parameter generating object constructor (invoked by reflection it is ensured that no-argument constructor ), simultaneously injected through the configured value reflecting the corresponding setter.

3. interface injection

Sometimes the resources are not from their own system, but from the outside world, which is injected into the interface


Assembly Bean Overview

How to develop their own bean fitted to the Spring IOC container it? In most scenarios, we will use the implementation class ApplicationContext, since Spring IOC container corresponding function is relatively powerful. Three methods are provided in the spring configuration:

1. Based on the configuration of convention priority principle, should be the highest priority by the discovery mechanism and the automatic assembly of the principle of implicit Bean. Benefit is the decision to reduce the developer's program, simple yet flexible.

2. Under no way to use the automatic assembly of such principles should give priority to implement Java interfaces and classes in the configuration, the advantage is to avoid the proliferation of XML configuration is also easier.

3. In the case of the above conditions are not available, you can only choose to XML configuration Spring IOC container. For example, the code can not be modified when used inside third-party libraries, can only be used through XML configuration.

Guess you like

Origin www.cnblogs.com/xc-xinxue/p/12380400.html