spring 3 简单笔记(一)
spring 3 框架主要提供以下一些主要特性:
一.IOC
IOC 个人理解类似于C++里的多态,只是spring里可以通过XML配置完成,并且除了方法还可以配置属性。spring框架会根据这个配置在启动或调用时实例化这个类。主要的配置如下:
<bean id = "sayHello" class = "com.SayHelloAction"> <property name = "message"> <value>"Fredric"</value> </property> </bean>
public class SayHelloAction implements SayAction { private String Message; @Override public void sayAction() { // TODO Auto-generated method stub System.out.println("SayHelloAction"); System.out.println(Message); }
具体调用如下:
public class App { public static void main(String[] args) { ApplicationContext ctx = new FileSystemXmlApplicationContext("src//applicationContext.xml"); SayAction sayAction = (SayAction) ctx.getBean("sayHello"); sayAction.sayAction(); } }
二.AOP
AOP 在类的某个方法调用前后、异常等情况下,织入某个方法。过去的项目中用的比较多的是日志和数据库操作的缓存机制。在spring 3.0中配置如下:
<aop:config> <aop:aspect id="logAspect" ref="BeforeApp"> <aop:pointcut id="pointcut" expression="execution(* *.*(..))"/> <aop:before method="doBefore" pointcut-ref="pointcut"/> </aop:aspect> </aop:config>
其中aop:pointcut定义那个被织入的点,expression里中表示执行方法的返回值、类、方法名等。这里采用通配符,表示所有的防范。aop:before表示在织入点之前织入BeforeApp这个bean的doBefore防范。