Spring学习之-各注解的含义总结

注解配置

  • @ComponentScan("spittr.web"):/在加载Spring上下文时,会扫描spittr.web包查找组件

  1. @ComponentScan注解扫描的组件有@Component、@Controller、@Repository注解的类,并为其创建bean。如果@ComponentScan没有带参数,默认会扫描配置类相同的的包以及子包。
  2. @ComponentScan注解同等于XMLSpring context命名空间的<context:component-scan base-package="spring_chapter_02.writingbean">
  3. 设置@ComponentScan注解的属性值,可以指定扫描的包:@ComponentScan注解的value属性@ComponentScan(basePackages={"package1","package2",..."packageN"})
  4. 还可以指定包中所含的类或接口,如果代码重构导致基础包发生变化,这样指定是相对比较安全的@ComponentScan(basePackageClasses={SgtPeppers.class,......,n})。
  • @Component:注解在类上,表明该类会作为组建类,并告诉Spring要为这个类创建bean
  1. 如果@Componet注解没有带参数,创建的bean为类名第一字母小写,如果想指定自己特定的bean ID可以带上参数@Componet(“bean-name”)。
  • @RequestMapping({"/","/homepage"}):将方法映射到对”/”和” /homepage”的GET或者POST请求url
  1. 服务器上运行网址: http://localhost:8080/Spring_shizhan4ban_Chapter05/和http://localhost:8080/Spring_shizhan4ban_Chapter05/homepage/
  2. @RequestMapping注解可以用在方法或者类级别
  • @Controller:注解在类上,声明为一个控制器
  • @Configuration:注解在类上,表明这是一个配置类,该类应该包含在Spring应用上下文中如何创建bean的细节。一般搭配@Bean注解一起使用,同等于XML中bean的装配。
  • @ContextConfiguration:告诉Spring容器需要在指定的配置类中加载配置

  1. @ContextConfiguration (classes=JavaConfig.class)指定配置类加载配置,该配置类可以使用@ComponentScan(basePackageClasses={SgtPeppers.class})注解扫描组件来创建上下文bean。
  2. @ContextConfiguration (classes=JavaConfig.class)指定配置类加载配置,该配置类可以使用组件扫描@Configuration注解结合JavaConfig显示配置@Bean注解来创建Java bena。
  • @ImportResource和@Import注解:这两种可以一起使用来用来创建Spring上下bean

  1. @ImportResource("classpath:Spring_XMLconfigure/*.xml"):导入xml配置来加载Spring上下文bean
  2. @Import(CDPlayerConfig.class):导入JavaConfig配置来创建Spring上下文中的bean
  • @Bean:该注解使用在方法级别上,这个方法会创建所需类型的实例,@Bean注解会告诉Spring容器,这个方法返回的对象注册为Spring上下文中的bean
  1. @Bean注解默认情况下,bean的ID与@Bean注解的方法名一致;@Bean(name=“bean-name”)可以指定特定的beanID
  • @Autowired:该注解实现制动装配,就是让Spring自动满足bean依赖的一种方法,在满足依赖的过程中,会在Spring应用上下文中寻找匹配某个bean需求的其他bean。
  1. @Autowired注解可以用在构造器上(构造函数的参数为依赖注入的对象),就是构造器自动注入
  2. @Autowired注解可以用在Setter方法上(Setter函数的参数为依赖注入的对象),属性的setter方法注入
  3. 不管是构造器、Setter方法还是其他方法,Spring都会尝试满足方法参数上所声明的依赖,假如有且只有一个bean匹配依赖需求的话,那么这个bean将会被装备进来。如果没有匹配的bean,Spring会抛出一个异常,你可以将@Autowired注解的required属性设置为false->@Autowired(required=false)
  4. 如果有多个bean都满足依赖关系,Spring将会抛出一个异常表明没有明确指定选择哪个bean来装配,这种情况小可以使用@Qualifier注解消除二义性,请关注@Qualifier的用法。
  • @Qualifier:
  1. @Qualifier("XXX") 中的 XX是 Bean 的名称,所以 @Autowired 和 @Qualifier 结合使用时,自动注入的策略就从 byType 转变成 byName 了,@Autowired 可以对成员变量、方法以及构造函数进行注释,而 @Qualifier 的标注对象是成员变量、方法入参、构造函数入参。
  • @Cacheable、@CacheEvict注解:
  1. @Cacheable注解可以用在方法或者类级别。当他应用于方法级别的时候,就是如上所说的缓存返回值了;当应用在类级别的时候,这个类的所有方法的返回值都将被缓存。
  • @Valid:
  1. @Valid注解的作用是:在@RequestMapping这个注解所在的方法上使用@Valid注解进行数据的校验,结合@InitBinder注解一起使用。

    第一步:在控制层(@Controller)中在@RequestMapping注解的方法中的参数进行@Valid注解(例如:public String singleFileUpload(@Valid FileBucket fileBucket,BindingResult result, ModelMap model) throws IOException)

    第二步:写一个MyValidator校验类实现Validator接口中的supports和validate方法(public class myValidator implements Validator{})

    第三步:在控制层(@Controller)中对MyValidator myValidator进行@InitBinder(myValidator)进行绑定

创建bean的方式

  • 自动装配方式;组件扫描和自动注入

  1. 组件扫描创建bean:一般都要用到@ComponentScan注解来扫描需要加载的组件类
  2. 显示装配方式;Java和XML配置:@bean是Java显示配置来创建bean,和@Configuration注解搭配使用,表明这是一个配置类。

Java配置装配Spring替代web.xml配置文件

  • 使用Java将DispatcherServlet装配Sverle容器中,来代替web.xml文件配置。容器会在类路径中查找实现
  • 在Servlet3.0环境中,容器会在类路径查找实现了
  1. javax.servlet.ServletContainerInitializer虚拟接口的类
  2. 而spring提供了这样一个接口的实现了:SpringServletContainerInitializer->public class SpringServletContainerInitializer implements ServletContainerInitializer。
  3. SpringServletContainerInitializer类反过来又会查找实现了WebApplicationInitializer的类并将配置任务交给他们来完成
  4. AbstractContextLoaderInitializer虚拟类实现了WebApplicationInitializer接口->Implements  org.springframework.web.WebApplicationInitializer。
  5. AbstractDispatcherServletInitialize类继承了AbstractContextLoaderInitializer,而AbstractAnnotationConfigDispatcherServletInitializer类继承了AbstractDispatcherServletInitialize类。
  6. 最终只要实现AbstractAnnotationConfigDispatcherServletInitializer类的方法即可
  7. 在应用程序中,程序员自己定义一个类继承AbstractAnnotationConfigDispatcherServletInitializer,重新父类的方法来实现DispatcherServlet在容器的加载工作。所以我们只要关注AbstractAnnotationConfigDispatcherServletInitializer类即可
  8. 总结如下:

    MY_DispatcherServletInitializer(自己定义的类,最终的配置由该类来完成,该类的主要工作是重新父类的方法)

    --extends->AbstractAnnotationConfigDispatcherServletInitializer

    --extends->AbstractDispatcherServletInitialize

    --extends->AbstractContextLoaderInitializer

    --Implements->WebApplicationInitializer(将配置任务交给该类)

    <--Implements--SpringServletContainerInitializer(查找实现了该类的实例)

    --Implements-->ServletContainerInitializer(Servlet3.0环境中,容器会在类路径查找实现了该类的实例)

   9.MY_DispatcherServletInitializer(自己定义的类,最终的配置由该类来完成,该类的主要工作是重新父类的方法)

    9.1:重写AbstractAnnotationConfigDispatcherServletInitializer方法解析:

    第一方法getServletMappings():它会将一个或多个路径映射到DispatcherServlet

protected String[] getServletMappings() {return new String[] { "/" };}

“/”表示它会是应用的默认Servlet,它会处理进入引用的所有请求。

protected String[] getServletMappings() {return new String[] { "/homepage" };}

“/homepage”表示指定了只有应用下的homepage的请求方法才能进人该应用

第二方法getRootConfigClasses():方法返回的带有@Configuration注解的类将会用来装配ContextLoaderListener创建的应用上下文中的bean

    第三方法getServletConfigClasses()方法返回的带有@Configuration注解的类将会用来定义DispatcherServlet应用上下文中的bean

XML配置转配Spring

中央库pom.xml的配置

//<artifactId>spring-webmvc</artifactId>

import org.springframework.web.bind.annotation.PathVariable;

import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestMethod;

import org.springframework.web.bind.annotation.RequestParam;

import static org.springframework.web.bind.annotation.RequestMethod.*;

import org.springframework.stereotype.Controller;

import org.springframework.ui.Model;

import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;

//<artifactId>spring-test</artifactId>

import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;

import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;

import static org.springframework.test.web.servlet.setup.MockMvcBuilders.*;

import org.springframework.test.web.servlet.MockMvc;

//<artifactId>mockito-core</artifactId>

import static org.mockito.Mockito.*;

//<artifactId>spring-matchers</artifactId>

import static org.hamcrest.Matchers.*;

//<artifactId>junit</artifactId>

import org.junit.Test;

//<artifactId>commons-lang3</artifactId>

import org.apache.commons.lang3.builder.EqualsBuilder;

import org.apache.commons.lang3.builder.HashCodeBuilder;

//<artifactId>spring-jdbc</artifactId>

import org.springframework.jdbc.core.JdbcOperations;

import org.springframework.jdbc.core.RowMapper;

 

posted on 2017-03-09 09:47  zhabayi  阅读(285)  评论(0编辑  收藏  举报