Springboot中@Configuration和@bean注解怎么用

本篇内容主要讲解“Springboot中@Configuration和@bean注解怎么用”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“Springboot中@Configuration和@bean注解怎么用”吧!

建网站原本是网站策划师、网络程序员、网页设计师等,应用各种网络程序开发技术和网页设计技术配合操作的协同工作。成都创新互联专业提供成都做网站、成都网站设计、成都外贸网站建设,网页设计,网站制作(企业站、自适应网站建设、电商门户网站)等服务,从网站深度策划、搜索引擎友好度优化到用户体验的提升,我们力求做到极致!

@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注解怎么用
文章网址:http://bzwzjz.com/article/pgsesj.html

其他资讯

Copyright © 2007-2020 广东宝晨空调科技有限公司 All Rights Reserved 粤ICP备2022107769号
友情链接: 成都网站制作 LED网站设计方案 成都网站建设 营销网站建设 成都网站制作 外贸网站建设 H5网站制作 重庆外贸网站建设 成都网站建设 温江网站设计 高端定制网站设计 成都网站建设公司 定制网站建设多少钱 成都网站设计制作公司 网站建设公司 外贸网站设计方案 成都网站建设公司 成都营销网站制作 网站建设公司 成都网站设计公司 重庆网站建设 网站制作