天天看點

Springboot注解 @Bean、@Component、 @Service、 @Repository 、@Controller、@Autowired @Qualifier @Primary

@Bean、@Component、 @Service、 @Repository 、 @Controller

@Bean:表示一個方法執行個體化、配置或者初始化一個Spring IoC容器管理的新對象。

@Data
@EnableConfigurationProperties
@Configuration
@ConfigurationProperties(prefix = "redis.cache")
public class RedisCacheConfig {
    @Bean
    public CacheManager cacheManager(RedisConnectionFactory redisConnectionFactory) {
        return new RedisCacheManager(
                RedisCacheWriter.nonLockingRedisCacheWriter(redisConnectionFactory),
                this.getRedisCacheConfigurationWithTtl(600),
                this.getRedisCacheConfigurationMap()
        );
    }
}
           

@Component: 自動被component掃描。 表示被注解的類會自動被component掃描

@Component

public class RedisLimiterUtils {
    @Resource
    RedisTemplate<Object, Object> redisTemplate;
    @Resource(name = "stringRedisTemplate")
    ValueOperations<String, String> ops;
    @Resource(name = "redisTemplate")
    ValueOperations<Object, Object> objOps;

public void saveObject(String key, Object obj, long timeout) {
        redisUtil.set(key, obj, timeout);
    }


    public void saveObject(String key, Object obj) {
        redisUtil.set(key, obj);
    }


    public Object getObject(String key) {
        return redisUtil.get(key);
    }


    public void removeObject(String key) {
        redisUtil.del(key);
    }
}
           

@Repository: 用于持久層,主要是資料庫存儲庫。

@Repository
public interface NginxLogDao extends ElasticsearchRepository<NinxLogDocument, String> {

}
           

@Service: 表示被注解的類是位于業務層的業務component。

@Slf4j
@Service
public class ValidateCodeServiceImpl implements ValidateCodeService {

@Autowired
  private RedisTemplate<String, Object> redisTemplate;

  /**
   * 儲存使用者驗證碼,和randomStr綁定
   * @param deviceId
   *            用戶端生成
   * @param imageCode
   *            驗證碼資訊
   */
  @Override
  public void saveImageCode(String deviceId, String imageCode) {
      String text = imageCode.toLowerCase().toString();
      redisTemplate.execute(new RedisCallback<String>() {
          @Override
          public String doInRedis(RedisConnection connection) throws DataAccessException {
              // redis info
              connection.set(buildKey(deviceId).getBytes(), imageCode.getBytes());
              connection.expire(buildKey(deviceId).getBytes(), 60*5);
              connection.close();
              return "";
          }
      });

  }

  /**
   * 擷取驗證碼
   * @param deviceId
   *            前端唯一辨別/手機号
   */
  @Override
  public String getCode(String deviceId)  {
      String code = "" ;
      try {
          code = redisTemplate.execute(new RedisCallback<String>() {
              @Override
              public String doInRedis(RedisConnection connection) throws DataAccessException {
                  // redis info
                  byte[] temp = "".getBytes();
                  temp = connection.get(buildKey(deviceId).getBytes()) ;
                  connection.close();
                  return new String(temp);
              }
          });
      } catch (Exception e) {
          throw new AuthenticationException("驗證碼不存在"){};
      }
      return code ;
  }

  /**
   * 删除驗證碼
   * @param deviceId
   *            前端唯一辨別/手機号
   */
  @Override
  public void remove(String deviceId) {
      redisTemplate.execute(new RedisCallback<String>() {
          @Override
          public String doInRedis(RedisConnection connection) throws DataAccessException {
              // redis info
              connection.del(buildKey(deviceId).getBytes());
              connection.close();
              return "";
          }
      });
  }

  /**
   * 驗證驗證碼
   */
  @Override
  public void validate(HttpServletRequest request) {
      String deviceId = request.getParameter("deviceId");
      if (StringUtils.isBlank(deviceId)) {
          throw new AuthenticationException("請在請求參數中攜帶deviceId參數"){};
      }
      String code = this.getCode(deviceId);
      String codeInRequest;
      try {
          codeInRequest = ServletRequestUtils.getStringParameter(request, "validCode");
      } catch (ServletRequestBindingException e) {
          throw new AuthenticationException ("擷取驗證碼的值失敗"){};
      }
      if (StringUtils.isBlank(codeInRequest)) {
          throw new AuthenticationException ("請填寫驗證碼"){};
      }

      if (code == null) {
          throw new AuthenticationException ("驗證碼不存在或已過期"){};
      }

      if (!StringUtils.equalsIgnoreCase(code, codeInRequest)) {
          throw new AuthenticationException ("驗證碼不正确"){};
      }
      this.remove(deviceId);
  }

  private String buildKey(String deviceId) {
      return "DEFAULT_CODE_KEY:" + deviceId;
  }
}
           

@Controller:表明被注解的類是控制component,主要用于展現層 。

@Controller
@Api(tags = "REDIS API")
@RequestMapping("/redis")
public class RedisController {
   @Autowired
    private RedisTemplate<String,Object> redisTemplate ;
 @ResponseBody
    @RequestMapping("/memoryInfo")
    @LogAnnotation(module="auth-server",recordRequestParam=false)
    public String getMemoryInfo() {
        Map<String, Object> map = new HashMap<>();
        Object o = redisTemplate.execute(new RedisCallback() {
            @Override
            public Object doInRedis(RedisConnection connection) throws DataAccessException {
                return connection.info("memory").get("used_memory");
            }
        });
        map.put("used_memory", o);
        map.put("create_time", System.currentTimeMillis());
        return JSON.toJSONString(map);
    }
}
           

@Bean與@Component差別

@Component是 spring 2.5引入的,為了擺脫通過classpath掃描根據xml方式定義的bean的方式.

@Bean是spring 3.0 引入的,和 @Configuration一起工作,為了擺脫原先的xml和java config方式。

Spring管理Bean方式有兩種,一種是注冊Bean,一種裝配Bean。

可以通過三種方式實作bean管理,一使用自動配置的方式、二使用JavaConfig的方式、三使用XML配置的方式。

@Component
@Data
public class User{
    private String name = "tom";
}
@Bean 需要在配置類中使用,即類上需要加上@Component或者@Configuration注解, 通常加上@Configuration。 @Bean的用法在這裡。

@Configuration
public class AppConfig {

    @Bean
    public TransferServiceImpl transferService() {
        return new TransferServiceImpl();
    }
}

@Autowired
private TransferService transferService;
           

@Component與@Service差別

目前基本沒有差別。@Service是一種具體的@Component

@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Component
public @interface Service {
   String value() default "";
}
           

@Autowired

自動裝配

@Component("fooFormatter")
    public class FooFormatter implements Formatter {
        public String format() {
            return "foo";
        }
    }

    @Component("barFormatter")
    public class BarFormatter implements Formatter {
        public String format() {
            return "bar";
        }
    }

    @Component
    public class FooService {
        @Autowired
        private Formatter formatter;
        
        //todo 
    }
           

上面的例子會抛出異常NoUniqueBeanDefinitionException,用@Qualifier注解指定

@Component
    public class FooService {
        @Autowired
        @Qualifier("fooFormatter")
        private Formatter formatter;
        //todo 
    }
           

也可以在 Formatter 實作類上使用 @Qualifier 注釋,而不是在 @Component 或者 @Bean 中指定名稱,也能達到相同的效果

@Component
     @Qualifier("fooFormatter")
     public class FooFormatter implements Formatter {
         public String format() {
             return "foo";
         }
     }
 
     @Component
     @Qualifier("barFormatter")
     public class BarFormatter implements Formatter {
         public String format() {
             return "bar";
         }
     }
           

@Primary 指定預設注入的bean消除歧義

@Component
     @Primary
     public class FooFormatter implements Formatter {
         public String format() {
             return "foo";
         }
     }

     @Component
     public class BarFormatter implements Formatter {
         public String format() {
             return "bar";
         }
     }
           

@Compent 作用就相當于 XML配置

@Component
@Data
public class User{
    private String name = "tom";
}
           

備注:

@Resource的作用相當于@Autowired,隻不過@Autowired按byType自動注入,而@Resource預設按 byName自動注入罷了。@Resource有兩個屬性是比較重要的,分是name和type。

Spring将@Resource注解的name屬性解析為bean的名字,而type屬性則解析為bean的類型。是以如果使用name屬性,則使用byName的自動注入政策,而使用type屬性時則使用byType自動注入政策。

如果既不指定name也不指定type屬性,這時将通過反射機制使用byName自動注入政策。

作者:學無止境吧

連結:https://www.jianshu.com/p/e7a62409f682

來源:簡書

著作權歸作者所有。商業轉載請聯系作者獲得授權,非商業轉載請注明出處。