Spring笔记二
来源:互联网 发布:手机淘宝花呗怎么开通 编辑:程序博客网 时间:2024/06/06 09:59
Spring笔记二
Spring的装载和启动
Spring是通过上下文(Application Context)装载Bean的定义并组装。
XML配置的Bean用ClassPathXmlApplicationContext比较合适。该类加载位于应用程序类路径下的一个或多个xml配置文件:
public class KnightMain { public static void main(String[] args) throw Exception { ClassPathXmlApplicationContext context=new ClassPathXmlApplicationContext("META-INF/spring/knight.xml"); <!-如果是java配置的bean则用AnnotationConfigApplicationContext--> Knight knight=context.getClass(Knight.class); knight.embarkonQuest(); context.close(); } }
应用切面
DI能够让相互协作的软件组件保持松散耦合,而面向切面变成(AOP)允许你把遍布应用各处的功能分离的功能分离出来形成可重用的组件。
例子(1):
public class Minstrel{ private PrintStream stream; public Minstrel(PrintStream stream){ this.stream=stream; } public void singBeforeQuest() { stream.println("探险之前调用"); } public void singAfterQuest() { stream.println("探险之后调用"); }}
(2):
public class BraveKnigth implements Knight{ private Quest quest; private Minstrel minstrel; public BraveKnight(Quest quest,Minstrel minstrel){ this.quest=quest; this.minstrel=minstrel; } public void embarkOnQuest() throws QuestException{ minstrel.singBeforeQuest(); quest.embark(); minstrel.singAfterQuest(); } }
在spring中配置:
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.soringframeword.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlms:aop="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="knight" class="com.springinaction.knights.BraveKnight"> <constructor-arg ref="quest" /> </bean> <bean id="quest" class="com.springinaction.knights.SlayDragonQuest"> <constructor-arg value="#{T(System).out}" /> </bean> <!--申明Minstrel Bean--> <bean id="minstrel" class="com.springinction.knights.Minstrel"> <constructor-arg value="#{T(System).out}"> </bean> <aop:config> <aop:aspect ref="minstrel"> <!--定义切点--> <aop:pointcut id="embark" expression="execution(* *.embarkInQuest(...))" /> <!--申明前置通知--> <aop:before pointcut-ref="embark" method="singBeforeQuest"/> <!--申明后置通知--> <aop:after pointcut-ref="embark" method="singAfterQuest" /> </aop:aspect> </aop:config> </beans>
阅读全文
0 0
- Spring学习笔记二
- Spring学习笔记二
- spring学习笔记二
- spring学习笔记二
- Spring笔记(二)
- 学习Spring笔记二
- Spring学习笔记<二>
- Spring学习笔记二
- Spring学习笔记二
- spring学习笔记(二)
- Spring笔记二
- Spring笔记(二)
- spring JpetStore学习笔记(二)
- Spring学习笔记(二)
- spring学习笔记(二)
- Spring学习笔记(二)
- spring学习笔记(二)
- Spring Integration学习笔记二
- 二叉树的创建
- 需要熟练的知识
- eclipse jad 反编译 插件安装
- Android Studio常用快捷键
- 转 别人的
- Spring笔记二
- 使用HttpURLConnection
- #bzoj2932#【重庆市NOIP模拟赛】旅行(贪心 DP是不可以的!)
- template(name="remote" type="string" string="%msg%")
- boost库实用工具之exception
- Android 发布项目到私服Nexus仓库
- js 格式化金额
- antd源码解析(一)button控件的解析
- js定时动态增加列表