`

Spring配置Annotation的几点备忘

阅读更多

1、如何注入常量

@Value("${upload.filePath}")

 

2、如何配置Service、controler、DAO

@Controller

@Service("orgService")重命名为orgService否则按照orgServiceImpl命名

@Repository

 

3、如何配置延迟加载

@Lazy(false)
 

4、如何配置bean的作用域

@Scope("singleton")

5、如何配置IBatis2.x版本的SqlMapClientTemplate

原有的SqlMapClientDaoSupport的setSqlMapClient是final方法,所以不能重写并标记为@Autowired

 

6、如何在域的位置注入bean

@Resource(name="beanId")

 

7、如何配置事务

<tx:annotation-driven transaction-manager="transactionManager" proxy-target-class="true"/>     

表明为cglib的代理方式:proxy-target-class="true"

 

8、全局性配置

 

 <context:component-scan base-package="com.resoft.prophet" /> 

 

9、如何将整个类的方法配置为事务包裹的方法

@Transactional

public class A{

}

定义在类上的 @Transactional 注解指定了类中所有方法执行时的默认事务语义。

 

10、设置@Transactional(readOnly = true)有效吗,大多数是有效的,但是据说的确需要数据库的支持,经测试在mysql中没有问题,如果插入数据库,会报错

@Transactional(readOnly = true)
 public void insertDept(Department department) throws EntityExistedException {
  if (departmentDAO.isDeptExisted(department)) {
   throw new EntityExistedException("您输入的部门全称已存在,请更换。");
  }
  departmentDAO.insertDept(department);
 }

 

11、默认是bytype的注入,如何byName注入?

 

12、@Autowired(required=true)在初始化时就校验

 

13、默认的事务annotation配置是?

  • 事务传播设置是 PROPAGATION_REQUIRED

  • 事务隔离级别是 ISOLATION_DEFAULT

  • 事务是 读/写

  • 事务超时默认是依赖于事务系统的,或者事务超时没有被支持。

  • 任何 RuntimeException 将触发事务回滚,但是任何 checked Exception 将不触发事务回滚

  • 14、如何标记多个事务属性???

    @Transactional(readOnly = true)

    @Transactional(readOnly = true, rollbackFor = { BusinessException.class })

     

    15、配置单元测试加载配置文件的注解

    @ContextConfiguration(locations = { "/cache/applicationContext-ehcache.xml" })

     

    分享到:
    评论

    相关推荐

    Global site tag (gtag.js) - Google Analytics