zoukankan      html  css  js  c++  java
  • 4.9 Annotationbased container configuration

    注解配置XML配置之前执行,所以后者会覆盖前者。

                          -----Spring Docs

    4.9.1 @Required

    @Required应用于setter方法,例如:

     1 public class SimpleMovieLister {
     2 
     3   private MovieFinder movieFinder;
     4 
     5   @Required
     6   public void setMovieFinder(MovieFinder movieFinder) {
     7       this.movieFinder = movieFinder;
     8   }
     9 
    10   // ...
    11 }

    @Required只是表明属性必须被设置,否则报异常NullPointerException。

    4.9.2 @Autowired

    通过 @Autowired的使用来消除 set get方法。

    @Autowired 将分别寻找和它们类型匹配的 Bean,将它们作为 Method(Car car ,Office office) 的入参来创建 Bean。 

    4.9.5 @Resource

    1 public class SimpleMovieLister {
    2 
    3   private MovieFinder movieFinder;
    4 
    5   @Resource(name="myMovieFinder")
    6   public void setMovieFinder(MovieFinder movieFinder) {
    7       this.movieFinder = movieFinder;
    8   }
    9 }

    如果没有设置name,默认为the field name or setter method

    1 public class SimpleMovieLister {
    2 
    3   private MovieFinder movieFinder;
    4 //name "movieFinder" injected
    5   @Resource
    6   public void setMovieFinder(MovieFinder movieFinder) {
    7       this.movieFinder = movieFinder;
    8   }
    9 }
    public class MovieRecommender {
    //looks for a bean named customerPreferenceDao
      @Resource
      private CustomerPreferenceDao customerPreferenceDao;
    //
      @Resource
      private ApplicationContext context;
    
      public MovieRecommender() {
      }
    
      // ...
    }

    4.9.6 @PostConstruct and @PreDestroy

     1 public class CachingMovieLister {
     2 
     3   @PostConstruct
     4   public void populateMovieCache() {
     5       // populates the movie cache upon initialization...
     6   }
     7 
     8   @PreDestroy
     9   public void clearMovieCache() {
    10       // clears the movie cache upon destruction...
    11   }
    12 }

    4.12 Java-based container configuration(基于java的Spring容器配置)

    4.12.1 Basic concepts: @Configuration and @Bean

    在别标记为@Configuration的类中配置容器。

    the @Bean annotation plays the same role as the <bean/> element.

     

     

  • 相关阅读:
    Lc617_合并二叉树
    Lc257_二叉树的所有路径
    Lc222_完全二叉树的节点个数
    记github下载上传遇到的各种问题
    Lc101_对称二叉树
    Lc222_翻转二叉树
    二叉树的dfs 与 bfs (递归遍历)
    全球最火的程序员学习路线!没有之一!3天就在Github收获了接近1w点赞
    大二逃课总结的1.2w字的计算机网络知识!扫盲!
    「IDEA插件精选」安利一个IDEA骚操作:一键生成方法的序列图
  • 原文地址:https://www.cnblogs.com/chuanheng/p/2762997.html
Copyright © 2011-2022 走看看