Knowledge Transfer

Ethickfox kb page with all notes


Project maintained by ethickfox Hosted on GitHub Pages — Theme by mattgraham

Inversion Of Control

Abstract principle that passes responsibility of creation and managing of objects for some external entity - IOC Container. Spring framework provides two implementations of IOC container in the form of differencebetweenbeanfactoryvsa

Dependency Injection

Object configuration style, which passes responsibility for object fields definition to another, external entity. It lowers coupling between objects. Object is being provided of all necessary dependencies when created.

Field Injection

Not recommended because harder to test

@Component("someBean")
public class SomeBean implements InitializingBean, DisposableBean {


	@Autowired
	private InjectedBean innerBean;

}

Setter Injection

Would be used in cases when there are some circular dependencies

@Component("someBean")
public class SomeBean implements InitializingBean, DisposableBean {

	private String text;

	public void setInnerBean(InjectedBean innerBean) {
		this.innerBean = innerBean;
	}

}

Constructor Injection

Recommended type

@Component("someBean")
public class SomeBean implements InitializingBean, DisposableBean {

	private InjectedBean innerBean;

	public SomeBean(InjectedBean innerBean) {
		this.innerBean = innerBean;
	}
}

If there are more then one constructor, one of them should be marked @Autowired to work with Spring

Method Injection

If you need Singleton bean to return prototype object. Spring  dynamically redefines method to return a bean from context

@Component
public abstract class SingletonBean {

	public SingletonBean(){
		System.out.println("Singleton Bean Instantiated !!");
	}

	@Lookup
	public abstract PrototypeBean getPrototypeBean();
	
}

Injecting the prototype into a singleton.

When we inject the prototype into a singleton, this object would be created only once. To change this behavior prototype bean’s proxy mode may be changed or the @Lookup method created.

Bean Injection

Dependency Lookup

Object knows whom it should ask for the dependencies

ApplicationContext

Spring framework provides two implementations of IOC container in the form of differencebetweenbeanfactoryvsa

Create container

ClassPathXmlApplicationContext context = 
		new ClassPathXmlApplicationContext("appContext.xml");

//get from Container
Pet pet = context.getBean(Pet.class);

BeanFactory

Highest level interface of IOC Container in Spring. Defines  getBean, containsBean...

Context usage

Context Initialization

  1. Parse Config and create BeanDefinitions
  2. Настройка созданных BeanDefinition - у нас есть возможность повлиять на то, какими будут наши бины еще до их фактического создания, иначе говоря мы имеем доступ к метаданным класса. Для этого существует специальный интерфейс BeanFactoryPostProcessor. For ex @Value("${host}") -> @Value("127.0.0.1")
  3. Create custom FactoryBean
  4. Create Bean Instances
  5. Setup created beans

Context Closing

Factory bean

There are two kinds of beans in the Spring bean container: ordinary beans and factory beans. Spring uses the former directly, whereas latter can produce objects themselves, which are managed by the framework.

Let's look at the FactoryBean interface first:

public interface FactoryBean {
    T getObject()throws Exception;
    Class<?> getObjectType();
		boolean isSingleton();
}

Let's discuss the three methods:

BeanDefinitionMap - Для еще не созданных бинов есть Map<BeanName, BeanDefinition>, а для созданных - Map<BeanName, Bean>

Dependency Resolution