Spring框架作用是简化java开发的复杂性。下面是spring in action 对spring初步介绍。
一、主要有4种关键策略:
1. 基于POJO的轻量级和最小侵入性编程 。
2. 通过依赖注入和面向接口实现松耦合。
3. 基于切面和惯例进行声明式编程 。
4.通过切面和模板减少样板式代码。
public class People { private Play play; /** * play 接口注入进构造器 * @param play */ public People(Play play) { this.play = play; } public void doSomething(){ play.playSomeThing(); } }
现在有一个实现了Play接口的实现类 PlayBasketBall 。
public class PlayBasketBall implements Play { public void playSomeThing() { System.out.println("play basketball。。。。。。。"); } }
我们怎么把PlayBasketBall交给People呢?
这个问题就涉及到另一个行为:装配。装配就是创建组件之间协作的行为。
Spring有多种装配方式:XML方式,java注解方式等等。
我们用xml方式装配,新建services.xml。
<?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-3.2.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <bean id="people" class="com.spring.di.People"> <constructor-arg ref="playBasketBall"/> </bean> <bean id="playBasketBall" class="com.spring.di.PlayBasketBall"> </bean> </beans>
对象的创建和组装
public class PlayMain { public static void main(String[] args) { ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("services.xml"); People people= context.getBean(People.class); //People people= (People) context.getBean("people"); people.doSomething(); } }
以上就是spring依赖注入的一个认识。
2. AOP面向切面编程
AOP:通过预编译方式和运行期动态代理实现程序功能的统一维护的一种技术。
如上面的People我们可以对doSomething方法前后动态加入一些操作。
列如加入切面:
public class PlayUtil { public void playBefore(){ System.out.println("play before do........."); } public void playAfter(){ System.out.println("play after do........."); } }
配置文件:
<?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-3.2.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <bean id="people" class="com.spring.di.People"> <constructor-arg ref="playBasketBall"/> </bean> <bean id="playBasketBall" class="com.spring.di.PlayBasketBall"> </bean> <!-- 切面 --> <bean id="playUtil" class="com.spring.aop.PlayUtil"/> <aop:config> <aop:aspect ref="playUtil"> <!-- 切点 expression表达式AspectJ切点表达式语言--> <aop:pointcut id="doSomething" expression="execution(* *.doSomething(..))"/> <!-- 前置通知--> <aop:before pointcut-ref="doSomething" method="playBefore"/> <!-- 后置通知 --> <aop:after pointcut-ref="doSomething" method="playAfter"/> </aop:aspect> </aop:config> </beans>
这样的方式可以把某些功能分离出来形成可重用的组件。
3.使用模板消除样板式代码
例如jdbc访问数据库代码。如果每个操作数据库的地方都写重复的连接数据库、关闭连接等操作那就太低效了。
spring的JdbcTemplate就避免了传统的样板式代码。
4.Spring容器
这是Spring框架的核心。负责创建对象、装配对象、配置对象并管理他们的整个生命周期。Spring的容器归为两类。一类是bean工厂、另一类是应用上下文。其实应用上下文也是基于BeanFactory构建。
a)应用上下文:ClassPathXmlApplicationContext:在所有的类路径(包含jar文件)下查找xml.
FileSystemXmlApplicationContext:在指定文件系统路径下查找xml.
上下文就绪后、我们就可以通过上下文的getBean()方法从容器中获取bean.
b)bean的生命周期
1、Spring对bean进行实例化
2、Spring将值和bean的引用注入到bean对应的属性中
3、如果bean实现了BeanNameAware接口,Spring将bean的ID传递给setBeanName()方法
4、如果bean实现了BeanFactoryAware接口,Spring将调用setBeanFactory()方法,将BeanFactory容器实例传入
5、如果bean实现了ApplicationContextAware接口,Spring将调用setApplicationContext方法,将bean所在应用上下文的引用传入进来
6、如果ean实现了BeanPostProcessor接口,Spring将调用它们的postProcessBeforeInitialization()方法
7、如果bean实现了InitializingBean接口,Spring将调用它们的afterPropertiesSet()方法,类似,如bean使用init-methon声明初始方法,该方法会被调用
8、如果bean实现BeanPostProcessor接口,Spring将调用它们的postProcessAfterInitialization()方法
9、此时、bean已经准备就绪。他们将一直驻留在应用上下文中、直到该上下文被销毁
10、如果bean实现了DisposableBean接口,Spring将调用其destory()接口方法;同样、如果有指定destroy-method声明了销毁方法,该方法也会被调用