http://docs.spring.io/spring/docs/3.1.x/spring-framework-reference/html/beans.html
Principal: dependency only through (in a word, not hard-wired in code!)
constructor arguments
factory method arguments
properties set after construction or factory
Packages:
org.springframeworks.beans
org.springframework.context
interface BeanFactory - configuration framework and basic fucntionality
interface ApplicationContext extends BeanFactory - adds more enterprise-specific functionality
Configuration metadata
XML
Java annotation
Java code
ApplicationContext - represents the IoC container, implementations:
ClassPathXmlApplicationContext or FileSystemXmlApplicationContext
4.2.1 Configuration metadata
bean definition
typically defined: service layer objects, DAOs, presentation objects, infrastructure such as session factories, etc.
typically not used for: fine-grained domain objects, because it's usually created and loaded by DAOs and business logic
can integrate AspectJ to configure objects created outside the control of IoC container
bean id and class
4.2.2 instantiating a container
new one of the implementations and provide with configuration metadata
ApplicationContext context = new ClassPathXmlApplicationContext(new String[] {"services.xml", "daos.xml"});
XML <import resource="xxx.xml"/> imports multiple files
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://your.organization.com/your/bean/definition/id" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
4.2.3 Using container
To retrieve bean
T getBean(String name, Class<T> requiredType)
Ideally application code should never use container methods directly, thus not dependent on Spring API.
Internally, bean definitions (XML, annotation or java object) are represented as BeanDefinition objects, contains
name
class name
behaviour
scope
lifecycle call backs
references
others
connection pool
pool size
Bean created by users outside the container can be registered via getBeanFactory.registerSingleton(..) and registerBeanDefinition(..)
4.3.1 Naming
Usually one, aliases supported
Convention: standardJavaInstanceFieldName
4.3.2 Instantiating beans
default constructor
static factory method
"factory-method" attribute
instance factory method
"factory-bean" refers to the bean name
"factory-method"
constructor based
sequentially <constructor-arg ref="xxx"/>
type match <constructor-arg type="int" value="1234"/>
index <constructor-arg index="0" value="2334"/>
name <constructor-arg name="xxx" value="122" />
nested bean definition
setter based
<property name="xxx"><ref bean="xxx"/></property>
<property name="beanTwo" ref="yetAnotherBean"/>
<property name="integerProperty" value="1"/>
<property name="xxx"><idref bean="xxx"/> </property> (checked at deployment)
<ref bean="xxx"/> <ref local="xxx"/> (using local is best choice if target is in same XML)
inner bean - anonymous, and always prototypes scoped
<list/> <set/> <map/> and <props/>
collection merging supported
strong-typed collection
p and c name space (not read....)
compound property names supported (the dot notion)
<depends-on> declares dependency (usually not needed, only when not explicit with ref) so force initialization sequence
<lazy-init>
auto-wiring: reduce configuration effort and auto adapt change
modes
no (default)
byName - give "master" to "setMaster()"
byType - exactly one bean of the type exists in container
constructor - byType, but applies to constructor arguments
method injection
problem: singleton A needs a new instance of B every time being invoked
solution 1: forget IoC, call context and obtain the bean B
solution 2: lookup method injection - inject an abstract method via byte-code manipulation with CGLIB
solution 3: arbitrary method replacement - just replace the method...
singleton - default, one per IoC container
prototype - any number of instances
create new one every time
initialization callback called
destruction callback NOT called (because destruction not managed)
request - one for each HTTP request, valid in aware of Spring ApplicationContext
session - one per HTTP Session
global session - one per global HTTP Session, only valid in a portlet context
Scoped bean as dependencies:
For example, if want to inject an HTTP request scoped bean into another bean, must inject an AOP proxy in place of the scoped bean with same public interface. (study later)
Custom scopes can be defined
4.6.1 life cycle callbacks
upon initialization
upon destruction
Lifecycle interface - any object can implement to has its own lifecycle requirements
SmartLifecycle - fine control of lifecycle sequence and dependency
Aware interfaces:
ApplicationContextAware
BeanAware - aware bean name
others...
a form of template
4.8.1 BeanPostProcessor
Work on a bean instance after IoC container instantiates the instance
4.8.2 BeanFactoryPostProcessor
Operates on bean configuration metadata, and potentially change it before container instantiates any bean
4.8.3 FactoryBean
Write complex initialization code with Java instead of XML
JSR-330 annotations - javax.inject.*
JSR-250 annotations - @PostConstruct, @PreDestroy
@Required
@Autowired - may fine tune with qualifiers
@Resource (JSR-250) - Java EE managed resources
some XML is needed to kick start... study later
An option for implicitly detecting the candidate components by scanning the classpath, therefore removes the need to use XML.
Study further....
To get a good reference or cheat sheet
4.12.1 Basic concepts: @Configuration and @Bean
@Configuration annotated class
consists of @Bean annotated methods that define instantiation, configuration and initialization logic
to work, must include CGLIB jar
note the implementation: see below, how scope is managed
4.12.2 Instantiating spring container using AnnotationConfigApplicationContext
AnnotationConfigApplicationContext:
accepts @Configuration classes
accepts @Component classes (JSR-330)
register().... refresh() - pragmatically build container
scan(String...) - scan component under base package
AnnotationConfigWebApplicationContext - with web application support
4.12.3 Composing Java-based configurations
@Import - import another @Configuration class
Dependency injection
use @Autowired on @Bean
first write "private @Autowired DependencyClass dependency;"
use this private variable in construction method
use @Autowired on @Configuration - fully qualified, completely explicit
private @Autowired SomeConfigurationClass someConfiguration
get the bean - someConfiguration.beanNameMethod()
Mixing XML with @Configuration classes (study later)
@Bean annotation:
call another @Bean method for dependencies
JSR-250 @PostConstruct and @PreDestroy supported
callback methods: @Bean(initMethod="methodName"), @Bean(destroyMethod="cleanup")
standard *Aware interfaces supported
name="xxx" - customize naming
name = {"name1","alias1","alias2"} aliasing
@Scope - annotate @Bean method with scope
advanced (study later): proxy, lookup method injection
VERY IMPORTANT: THE MECHANISM
all @Configuration classes are subclassed at start-up with CGLIB, in subclass, @Bean method is checked for scope before being called!
that means, if the bean is singleton scoped, subsequent call for the @Bean method only retrieves the singleton instead of calling the method
for JPA and for AspectJ load-time weaving
access messages in i18n-style through MessageSource interface
access to resources through ResourceLoader interface
event publication to beans implementing ApplicationListener interface through use of ApplicationEventPublisher interface
loading multiple contexts through HierarchicalBeanFactory interface
... to be studied later....