Spring下载网址以及相关知识点

Jar包和文档下地址:
[url]http://repo.spring.io/release/org/springframework/spring/[/url]

源代码下载地址:
[url]https://github.com/spring-projects/spring-framework[/url]
可以选择需要的版本,右下角有个按钮“Download ZIP”

本文以Spring3.2.8为例

一、Spring自动装配类型
1.byType
2.byName

二、@Autowired和@Resource的区别
1.@Autowired
a.此注解属于spring实现的注解。
b.默认装配类型为byType。如果同时存在多个同一类型的bean,且没有指定其中一个,会报异常;如果想指定其中一个,可通过@qualifier("beanName")指定。

2.@Resource
a.此注解属于J2EE标准注解。在(JAVAX包内)
b.默认装配类型为byName。如果名称找不到对应类型,则会按类型匹配。但要注意一旦指定了名字,如@Resource(name="beanName"),如果找不到指定的bean,则会抛异常,不会再按类型匹配。

三、自动扫描组件机制
spring配置文件中加入如下配置
<context:component-scan base-package="com.pkg" />
,spring会自动在类路径下寻找标有@Compoent,@Controller,@Service,@Repository等注解的类,并将这些类纳入spring的窗口管理,这四个注解实现上没有太大区别,估计是以后扩展用吧,可以用来指定不同的层所对应的类,它们有一个共同的属性value,可以用来指定bean的名称,如果没有指定,则会以类名第一个字母小写作为名称。

四、hibernate在spring中的几个实现类,具体用法可参考文档。
1.HibernateTemplate.下文摘自spring文档
HibernateTemplate will ensure that Session instances are properly opened and closed, and automatically participate in transactions. The template instances are thread-safe and reusable, they can thus be kept as instance variables of the surrounding class.

2.继承HibernateDaoSupport
Spring provides a convenient HibernateDaoSupport base class that provides a setSessionFactory(..) method for receiving a SessionFactory, and getSessionFactory() and getHibernateTemplate()for use by subclasses.

两者比较 :
HibernateDaoSupport allows any checked application exception to be thrown within the data access code; but HibernateTemplate class throwing only unchecked exceptions within the callback.


五、AOP in spring
代理方式默认为JDK动态代理,也可以是CGLIB代理。关于AOP的其它概念可参考spring文档,文档里对pointcut和advice的类型都进行了详细的说明,此处只提取下面的例子
package x.y;
import org.aspectj.lang.ProceedingJoinPoint;
import org.springframework.util.StopWatch;

public class SimpleProfiler {

public Object profile(ProceedingJoinPoint call, String name, int age) throws Throwable {
StopWatch clock = new StopWatch(
"Profiling for '" + name + "' and '" + age + "'");
try {
clock.start(call.toShortString());
return call.proceed();
} finally {
clock.stop();
System.out.println(clock.prettyPrint());
}
}
}


<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">

<!-- this is the object that will be proxied by Spring's AOP infrastructure -->
<bean id="fooService" class="x.y.service.DefaultFooService"/>

<!-- this is the actual advice itself -->
<bean id="profiler" class="x.y.SimpleProfiler"/>

<aop:config>
<!-- 定义切面bean -->
<aop:aspect ref="profiler">
<!-- 定义哪些方法需要进行代理 -->
<aop:pointcut id="theExecutionOfSomeFooServiceMethod"
expression="execution(* x.y.service.FooService.getFoo(String,int))
and args(name, age)"/>
<!-- 代理的方法(profile)以及代理方式(aop:around) -->
<aop:around pointcut-ref="theExecutionOfSomeFooServiceMethod"
method="profile"/>

</aop:aspect>
</aop:config>

</beans>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值