Spring Study[4]

Alternatively, you can have Spring wire them automatically by setting the autowire property

on each <bean> that you want autowired:
There are four types of autowiring:
■ byName—Attempts to find a bean in the container whose name (or ID) is the same as the name

of the property being wired. If a matching bean is not found, then the property will remain

unwired.
■ byType—Attempts to find a single bean in the container whose type matches the type of the

property being wired. If no matching bean is found, then the property will not be wired. If

more than one bean matches,an

org.springrframework.beans.factory.UnsatisfiedDependencyExcpetion will be thrown.
■ constructor—Tries to match up one or more beans in the container with the parameters of

one of the constructors of the bean being wired. In the event of ambiguous beans or

ambiguous constructors, an org.springframework.beans.factory.UnsatisfiedDependencyException

will be thrown.(by type in constructor)
■ autodetect—Attempts to autowire by constructor first and then using byType. Ambiguity is

handled the same way as with constructor and byType wiring.

you can set a default autowiring for all beans within the Spring configuration wiring file

by setting default-autowire on the root <beans> element:<beans default-autowire="byName">

Working with Spring’s special beans:
■ Become involved in the bean’s and the bean factory’s life cycles by postprocessing
bean configuration
■ Load configuration information from external property files
■ Alter Spring’s dependency injection to automatically convert String values to another type

when setting bean properties—for example, being able to inject a String value into a

java.util.Date field and have the date automatically converted
■ Load textual messages from property files, including internationalized messages
■ Listen for and respond to application events that are published by other beans and by the

Spring container itself
■ Are aware of their identity within the Spring container

The BeanPostProcessor interface gives you two opportunities to alter a bean after it has

been created and wired:
The postProcessBeforeInitialization() method is called immediately prior to bean

initialization (the call to afterPropertiesSet() and the bean’s custom initmethod).Likewise,

the postProcessAfterInitialization() method is called immediately after initialization.

The Spring framework itself uses several implementations of BeanPostProcessor under the

covers. For example, ApplicationContextAwareProcessor is a BeanPostProcessor that sets the

application context on beans that implement the ApplicationContextAware interface.

While a BeanPostProcessor performs postprocessing on a bean after it has been loaded, a

BeanFactoryPostProcessor performs postprocessing on a bean factory after the bean factory

has loaded its bean definitions but before any of the beans have been instantiated.

Two very useful implementations of BeanFactoryPostProcessor are

PropertyPlaceholderConfigurer and CustomEditorConfigurer

PropertyPlaceholderConfigurer loads properties from one or more external property files and

uses those properties to fill in placeholder variables in the bean wiring XML file.

CustomEditorConfigurer lets you register custom implementations of java.beans.PropertyEditor

to translate property wired values to other property types.

你可能感兴趣的:(spring)