Springboot中@Configuration和@bean注解的作用是什么

本篇文章给大家分享的是有关Springboot中 @Configuration和 @bean注解的作用是什么,小编觉得挺实用的,因此分享给大家学习,希望大家阅读完这篇文章后可以有所收获,话不多说,跟着小编一起来看看吧。

创新互联公司-专业网站定制、快速模板网站建设、高性价比如皋网站开发、企业建站全套包干低至880元,成熟完善的模板库,直接使用。一站式如皋网站制作公司更省心,省钱,快速模板网站建设找我们,业务覆盖如皋地区。费用合理售后完善,10余年实体公司更值得信赖。

@Configuration注解可以达到在Spring中使用xml配置文件的作用

@Bean就等同于xml配置文件中的

在spring项目中我们集成第三方的框架如shiro会在spring.xml配置文件中进行配置,例如:


  
    
    
    
    
    
    
    
      
        /css/**=anon
        /js/**=anon
        /validatecode.jsp*=anon
        /images/**=anon
        /login.jsp=anon
        /service/**=anon
        /**=authc
      
    
  
  
  
     
     
     
  

  
  
     
    
   

  
  
    
    
  
  
  

在springboot与shiro整合:

@Configuration
public class ShiroConfig {
  @Bean
  public ShiroFilterFactoryBean shirFilter(SecurityManager securityManager) {
    ShiroFilterFactoryBean shiroFilterFactoryBean = new ShiroFilterFactoryBean();
    shiroFilterFactoryBean.setSecurityManager(securityManager);

    Map filterChainDefinitionMap = new HashMap();
    shiroFilterFactoryBean.setLoginUrl("/login");
    shiroFilterFactoryBean.setUnauthorizedUrl("/unauthc");
    shiroFilterFactoryBean.setSuccessUrl("/home/index");
    
    filterChainDefinitionMap.put("/*", "anon");
    filterChainDefinitionMap.put("/authc/index", "authc");
    return shiroFilterFactoryBean;
  }

  @Bean
  public HashedCredentialsMatcher hashedCredentialsMatcher() {
    HashedCredentialsMatcher hashedCredentialsMatcher = new HashedCredentialsMatcher();
    hashedCredentialsMatcher.setHashAlgorithmName(PasswordHelper.ALGORITHM_NAME); 
    hashedCredentialsMatcher.setHashIterations(PasswordHelper.HASH_ITERATIONS); 
    return hashedCredentialsMatcher;
  }

  @Bean
  public EnceladusShiroRealm shiroRealm() {
    EnceladusShiroRealm shiroRealm = new EnceladusShiroRealm();
    shiroRealm.setCredentialsMatcher(hashedCredentialsMatcher()); 
    return shiroRealm;
  }

  @Bean
  public SecurityManager securityManager() {
    DefaultWebSecurityManager securityManager = new DefaultWebSecurityManager();
    securityManager.setRealm(shiroRealm());
    return securityManager;
  }

  @Bean
  public PasswordHelper passwordHelper() {
    return new PasswordHelper();
  }
}

@Configuration注解可以达到在Spring中使用xml配置文件的作用。

@Bean就等同于xml配置文件中的

以上就是Springboot中 @Configuration和 @bean注解的作用是什么,小编相信有部分知识点可能是我们日常工作会见到或用到的。希望你能通过这篇文章学到更多知识。更多详情敬请关注创新互联行业资讯频道。


新闻名称:Springboot中@Configuration和@bean注解的作用是什么
链接URL:http://pwwzsj.com/article/gosjsh.html