Spring learn note
2017-07-30 17:43
344 查看
Core Spring
Spring module and portfolio have a lot of function and feature, but its primary features are dependency injection (DI) and aspect-oriented programming (AOP).Spring aims at Simplifying Java development, Spring employs four key strategies:
Lightweight and minimally invasive development with POJOs
Loose coupling through DI and interface orientation
Declarative programming through aspects and common conventions
Eliminating boilerplate code with aspects and templates
Dependency injection (DI)
The act of creating associations between application components is commonly referred to as wiring. In Spring, there are many ways to wire components together, but a common approach has always been via XML, Spring also allows you to express configuration using Java.e.g, XML
<beans ...> <bean id="person" class="com.gordon.Person"> <constructor-arg ref="eat" /> </bean> <bean id="eat" class="com.gordon.Eat"> <constructor-arg value="#{T(System).out}" /><!-- SEL --> </bean> </beans>
e.g, Java code
Omit relevant package and import. @Configuration public class PersonConfig{ @Bean public Person doSomething() { return new Person(eat()); } @Bean public Eat eat() { return new Eat(System.out); } }
aspect-oriented programming (AOP)
It may help to think of aspects as blankets that cover many components of an application. At its core, an application consists of modules that implement business functionality. With AOP, you can then cover your core application with layers of functionality. These layers can be applied declaratively throughout your application in a flexible manner without your core application even knowing they exist. This is a powerful concept, because it keeps the security, transaction, and logging concerns from littering the application’s core business logic.e.g XML
<beans ...> <bean id="person" class="com.gordon.Person"> <constructor-arg ref="eat" /> </bean> <bean id="eat" class="com.gordon.Eat"> <constructor-arg value="#{T(System).out}" /> </bean> <bean id="washHand" class="com.gordon.washHand"> <constructor-arg value="#{T(System).out}" /> </bean> <aop:config> <aop:aspect ref="washHand"> <!-- AspectJ --> <aop:pointcut id="doing" expression="execution(* *.doSomething(..))"/> <!-- before advice --> <aop:before pointcut-ref="doing" method="washBeforeDoing"/> <!-- after advice --> <aop:after pointcut-ref="embark" method="washAfterDoing"/> </aop:aspect> </aop:config> </beans>
Spring container
In a Spring-based application, your application objects live in the Spring container. The container creates the objects, wires them together, configures them, and manages their complete lifecycle from cradle to grave (or new to finalize(), as the case may be).Spring container can be categorized into two distinct types.
Bean factories (defined by the org.springframework.beans.factory.BeanFactory interface) are the simplest of containers, providing basic support for DI
Application contexts (defined by the org.springframework.context.ApplicationContext interface) build on the notion of a bean factory by providing application-framework services(Recommend)
Application Context
In a Spring application, an application context loads bean definitions and wires them together. The Spring application context is fully responsible for the creation of and wiring of the objects that make up the application.Here are a few that you’ll most likely encounter:
AnnotationConfigApplicationContext
AnnotationConfigWebApplicationContext
ClassPathXmlApplicationContext
FileSystemXmlApplicationContext
XmlWebApplicationContext
How it works
public class PersonMain { public static void main(String[] args) throws Exception { ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext( "META-INF/spring/Persons.xml"); Person person= context.getBean(Person.class); person.doSomething(); context.close(); } }
You can find that it’s similar to annotation. three phase:
Definition
Declaration, use definition
Process and resolution(likes apt)
相关文章推荐
- Spring learn note 4
- Spring learn note 3
- Spring learn note 2
- Spring4 Note @Scope @RequestMapping @Controller
- Spring Note--IOC
- Note of "Learn Python the HardWay"
- NOTE:Spring
- 【spring note】强制初始化JAVA BEAN
- mynote springMVC拦截器配置
- Spring Note--AOP
- Learn Features of Spring 3.0
- Learn Spring in spring(三)
- Learn Spring in spring(一) (zz)
- 学习SPRING在春天 - Learn Spring in spring(一)
- python scikit-learn, note 20161209
- spring-note-01
- [Learn Note] Java Script Guide Line
- Learn Spring in spring(二) (zz)
- http://www.learnspringframework.blogspot.com/
- 学习SPRING在春天 - Learn Spring in spring(二)