189 8069 5689

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://cdxtjz.cn/article/pgsesj.html

其他资讯