(1) 、<context:component-scan base-package="*.*" />
该配置隐式注册了多个对注解进行解析的处理器,如:
AutowiredAnnotationBeanPostProcessor
CommonAnnotationBeanPostProcessor
PersistenceAnnotationBeanPostProcessor
RequiredAnnotationBeanPostProcessor
其实,注解本身做不了任何事情,和XML一样,只起到配置的作用,主要在于背后强大的处理器
其中就包括了<context:annotation-config/>配置项里面的注解所使用的处理器
所以配置了<context:component-scan base-package="">之后,便无需再配置<context:annotation-config>
(2)、<tx:annotation-driven transaction-manager="txManager" />
@Transactional 注解可以被应用于接口定义和接口方法、类定义和类的 public 方法上。
Spring团队的建议是你在具体的类(或类的方法)上使用 @Transactional 注解,而不要使用在类所要实现的任何接口上。
public class ButtonBo extends GlobalBo {
....
@Transactional(value = "txManager", rollbackFor = Exception.class)
public Button findButton(String buttonid) throws BaseException {
return hibernateEntityDao.get(Button.class, buttonid);
}
}
(3)
<bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<!--保证事务管理对象正确引用DataSource对象。如果系统中有两个及以上的数据源,因为只能配一个,请配置有增删改偏操作的那个。 -->
<property name="dataSource"><ref bean="dataSource" /></property>
<!--使用容器数据源时,此值为2。如果不设为2,在高并发下有可能有问题。 -->
<property name="transactionSynchronization"><value>2</value></property>
</bean>
(4)、@Component、@Repository、@Service、@Controller、@Autowired、@Resource
而Spring2.5就为我们引入了组件自动扫描机制
它可以在classpath下寻找标注了@Service、@Repository、@Controller、@Component注解的类
并把这些类纳入Spring容器中管理,它的作用和在XML中使用bean节点配置组件是一样的
使用自动扫描机制,则需配置<context:component-scan base-package="com.jadyer"/>启动自动扫描
其中base-package指定需要扫描的包,它会扫描指定包中的类和子包里面类
@Service用于标注业务层组件
@Repository用于标注数据访问组件,即DAO组件
@Controller用于标注控制层组件,如Struts中的Action
@Component泛指组件,当组件不要好归类时,可以使用这个注解进行标注
1、可以使用诸如@Service("personDao")修改bean名称,而它默认的是将首字母小写的类名作为<bean>名称
2、若要更改<bean>作用域的话,可以使用@Scope("prototype")注解来修改<bean>作用域
一般使用@Resource注解,而不要使用@Autowired注解
因为@Autowired注解是Spring提供的,而@Resource注解是J2EE提供的
在JDK6中就已经包含@Resource注解了,所以它没有跟Spring紧密耦合