spring常用注解

更新时间:2023-10-11 00:38:01 阅读量: 综合文库 文档下载

说明:文章内容仅供预览,部分内容可能不全。下载后的文档,内容与下面显示的完全一致。下载之前请确认下面内容是否您想要的,是否完整无缺。

Spring4.1.6 常用注解

@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(\ ? 说明

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

@Service

? 例如 @Service

public class SoftCreateServiceImpl implements ISoftCreateService {} ? 或者

@Service(\ ? 说明

@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(\copyright www.itxxz.com public class BbtForumController {

@RequestMapping(params = \public String listBoardTopic(int topicId,User user) {} } ? 方法

@RequestMapping(\

@RequestMapping(value=\@RequestMapping(value

=

\

params

=

{ \ ? 说明

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

? 参数绑定说明

如果我们使用以下的 URL 请求:

http://localhost/itxxzSpring4?method=listBoardTopic&topicId=1&userId=10&userName=tom

topicId URL 参数将绑定到 topicId 入参上,而 userId 和 userName URL 参数将绑定到 user 对象的 userId 和 userName 属性中。和 URL 请求中不允许没有

\,并被保存到model 里@ModelAttribute 声明在方法上,表示该方法的返回值被保存到model 里

@Cacheable 和@CacheFlush

? @Cacheable :声明一个方法的返回值应该被缓 存 例如:@Cacheable(modelId = \

? @CacheFlush :声明一个方法是清空缓存的触发器 例如:@CacheFlush(modelId = \ ? 说明

要配合缓存处理器使用

@Resource

? 例如 @Resource

private DataSource dataSource; // inject the bean named 'dataSource' ? 或者

@Resource(name=\@Resource(type=DataSource.class) ? 说明

@Resource 默认按bean 的name 进行查找,如果没有找到会按type 进行查找, 此时与@Autowired 类 似.

在没有为 @Resource 注解显式指定 name 属性的前提下,如果将其标注在 BeanFactory 类型、ApplicationContext 类型、ResourceLoader 类型、ApplicationEventPublisher 类型、MessageSource 类型上,那么 Spring 会自动注入这些实现类的实例,不需要额外的操作。此时 name 属性不需要指定 ( 或者指定为\,否则注入失败;

@PostConstruct 和@PreDestroy

? @PostConstruct

在方法上加上注解@PostConstruct ,这个方法就会在Bean 初始化之后被Spring 容器执 行

(注:Bean 初始化包括,实例化Bean ,并装配Bean 的属性(依赖注入))。

? @PreDestroy

在方法上加上注解@PreDestroy ,这个方法就会在Bean 被销毁前被Spring 容器执行。

@Repository

? 与@Controller 、@Service 类似,都是向spring 上下文中注册bean ,不在赘述。

@Component (不推荐使用)

@Component 是所有受Spring 管理组件的通用形式,Spring 还提供了更加细化的注解形式: @Repository 、@Service 、@Controller ,它们分别对应存储层Bean ,业务层Bean ,和展示层Bean 。

目前版本(2.5 )中,这些注解与@Component 的语义是一样的,完全通用, 在Spring 以后的版本中可能会给它们追加更多的语义。 所以,我们推荐使用@Repository 、@Service 、@Controller 来替代@Component 。

@Scope

? 例如

@Scope(\@Repository()

public class UserSessionBean implementsSerializable {} ? 说明

在使用XML 定义Bean 时,可以通过bean 的scope 属性来定义一个Bean 的作用范围,

同样可以通过@Scope 注解来完成

@Scope中可以指定如下值:

singleton:定义bean的范围为每个spring容器一个实例(默认值) prototype:定义bean可以被多次实例化(使用一次就创建一次) request:定义bean的范围是http请求(springMVC中有效) session:定义bean的范围是http会话(springMVC中有效) global-session:定义bean的范围是全局http会话(portlet中有效)

@SessionAttributes

? 说明

Spring 允许我们有选择地指定 ModelMap 中的哪些属性需要转存到 session 中, 以便下一个请求属对应的 ModelMap 的属性列表中还能访问到这些属性。

这一功能是通过类定义处标注 @SessionAttributes 注解来实现的。 @SessionAttributes 只能声明在类上,而不能声明在方法上。 ? 例如

@SessionAttributes(\将ModelMap 中属性名为currUser 的属性 @SessionAttributes({\@SessionAttributes(types = User.class)

@SessionAttributes(types = {User.class,Dept.class})

@SessionAttributes(types = {User.class,Dept.class},value={\

@InitBinder

? 说明

如果希望某个属性编辑器仅作用于特定的 Controller ,

可以在 Controller 中定义一个标注 @InitBinder 注解的方法, 可以在该方法中向 Controller 了注册若干个属性编辑器 ? 例如 @InitBinder

public void initBinder(WebDataBinder binder) {

SimpleDateFormat dateFormat = new SimpleDateFormat(\dateFormat.setLenient(false);

本文来源:https://www.bwwdw.com/article/m6sf.html

Top