Spring learn note
来源:互联网 发布:游戏程序员必看书籍 编辑:程序博客网 时间:2024/06/14 01:48
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.@Configurationpublic 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
- Spring learn note 2
- Spring learn note 3
- Spring learn note 4
- Installing scikit-learn note
- How to Learn Note
- note:some learn remark
- JAVA NET PROGRAM LEARN NOTE
- JAVA NET PROGRAM LEARN NOTE
- python scikit-learn, note 20161209
- NOTE:Spring
- Spring note
- Spring note
- learn spring
- Learn Spring in spring
- c programming language learn note 4
- Note of "Learn Python the HardWay"
- Spring Note 1
- 20、顺时针打印矩阵
- 插件加载---之一
- Linux下使用getopt函数来获取传入的参数
- Codeforces contest 295 recordings
- IPC之共享内存(3)
- Spring learn note
- 字符串匹配KMP算法
- Hibernate缓存总结
- 并发性:互斥和同步
- 进程总结
- eclipset调试1
- 组个最小数
- 非制冷和制冷型红外热像仪的区别
- [坑死老子了]C++ string中的几个小陷阱,你掉进过吗?