完整代码请见:https://github.com/codercuixi...
为了下降Java开发的复杂性,Spring采用了如下4种策略:java
(利用面向接口编程的思想,由Spring应用上下文负责依赖注入)
经过依赖注入(DI ,Dependency Inject),对象的依赖关系由系统中负责协调各对象的第三方组件在建立对象的时候进行设定。对象无需自行建立或管理他们的依赖关系,如图1.1所示,依赖关系被自动注入到须要他们的对象中去。
Spring经过应用上下文(Application Context)来装在bean定义并把组装起来。Spring自带了不少不少应用上下文,他们之间的区别仅仅在于如何加载配置。git
package sia.knights; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import java.io.PrintStream; /** * Create by cuixin on 2018/8/25 **/ @Configuration public class KnightConfig { @Bean public Knight knight() { return new BraveKnight(quest()); } @Bean public Quest quest() { return new SlayDragonQuest(stream()); } @Bean public PrintStream stream() { return new FakePrintStream(); } }
1.2. 经过@ContextConfiguration来配置Spring应用上文下信息github
package sia.knights; import org.junit.After; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import org.springframework.test.context.support.AnnotationConfigContextLoader; import javax.annotation.Resource; /** * Create by cuixin on 2018/8/25 **/ @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(classes = KnightConfig.class, loader = AnnotationConfigContextLoader.class) public class KnightJavaConfigInjectionTest { @Autowired private Knight knight; @Resource private FakePrintStream printStream; @After public void clearPrintStream() { printStream.clear(); } @Test public void shuoldInjectKnightWithSlayDragonQuest() { knight.embarkOnQuest(); assert "Embarking on quest to slay the dragon!\n".equals(printStream.getPrintedString()); } }
2.1. knigh.xmlweb
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <bean id="knight" class="sia.knights.BraveKnight"> <constructor-arg ref="quest"/> </bean> <bean id="quest" class="sia.knights.SlayDragonQuest"> <constructor-arg value="#{T(System).out}"/> </bean> </beans>
2.2 经过Spring提供的ClassPathXmlApplicationContext或者FileSystemXmlApplicationContext来寻找xml配置文件spring
package sia.knights; import org.springframework.context.support.ClassPathXmlApplicationContext; /** * Create by cuixin on 2018/8/24 **/ public class KnightMain { public static void main(String args[]){ ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("spring/knight.xml"); Knight knight = context.getBean(Knight.class); knight.embarkOnQuest(); context.close(); } }
面向切面编程(aspect-oriented programming,AOP)容许你把遍及应用各处的功能分离出来造成可重用的组件。express
系统由多个组件组成,好比核心业务,日志,事务管理和安全。编程
日志,事务管理和安全这样的系统服务常常融入到具备核心业务逻辑的组件中去,这些系统服务经过被称为横切关注点。安全
若是无论不问,那么这些横切关注点会重复出如今多个组件中,也会增长每个组件的复杂性。框架
AOP可以使这些服务模块化,而且以声明的方式将他们须要影响的组件中去。咱们能够将切面想象成为覆盖在不少组件之上的一个外壳。模块化
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <bean id="knight" class="sia.knights.BraveKnight"> <constructor-arg ref="quest" /> </bean> <bean id="quest" class="sia.knights.SlayDragonQuest"> <constructor-arg value="#{T(System).out}" /> </bean> <bean id="minstrel" class="sia.knights.Minstrel"> <constructor-arg value="#{T(System).out}" /> </bean> <aop:config> <aop:aspect ref="minstrel"> <aop:pointcut id="embark" expression="execution(* *.embarkOnQuest(..))"/> <aop:before pointcut-ref="embark" method="singBeforeQuest"/> <aop:after pointcut-ref="embark" method="singAfterQuest"/> </aop:aspect> </aop:config> </beans>
样板代码:为了实现通用的和简单的任务,不得不一遍遍重复写的代码。
在基于Spring的应用中,你的应用对象存在于Spring容器(container)中,如图1.4所示,Sring容器负责建立帝乡,装配他们,配置他们并管理他们的整个生命周期,从生存到死亡(在这里,可能就是new到finalize)。
容器是Spring的核心。Spring容器使用DI管理构成应用的组件,他会建立相互协做的组件之间的关联。
3.2.1. bean工厂不经常使用。
由org.springframework.benas.factory.BeanFactory接口定义,是简单的容器,提供最简单的DI支持。应用上下文由orgspringframework.context.ApplicationContext接口定义)基于BeanFactory构建,并提供应用框架级别的
3.2.2.使用应用上下文
AnnotationConfigApplicationContext: 从一个或多个基于Java的配置类中加载Spring应用上下文 ApplicationContext context = new AnnotationConfigApplicationContext(KnightConfig.class) AnnotationConfigWebApplicationContext:从一个或多个基于Java的配置类中加载Spring Web应用上下文 ClassPathXmlApplicationContext:从类路径下的一个或多个xml配置文件中加载上下文定义,把应用上下文的定义文件做为类资源 ApplicationContext context = new ClassPathXmlApplicationContext("knight.xml") FileSystemXmlApplicationContext:从文件系统的一个或多个xml配置文件中加载上下文定义 ApplicationContext context = new FileSystemXmlApplicationContext("c://knight.xml") XmlWebApplicationContext:从Web应用的一个或多个xml配置文件中加载上下文定义。
srping-web-flow . Spring web Service, Spring security,Spring Integration, SpringBatch, Spring Data, Spring Social,Spring Mobile, Spring Boot