Spring Beans Configurations

Beans Properties

a) Bean property can be set by <Property> syntax.

b) Bean property can be set by p:xxx syntex.

c) Arbitrary properties can be set by <list> or <props> or <map> according to the property type.  

Beans Destory

a) Destory method will be called when the ApplicationContext is closed.

b) Destory method will only be called when the bean scope is "singleton".

Beans Factory

a) The default bean factory is the matched constructor method of the class.

b) The factory method can be assigned to a static method of the class.

c) The factory method can be assigned to a method of another class (factory class). The factory class also has to be added as a bean.

Beans Nest

a) Bean can be defined within a bean, which will be inner bean. The inner bean can't be refered outside its parent bean.

Beans AutoWiring

a) "byType" there can't be ambiguities.

b) "byName" the name or id of the bean has to be the same as the parameter name.

c) "constructor" the bean type has to be matched with the parameter type of the constructor. Name and id can be left as empty.

d) The default autowiring option can be set at xml level. All beans will inherit the setting by default.

Beans Removing Ambiguities

a) exclude the bean from being autowired

b) set the bean as primary for being autowired

Beans Injection by Annotation

The benefit of annotation is that the beans xml can be simplified significantlly.

Ambiguities can be eliminated by corresponding annotations in each of below solutions.

Solution1

a) @Autowired (from Spring Annotation) or @Resource (from JDK Annoationto mark properties in class

b) <context:annotation-config></context:annotation-config> to be added in beans xml to scan annotaion

Solution2

a) @Resource (from JDK Annoation) to mark properties in class

b) <context:annotation-config></context:annotation-config> to be added in beans xml to scan annotaion

Solution3

a) @Inject (from javax.inject.jar which is not a standard part of JDK 1.8) to mark properties in class

b) <context:annotation-config></context:annotation-config> to be added in beans xml to scan annotaion

Beans Create and Destory by AnnotationNote: the bean has to be singleton and the application context has to be closed after using.

a) @PostConstruct (from JDK Annoation) to mark initial method

b) @PreDestroy (from JDK Annoation) to mark destory method

Beans Definations by Annotation

a) @Component(from Spring Annotation) to mark beans for exporting

b) <context:component-scan/> to be added in beans xml to scan classes in assigned jar

Ambiguities can be eliminated by setting names for components

Set Beans Properties by Property File

 

posted @ 2016-09-25 21:43  FunnyBear  阅读(223)  评论(0)    收藏  举报