четвер, 11 лютого 2016 р.

Spring


  1. What is Spring
  • lightweight jar libraries
  • container (manages lifecycle of objects) (there is no need to use new for creating objects. All objects are in container)
  • framework (there are a lot of classes which can help with data base, services)
  • Dependency Injection (Inversion of Control) (objects get their their dependencies and do not create it)
  • AOP (Aspect Oriented Programming)
2. How to inject information in bean: by constructor ; by getters and setters
In class Client create construstor :
    public Client(String name, String ID) {
        this.fullName = name;
        this.id = ID;
    }

<bean id="client" class="ua.epam.spring.core.beans.Client">
<constructor-arg value="John Smith"/>
<constructor-arg value="1"/>
</bean>

3. How to inject beans in each other. Use ref:
        <bean id="app" class="ua.epam.spring.core.App"/>
<constructor-arg ref="client"/>
<constructor-arg ref="eventLogger"/>
</bean>

4. There are two types of containers in Spring:
- BeanFactory (simple container that supports only DI)
- ApplicationContext (supports DI and frameword services
ClassPathXmlApplicationContext 
FileSystemXmlApplicationContext
AnnotationConfigApplicationContext
XmlWebApplicationContext 
StaticApplicationContext)

5. How to create context
ApplicationContext ctx = new ClassPathXmlApplicationContext( "spring.xml");
App app = (App) ctx.getBean("app");
app.logEvent("Some event for 1");

Немає коментарів:

Дописати коментар