Annotation Meaning and Application Scenario Summary in SSM Framework

 

First let's talk about what component scanning is:

Specify a package path, and Spring will automatically scan all component classes in the package and its subpackages. When a specific annotation mark is found before the definition of the component class, the component will be included in the Spring container. Equivalent to the <bean> definition function in the original XML configuration.

Component scanning can replace <bean> definitions for a large number of XML configurations.

To use component scanning, you first need to specify the scanning classpath in the XML configuration

<context:component-scan

      base-package = "org.example"/>

The official website is configured above: www.fhadmin.org. When the container is instantiated, it will automatically scan all component classes under the org.example package and its subpackages.

After specifying the scanning classpath, not all component classes in this path are scanned to the Spring container. Only when the component class definition is preceded by the following annotation marks, the Spring container will be scanned.

@Component: General annotation; @Name: General annotation; @Repository: Persistence layer component annotation

@Service: business layer component annotation; @Controller: control layer component annotation

When a component is detected during the scanning process, a default id value is generated, and the default id is the class name starting with lowercase. It is also possible to customize the id in the annotation tag. The following two component id names are oracleUserDao and loginService respectively

@Repository

public class OracleUserDao implements UserDao{

  //.....

}

@Service("loginService")

public class UserService{

  //....

}

Official website: www.fhadmin.org Components usually managed by Spring, the default scope is "singleton". If you need other scopes, you can use the @Scope annotation, as long as you provide the name of the scope in the annotation.

@Scope("prototype")

@Repository

public class OracleUserDao implements EmpDao{

  //...

}

@PostConstruct和@PreDestroy注解标记分别用于指定初始化和销毁回调方法,使用示例:

public class ExampleBean{

  @PostConstruct

  public void init(){

    //初始化回调方法

  }

  @PreDestroy

  public void destroy(){

  //销毁回调方法

  }

}

具有依赖关系的Bean对象,利用下面任何一种注解都可以实现关系注入:

@Resource

@Autowired/@Qualifier

@Inject/@Named

 

@Resource注解标记可以用在字段定义或setter方法定义前面,默认首先按名称匹配注入,然后类型匹配注入:

public class UserService{

  //@Resource

  private UserDao userDao;

  @Resource

  public void setUserDao(UserDao dao){

    this.userDao = dao;

  }

}

当遇到多个匹配Bean时注入会发生错误,官网:www.fhadmin.org 可以显示指定名称,例如@Resource(name = "empDao1")

@Autowired注解标记也可以用在字段定义或setter方法定义前面,默认按类型匹配注入:

public class UserService{

  //@Autowired

  private UserDao userDao;

  @Autowired

  public void setUserDao(UserDao dao){

    this.userDao = dao;

  }

}

@Autowired当遇到多个匹配Bean时注入会发生错误,可以使用下面方法指定名称:

public class UserService{

  //@Autowired

  //@Qualifiter("mysqlUserDao")

  private UserDao userDao;

  @Autowired

  public void setUserDao(@Qualifiter("mysqlUserDao")  userDao dao){

    this.userDao = dao;

  }

}

@Inject注解标记是Spring3.0开始增添的对JSR-330标准的支持,使用前需要添加JSR-330的jar包,使用方法与@Autowired相似,具体如:

public class UserService{

  //@Inject

  private UserDao  userDao;

  @Inject

  public void setUserDao(UserDao dao){

    this.userDao = dao;

  }

}

@Inject当遇到多个匹配Bean时注入会发生错误,可以使用@Named指定名称限定,使用方法为:

public class UserService{

  private UserDao userDao;

  @Inject

  public void setUserDao(@Named("mysqlUserDao")  UserDao dao){

    this.userDao = dao;

  }

}

 

@Value注解可以注入Spring表达式值,使用方法:

首先在XML配置中指定要注入的properties文件

<util:properties id = "jdbcProps"  location = "classpath:db.properties"/>

然后在setter方法前使用@Value注解

public class JDBCDataSource{

  @Value("#{jdbcProps.url}")

  private String url;

  @Value("#{jdbcProps.driver}")

  public void setUrl(String driver){

    try{Class.forName(driver)}catch(.....)...

  }

}

 

RequestMapping注解应用

@RequestMapping可以用在类定义和方法定义上

@RequestMapping标明这个类或者方法与哪一个客户请求对应

@RequestMapping可以定义在Controller类前和处理方法前,主要用于指定Controller的方法处理哪些请求

@Controller

@RequestMapping("/demo01")

public class Controller{

  @RequestMapping("/hello")

  public String execute()throws Exception{

    return "hello";

  }

}

开启@RequestMapping注解映射,官网:www.fhadmin.org需要在Spring的XML配置文件中定义RequestMappingHandlerMapping(类定义前)和RequestMappingHandlerAdapter(方法定义前)两个bean组件

示例:

<bean 

class ="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"/>

<bean

class = "org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"/>

从Spring3.2版本开始可以使用下面XML配置简化RequestMappingHandlerMapping和RequestMappingHandlerAdapter定义:

<mvc:annotation-driven/>

 

Controller注解应用

推荐使用@Controller注解声明Controller组件,这样可以使得Controller定义更加灵活,可以不用实现Controller接口,请求处理的方法也可以灵活定义

@Controller

@RequestMapping("/demo01")

public classs Controller{

 @RequestMapping("/hello")

  public String execute()throws Exception{

    return "hello";

  }

}

为了使@Controller注解生效,需要在Spring的XML配置文件中开启组件扫描定义,并指定Controller组件所在包

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

 

接受请求参数值

Spring MVC Web请求提交数据到控制器有下面几种方法:

使用 HttpServletRequest获取

使用@RequestParam注解

使用自动机制封装成Bean对象

 

使用@RequestParam注解:

Spring会自动将表单参数注入到方法参数(名称一致)

使用@RequestParam注解,映射不一致的名称

优点参数类型会自动转换,但可能出现类型转换异常

@RequestMapping("/login-action.form")

public String checkLogin(String name,@RequestParam("pwd")String password,HttpServletRequest req){

  System.out.println(name);

  System.out.println(password);

  User user = userService.login(name,password);

    //处理过程

  return "success";

 

使用@ModelAttribute注解是当Controller组件处理后,需要向JSP传值用此方法。

(引出:向JSP页面传值也可以直接使用HttpServletRequest和Session;ModeAndView对象;ModelMap参数对象等)

示例:

在Controller方法的参数部分或Bean属性方法上使用。

@RequestMapping("login-action.from")

public String checkLogin(@ModelAttribute("user")User user){

  //处理过程

  return "success";

}

@ModelAttribute("name")

public String getName(){

  return name;

}

(@ModelAttribute数据会利用HttpServletRequest的Attribute传递到JSP页面中)

@ExceptionHandler注解

@ExceptionHandler注解提供了Spring MVC处理异常的方法

(引出:Spring MVC处理异常的方法有三种:使用Spring MVC提供的简单异常处理器(SimpleMappingExceptionResolver);实现HandlerExceptionResolver接口自定义异常处理器;使用 @ExceptionHandler注解实现异常处理)

@ExceptionHandler注解使用方法:

public class BaseController{

  @ExceptionHandler

  public String execute(HttpServletRequest req,Exception e){

    req.setAttribute("e",e);

    //可根据异常类型不同返回不同视图名

    return "error";

  }

}

(适合局部处理有“处理过程”的异常)

然后其他的Controller继承BaseController类即可

 

@ResponseBody注解(在Spring MVC中,此注解与JSON响应相关)

在控制器方法上使用 @ResponseBody 注解, 就可以自动的将控制器的返回值(JavaBean)转换为JSON发送到客户端.

@ResponseBody注解主要用于Controller组件的处理方法前,使用方法为:

引入jackson开发包,后面示例用的是jackson-annotations-2.4.1.jar,jackson-core-2.4.1.jar,jackson-databind-2.4.1.jar

在Spring配置文件中定义<mvc:annotation-driven/>,开启对@ResponseBody应用的支持

在Controller处理方法前定义@ResponseBody注解

示例:

使用@ResponseBody,返回单个值:

@RequestMapping("/test1")

@ResponseBody

public boolean f1(){
  //业务处理代码

  return true;

}

使用@ResponseBody返回多个值:

@RequestMapping("/test2")

@ResponseBody

public Map<String,Object>f2(){

  Map<String,Object> m = new HashMap<String,Object>)();

  m.put("id",1);

  m.put("name","Tom");

  return m;

}

使用@ResponseBody返回List结果:

@RequestMapping("/test3")

@ResponseBody

public List<String>f3(){
  List<String> list = new ArrayList<String>();

  list.add("java");

  list.add("php");

  list.add("ui");

  return list;

}

使用@ResponseBody返回对象结果:

@RequestMapping("/test4")

@ResponseBody

public Object f4(){

  Object obj = new Object();

  return obj;

}

使用@ResponseBody返回对象集合结果:

@RequestMapping("/test5")

@ResponseBody

public Object f5(){

  Emp e1 = new Emp();

  e1.setId(1);

  e1.setName("张三");

 

  Emp e2 = new Emp();

  e2.setId(2);

  e2.setName("李四");

 

  List<Emp> list = new ArrayList<Emp>();

  list.add(e1);

  ;ist.add(e2);

  return list;

}

 

注解实现AOP

开发步骤:

创建方面组件:

创建一个类,充当方面组件,实现通用业务逻辑。

声明方面组件官网:www.fhadmin.org:

在applicationContext.xml中开启AOP注解扫描:

<aop:aspectj-autoproxy proxy-target-class = "true"/>;

使用@Component注解标识这个类,将其声明为组件。

使用@Aspect注解标识这个类,将其声明为方面组件。

使用方面组件:

在组件的方法上,使用注解将方面组件作用到目标组件的方法上,并设置通知类型以确认方面组件调用的时机。

 

使用前置通知,在方面组件方法上增加注解:

@Before("within(controller..*)")

public void log(){

}

后置通知、最终通知使用方法与前置通知一致,只需要将注解改为@AfterReturning、@After即可。

环绕通知,在方面组件方法上增加注解:

@Around("within(controller..*)")

public Object log(ProceedingJoinPoint p) throws Throwable{

  //此处代码在目标组件前执行

  Object obj = p.proceed();//执行目标组件方法

  //此处代码在目标组件后执行

  return obj;

}

使用异常通知,在方面组件方法上增加注解:

@AfterThrowing(pointcut = "within(controller..*)",throwing = "e")

public void log(Exception e){

}

 

 

@Transactional注解

此注解实现声明式事务,步骤如下:

在applicationContext.xml中声明事务组件官网:www.fhadmin.org,开始事务注解扫描,示例为:

<!-- 声明事务管理组件 -->

<bean id = "txManager"

  class = "org.springframework.jdbc.datasource.DataSourceTransactionManager">

      <property name = "dataSource"  ref = "ds"/>

</bean>

<!-- 开启事务注解扫描 -->

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

transaction-manager指定的事务管理器txManager,需要根据数据库访问技术不同选择不同的实现。例如JDBC、Mybatis技术选用DataSourceTransactionManager,而Hibernate技术则选用HibernateTransactionManager.

 

使用 @Transactional注解声明事务,使用方法为:

 @Transactional

public class DefaultFooService implements FooService{

  // @Transactional

  public void insertFoo(Foo foo){...}

  public void updateFoo(Foo foo){...}

 @Transactional注解标记可以用在类定义和方法定义前,方法的事务设置将优先于类级别注解的事务设置。

 @Transactional注解标记有以下属性,在使用时可以根据需要做特殊设定:

- propagation:设置事务传播

- isolation:设置事务隔离级别

- readOnly:设置为只读,还是可读写

- rollbackFor:设置遇到哪些异常必须回滚

- noRollbackFor:设置遇到哪些异常不回滚

 

@Transactional注解属性默认设置如下:

事务传播设置是:PROPAGATION_REQUIRED

事务隔离级别是:ISOLATION_DEFAULT

事务是读/写

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

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

 

 

@PathVariable应用

@PathVariable作用是将URI请求模板中的变量解析出来,映射到处理方法的参数上,示例为:

@RequestMapping(value = "/emp/{id}",method = requestMethod.GET)

public String execute(@PathVariable("id") int id){

  //查询操作处理

  return " ";

}

上述URI请求模板匹配/emp/1、emp/2等格式请求

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326271578&siteId=291194637