public class HelloWorldBean {
public String sayHello(){
return "Hello World";
}
}
/**这个拗口的类名是作者为了拟合一个模拟的场景特地取名
* 这个类表示营救少女的骑士
* Created by Wung on 2016/8/25.
*/
public class DamselRescuingKnight implements Knight{
private RescueDamselQuest quest;
/**
* 在它的构造函数中自行创建了RescueDamselQuest
* 这使得DamselRescuingKnight和在它的构造函数中自行创建了RescueDamselQuest耦合在了一起
*/
public DamselRescuingKnight(){
this.quest = new RescueDamselQuest();
}
public void embarkOnQuest(){
quest.embark();
}
}
/**
* 这个骑士可以执行各种冒险任务而不在仅仅是之前那个营救少女的任务
* Created by Wung on 2016/8/25.
*/
public class BraveKnight implements Knight{
private Quest quest;
/**
* BraveKnight没有自行创建冒险类型,而是在构造的时候把冒险任务作为参数传入
* 这是依赖注入的方式之一:构造器注入
*/
public BraveKnight(Quest quest){
this.quest = quest;
}
public void embarkOnQuest(){
quest.embark();
}
}
/**屠龙的冒险任务(这个作者好中二阿)
* Created by Wung on 2016/8/25.
*/
public class SlayDragonQuest implements Quest{
private PrintStream stream;
public SlayDragonQuest(PrintStream stream){
this.stream = stream;
}
public void embark() {
stream.print("slay the dragon");
}
}
<!--
这就是一个装配的过程,将SlayDragonQuest声明为一个bean,取名为"quest"
因为是构造器注入所以使用constructor-arg属性 value表示注入的值
那么这个过程解决了之前的问题,将PrintStream对象注入到SlayDragonQuest对象中
-->
<bean class="impl.SlayDragonQuest" id="quest">
<constructor-arg value="#{T(System).out}">
</constructor-arg></bean>
<!--
这个过程同上,BraveKnight声明为一个bean取名为"knight"(不一定使用到这个名字)
然后BraveKnight的构造器参数要求为Quest类型此时传入了另外一个bean的引用
就是上面这个名字"quest"的bean的引用,此时也完成了对BraveKnight的构造器注入
-->
<bean class="impl.BraveKnight" id="knight">
<constructor-arg ref="quest">
</constructor-arg></bean>
/**基于Java的配置文件实现对象的装配
* Created by Wung on 2016/8/26.
*/
@Configuration
public class KnightConfig {
@Bean
public Knight knight(){
return new BraveKnight(quest());
}
@Bean
public Quest quest(){
return new SlayDragonQuest(System.out);
}
}
public class KnightMain {
public static void main(String[] args){
AnnotationConfigApplicationContext context = new
AnnotationConfigApplicationContext(KnightConfig.class);
//从配置文件中就可以获取到bean的定义了
Knight knight = context.getBean(Knight.class);
knight.embarkOnQuest();
context.close();
}
}
/**一个歌手类用来歌颂骑士也就是服务于骑士类
* Created by Wung on 2016/8/26.
*/
public class Minstrel {
private PrintStream stream;
public Minstrel(PrintStream stream){
this.stream = stream;
}
//在冒险之前执行
public void singBeforeQuest(){
stream.print("Begin");
}
//在冒险之后执行
public void singAfterQuest(){
stream.print("End");
}
}
public class BraveKnight implements Knight{
private Quest quest;
private Minstrel minstrel;
/**
* BraveKnight没有自行创建冒险类型,而是在构造的时候把冒险任务作为参数传入
* 这是依赖注入的方式之一:构造器注入
*/
// public BraveKnight(Quest quest){
// this.quest = quest;
// }
public BraveKnight(Quest quest, Minstrel minstrel){
this.quest = quest;
this.minstrel = minstrel;
}
public void embarkOnQuest(){
minstrel.singBeforeQuest();
quest.embark();
minstrel.singAfterQuest();
}
}
<!-- 意思就是将上述id为minstrel的bean配置为切面实际上就是把歌手配置为切面 -->
aspect ref="minstrel">
<!--
定义切入点,也就是在哪里会使用切面
expression="execution(* *.embarkOnQuest(..))
是一种AspectJ的切点表达式语言后面会深入
此处的意思是会在embarkOnQuest()方法处切入
下面那个分别为前置通知和后置通知在切入点之前和之后执行
-->
</aop:after></aop:before></aop:pointcut>
</aop:</aop:config>
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有