public void create(int i){} |
@Test, @Test1 |
- @Test
- public void create(int i){}
- public @Test void create(int i){}
- @Test public void create(int i){}
- //注意不能放在void的后面
- public void create(@Test int i){}
- @Test@Test1
- public void create(int i){}
- //注解以@分隔,与空白符包括行(row) 无关
- import java.lang.annotation.*;
- @Target(ElementType.METHOD)//注解只能用于方法
- @Retention(RetentionPolicy.RUNTIME)//注解在运行期也保留,可以通过反射读取注解信息
- public @interface Scholar {
- public int id();
- public String descrition() default "no description";
- }
- //@Scholar(id=1)//Scholar只能用于方法
- public class TestScholar {
- @Scholar(id=1)
- public void test() {};
- }
表示注解可用于什么地方 | ||
CONSTRUCTOR | 构造器的声明 | |
FIELD | 域声明(包括enum实例) | |
LOCAL_VARIABLE | 局部变量声明 | |
METHOD | 方法声明 | |
PACKAGE | 包声明 | |
PARAMETER | 参数声明 | |
TYPE | 类,接口(包括注解)或者enum声明 | |
SOURCE | 注解将被编译器丢弃 | |
CLASS | 注解在class文件中可用,会被VM丢弃 | |
RUNTIME | VM在运行期被保留,可以通过反射机制读取注解 | |
所有基本类型(int,float,boolean等) String Class enum Annotation 以上类型的数组 |
- for(Method m : of.getDeclaredMethods())
- Scholar scholar = m.getAnnotation(Scholar.class)
- <?xml version="1.0" encoding="UTF-8"?>
- <beans xmlns="http://www.springframework.org/schema/beans"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xmlns:context="http://www.springframework.org/schema/context"
- xsi:schemaLocation="http://www.springframework.org/schema/beans
- http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
- http://www.springframework.org/schema/context
- http://www.springframework.org/schema/context/spring-context-2.5.xsd">
- <context:annotation-config/>
- <!--注意:xmlns:context="http://www.springframework.org/schema/context" -->
- <!--(隐式注册 post-processors 包括了
- AutowiredAnnotationBeanPostProcessor,
- CommonAnnotationBeanPostProcessor,
- PersistenceAnnotationBeanPostProcessor,也包括了前面提到的 RequiredAnnotationBeanPostProcessor。) -->
- </beans>
- public class SimpleMovieLister {
- private MovieFinder movieFinder;
- @Autowired
- public void setMovieFinder(MovieFinder movieFinder) {
- this.movieFinder = movieFinder;
- }
- // ...
- }
- public class MovieRecommender {
- private MovieCatalog movieCatalog;
- private CustomerPreferenceDao customerPreferenceDao;
- @Autowired
- public void prepare(MovieCatalog movieCatalog,
- CustomerPreferenceDao customerPreferenceDao) {
- this.movieCatalog = movieCatalog;
- this.customerPreferenceDao = customerPreferenceDao;
- }
- // ...
- }
- public class MovieRecommender {
- @Autowired
- private MovieCatalog movieCatalog;
- private CustomerPreferenceDao customerPreferenceDao;
- @Autowired
- public MovieRecommender(CustomerPreferenceDao customerPreferenceDao) {
- this.customerPreferenceDao = customerPreferenceDao;
- }
- // ...
- }
- public class MovieRecommender {
- @Autowired
- private MovieCatalog[] movieCatalogs;
- // ...
- }
- public class MovieRecommender {
- private Set<MovieCatalog> movieCatalogs;
- @Autowired
- public void setMovieCatalogs(Set<MovieCatalog> movieCatalogs) {
- this.movieCatalogs = movieCatalogs;
- }
- // ...
- }
- public class SimpleMovieLister {
- private MovieFinder movieFinder;
- @Autowired(required = false)
- public void setMovieFinder(MovieFinder movieFinder) {
- this.movieFinder = movieFinder;
- }
- // ...
- }
- public class MovieRecommender {
- @Autowired
- @Qualifier("mainCatalog")
- private MovieCatalog movieCatalog;
- // ...
- }
- public class MovieRecommender {
- private MovieCatalog movieCatalog;
- private CustomerPreferenceDao customerPreferenceDao;
- @Autowired
- public void prepare(@Qualifier("mainCatalog") MovieCatalog movieCatalog,
- CustomerPreferenceDao customerPreferenceDao) {
- this.movieCatalog = movieCatalog;
- this.customerPreferenceDao = customerPreferenceDao;
- }
- // ...
- }
- <!--package是要扫描的包-->
- <context:component-scan base-package="org.springframework.samples.petclinic.web"/>
- @Controller
- @RequestMapping("/editPet.do")
- @SessionAttributes("pet")
- public class EditPetForm {
- private final Clinic clinic;
- @Autowired
- public EditPetForm(Clinic clinic) {
- this.clinic = clinic;
- }
- @ModelAttribute("types")
- public Collection<PetType> populatePetTypes() {
- return this.clinic.getPetTypes();
- }
- @RequestMapping(method = RequestMethod.GET)
- public String setupForm(@RequestParam("petId") int petId, ModelMap model) {
- Pet pet = this.clinic.loadPet(petId);
- model.addAttribute("pet", pet);
- return "petForm";
- }
- @RequestMapping(method = RequestMethod.POST)
- public String processSubmit(@ModelAttribute("pet") Pet pet,
- BindingResult result, SessionStatus status) {
- new PetValidator().validate(pet, result);
- if (result.hasErrors()) {
- return "petForm";
- } else {
- this.clinic.storePet(pet);
- status.setComplete();
- return "redirect:owner.do?ownerId=" + pet.getOwner().getId();
- }
- }
- }