spring实战笔记1 spring概览

来源:互联网 发布:网络兼职申请表 编辑:程序博客网 时间:2024/05/29 04:50

趁新机器装系统时间,把spring实战翻出来看看。前几天下载的没空看,实际开始写(其实是copy)代码时候经常遇到问题,不清楚代码配置的含义,理论知识用到时才知少。


从头系统的看一遍spring很有必要,配合代码学习应该比较快。


Spring核心:依赖注入  面向切面编程


DI 依赖注入,简化开发,降低耦合。 

MrLi 是BOSS的一个对象,李经理。通过注入quest,可以接受各种任务,然后通过excute去执行这种任务。

只需要注入合适的quest,就能实现各种任务。这样就简化了开发,降低了对象之间的耦合程度。

public class MrLi implements Boss{private Quest q;public getquest(Quest quest){this.q=quest;}public excutequest(){q.excute();}}
public class seecustomer implements Quest(){String str;public doproject1 (String s){this.str=s;}public excute(){System.Out.Println(str);}}

通过构造函数传递进去一串字符,在执行时打印出来。具体装配如下:
<bean id=boss class="com.*.Boss.MrLi" >
<constructor-arg ref="quest" />
</bean>
<bean id=quest class="com.*.Quest.seecustomer" >
<constructor-arg value="Boss has saw the customer!" />
</bean>
如上,可以建立一个quest,然后装入boss内。实现李经理拜访客户的任务。

JAVA配置方式:
@Configuration   表明这是一个配置类
public class bossconf{
@Bean
public Boss boss(){
return new MrLi(quest());
}
@Bean
public Quest quest(){
return new seecustomer("Boss has saw the customer!");
}
}
切面AOP
有个秘书记录经理的行程。
public class MrsZhao implement Secretary{
private String str;
public MrsZhao(String s){
this.str=s;
}
public void recbef(){
System.Out.Println(this.str+"Boss take a quest");
}
public void recaft(){
System.Out.Println(this.str+"Boss excute a quest");
}
}

先配置bean
<bean id="secretary" class="com.*.secretary.MrsZhao" >
<constructor-arg value="MrsZhao record:"/>
</bean>
配置切面工作
<aop:config>
 <aop:aspect ref="secretary">
  <aop:pointcut id="excute" expression="excution().).excute(..))" />  <!--表达式后续研究-->
  <aop:before pointcut-ref="excute" method="recbef" />
  <aop:after pointcut-ref="excute" method="recaft" />
 </aop:aspect>
</aop:config>

spring jdbctemplate利用模板简化代码。后续数据库相关补充

应用上下文。
加载xml    ApplicationContext context=new FileSystemXmlApllicationContext("c:/aaa/a.xml");
ApplicationContext context=new ClassPathXmlApllicationContext("a.xml");classpath路径
ApplicationContext context=new AnnotationConfigApllicationContext(com.*.Boss.bossconf);加载java配置

bean声明周期 了解。







原创粉丝点击