Spring注解的使用

一、目的:
在功能变动很小并且功能相对复杂配置文件繁多的组件中使用,是一个不错的选择
减少繁多的配置文件的编写。

二、使用方法:
1、在spring的配置文件applicationContext.xml文件中配置注解包目录
<context:component-scan base-package="com.xx.xx"/>
注意:使用注解的类一定要在com.xx.xx的包中

2、具体的配置过程
(1)dao类的注解使用 @Repository
例如:

@Repository("BaseDao")
public class BaseDao<T, PK extends Serializable> implements IBaseDao<T, PK> {
    代码略
}

(2)service类的注解使用 @Service
示例代码:
@Service("UService")
public class UService extends BaseServiceImpl implements IUService{
 
 @Autowired
 @Qualifier("BaseDao")
 private IBaseDao baseDao;
     
 public List<User> getAllUser(){
  略...
 }
       
        略...
}
(3)action类的注解使用@Controller 或者 @Component
示例代码:
@SuppressWarnings("serial")
@Component("UserAction")
@Scope("prototype")
public class UserAction extends BaseAction {
 
 @Autowired
 @Qualifier("UService")
 private IUService userService;
 @Override
 public String executeAction() throws AppException, SQLException {
        }
}

猜你喜欢

转载自hahawowo.iteye.com/blog/1173091