spring实战笔记1 spring概览

趁新机器装系统时间,把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声明周期 了解。







 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值