SpringMVC中四个基本注解:
@Component、@Repository @Service、@Controller
看字面含义,很容易却别出其中三个:
@Controller 控制层,就是我们的action层
@Service 业务逻辑层,就是我们的service或者manager层
@Repository 持久层,就是我们常说的DAO层
而@Component (字面意思就是组件),它在你确定不了事哪一个层的时候使用。
示例:
1. 控制层
@Controller // 注释为controller
@RequestMapping("/login")public class LoginAction { @Autowired @Qualifier("userService") //注释指定注入 Bean private IUserService userService;
}
2. 业务逻辑层
@Service("userService")
public class UserServiceImpl implements IUserService {@Autowired
@Qualifier("userDao") private IUserDao userDao;
}
3. 持久层
@Repository("userDao")
public class UserDaoImpl implements IUserDao { private static Logger logger = LoggerFactory.getLogger(UserDaoImpl.class); private DataSource dataSource; private JdbcTemplate template; @Autowired public UserDaoImpl(DataSource dataSource){ this.dataSource= dataSource; template = new JdbcTemplate(this.dataSource); }}