千家信息网

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

发表于:2025-02-01 作者:千家信息网编辑
千家信息网最后更新 2025年02月01日,本篇内容主要讲解"Springboot中@Configuration和@bean注解怎么用",感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习"Springboo
千家信息网最后更新 2025年02月01日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整合:

@Configurationpublic 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注解怎么用"有了更深的了解,不妨来实际操作一番吧!这里是网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!

0