ITeye博客

Spring2.5 注解介绍

ceoajun · 2011-08-07 11:08 · spring · 编程语言 · Spring · Markdown

注解介绍

              • @Controller

              • @Service

              • @Autowired

              • @RequestMapping

              • @RequestParam

              • @ModelAttribute

              • @Cacheable

              • @CacheFlush

              • @Resource

              • @PostConstruct

              • @PreDestroy

              • @Repository

              • @Component (不推荐使用)

              • @Scope

              • @SessionAttributes

              • @InitBinder

              • @Required

              • @Qualifier

 

 

@Controller

              • 例如

              @Controller
              public class SoftCreateController extends SimpleBaseController {}

              • 或者
             @Controller("softCreateController")

              • 说明

              @Controller 负责注册一个bean 到spring 上下文中,bean 的ID 默认为类名称开头字母小写

 

 

@Service

              • 例如

              @Service
              public class SoftCreateServiceImpl implements ISoftCreateService {}

              • 或者
              @Service("softCreateServiceImpl")

              • 说明

              @Service 负责注册一个bean 到spring 上下文中,bean 的ID 默认为类名称开头字母小写

 

 

@Autowired

              • 例如

              @Autowired
              private ISoftPMService softPMService;

              • 或者

 

     @Autowired(required=false)
     private ISoftPMService softPMService = new SoftPMServiceImpl();

 

         • 说明

    @Autowired 根据bean 类型从spring 上线文中进行查找,注册类型必须唯一,否则报异常。与@Resource 的区别在于,@Resource 允许通过bean 名称或bean 类型两种方式进行查找@Autowired(required=false) 表示,如果spring 上下文中没有找到该类型的bean 时, 才会使用new SoftPMServiceImpl();

@Autowired 标注作用于 Map 类型时,如果 Map 的 key 为 String 类型,则 Spring 会将容器中所有类型符合 Map 的 value 对应的类型的 Bean 增加进来,用 Bean 的 id 或 name 作为 Map 的 key。

@Autowired 还有一个作用就是,如果将其标注在 BeanFactory 类型、ApplicationContext 类型、ResourceLoader 类型、ApplicationEventPublisher 类型、MessageSource 类型上,那么 Spring 会自动注入这些实现类的实例,不需要额外的操作。

 

@RequestMapping

• 类

@Controller 
@RequestMapping("/bbtForum.do")

public class BbtForumController {
             @RequestMapping(params = "method=listBoardTopic")

public String listBoardTopic(int topicId,User user) {}
}

 

 

  • 方法

              @RequestMapping("/softpg/downSoftPg.do")
              @RequestMapping(value="/softpg/ajaxLoadSoftId.do",method = POST)
              @RequestMapping(value = "/osu/product/detail.do", params = { "modify=false" }, method =POST)

  • 说明
    @RequestMapping 可以声明到类或方法上

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。