Spring Study[4]

來源:互聯網
上載者:User

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.

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.