Springboot中@Configuration和@Bean注解如何解析其具体作用?
- 内容介绍
- 文章标签
- 相关推荐
本文共计763个文字,预计阅读时间需要4分钟。
这篇文章简要介绍了Spring Boot中的`@Configuration`和`@Bean`注解的作用。通过示例代码展示了它们的基本用法,对于想要深入学习或工作的朋友来说,具有一定的参考价值。需要的朋友可以参考以下内容:
`@Configuration`注解用于标记一个类为配置类,Spring会自动扫描并加载这个类中的`@Bean`注解定义的Bean。
`@Bean`注解用于在配置类中定义Bean,相当于XML配置中的``标签。
以下是一个简单的示例:
java@Configurationpublic class AppConfig {
@Bean public HelloService helloService() { return new HelloService(); }}
在这个示例中,`AppConfig`类被标记为配置类,`helloService`方法被标记为返回一个`HelloService`类型的Bean。
通过以上内容,您可以对`@Configuration`和`@Bean`注解有一个基本的了解。希望对您的学习或工作有所帮助。
这篇文章主要介绍了springboot @Configuration @bean注解作用解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
@Configuration注解可以达到在Spring中使用xml配置文件的作用
@Bean就等同于xml配置文件中的<bean>
在spring项目中我们集成第三方的框架如shiro会在spring.xml配置文件中进行配置,例如:
<!-- 配置shiro框架提供过滤器工厂 --> <bean id="shiroFilter" class="org.apache.shiro.spring.web.ShiroFilterFactoryBean"> <!-- 注入shiro核心组件安全管理器 --> <property name="securityManager" ref="securityManager"></property> <!-- 注入相关页面 --> <property name="loginUrl" value="/login.jsp"></property> <property name="unauthorizedUrl" value="/unauthorized.jsp"></property> <!-- 配置过滤器链:配置项目发出url对应拦截规则:指定什么url要求具有什么样权限 --> <property name="filterChainDefinitions"> <value> /css/**=anon /js/**=anon /validatecode.jsp*=anon /images/**=anon /login.jsp=anon /service/**=anon /**=authc </value> </property> </bean> <!-- 配置安全管理器 --> <bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager"> <property name="realms" ref="bosRealm"></property> <!-- 使用缓存 --> <property name="cacheManager" ref="cacheManager"></property> </bean> <!-- 配置缓存管理器--> <bean id="cacheManager" class="org.apache.shiro.cache.ehcache.EhCacheManager"> <!-- 加载ehcache的配置文件,指定缓存策略 --> <property name="cacheManager" ref="ehcacheManager"></property> </bean> <!-- 开启shiro注解支持 --> <bean class="org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator"> <!-- 强制使用cglib代理 --> <property name="proxyTargetClass" value="true"></property> </bean> <!-- 配置切面 目的验权,判断当前用户是否有权限调用service层方法 --> <bean class="org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor"></bean>
在springboot与shiro整合:
@Configuration public class ShiroConfig { @Bean public ShiroFilterFactoryBean shirFilter(SecurityManager securityManager) { ShiroFilterFactoryBean shiroFilterFactoryBean = new ShiroFilterFactoryBean(); shiroFilterFactoryBean.setSecurityManager(securityManager); Map<String, String> filterChainDefinitionMap = new HashMap<String, String>(); 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配置文件中的<bean>
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持易盾网络。
本文共计763个文字,预计阅读时间需要4分钟。
这篇文章简要介绍了Spring Boot中的`@Configuration`和`@Bean`注解的作用。通过示例代码展示了它们的基本用法,对于想要深入学习或工作的朋友来说,具有一定的参考价值。需要的朋友可以参考以下内容:
`@Configuration`注解用于标记一个类为配置类,Spring会自动扫描并加载这个类中的`@Bean`注解定义的Bean。
`@Bean`注解用于在配置类中定义Bean,相当于XML配置中的``标签。
以下是一个简单的示例:
java@Configurationpublic class AppConfig {
@Bean public HelloService helloService() { return new HelloService(); }}
在这个示例中,`AppConfig`类被标记为配置类,`helloService`方法被标记为返回一个`HelloService`类型的Bean。
通过以上内容,您可以对`@Configuration`和`@Bean`注解有一个基本的了解。希望对您的学习或工作有所帮助。
这篇文章主要介绍了springboot @Configuration @bean注解作用解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
@Configuration注解可以达到在Spring中使用xml配置文件的作用
@Bean就等同于xml配置文件中的<bean>
在spring项目中我们集成第三方的框架如shiro会在spring.xml配置文件中进行配置,例如:
<!-- 配置shiro框架提供过滤器工厂 --> <bean id="shiroFilter" class="org.apache.shiro.spring.web.ShiroFilterFactoryBean"> <!-- 注入shiro核心组件安全管理器 --> <property name="securityManager" ref="securityManager"></property> <!-- 注入相关页面 --> <property name="loginUrl" value="/login.jsp"></property> <property name="unauthorizedUrl" value="/unauthorized.jsp"></property> <!-- 配置过滤器链:配置项目发出url对应拦截规则:指定什么url要求具有什么样权限 --> <property name="filterChainDefinitions"> <value> /css/**=anon /js/**=anon /validatecode.jsp*=anon /images/**=anon /login.jsp=anon /service/**=anon /**=authc </value> </property> </bean> <!-- 配置安全管理器 --> <bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager"> <property name="realms" ref="bosRealm"></property> <!-- 使用缓存 --> <property name="cacheManager" ref="cacheManager"></property> </bean> <!-- 配置缓存管理器--> <bean id="cacheManager" class="org.apache.shiro.cache.ehcache.EhCacheManager"> <!-- 加载ehcache的配置文件,指定缓存策略 --> <property name="cacheManager" ref="ehcacheManager"></property> </bean> <!-- 开启shiro注解支持 --> <bean class="org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator"> <!-- 强制使用cglib代理 --> <property name="proxyTargetClass" value="true"></property> </bean> <!-- 配置切面 目的验权,判断当前用户是否有权限调用service层方法 --> <bean class="org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor"></bean>
在springboot与shiro整合:
@Configuration public class ShiroConfig { @Bean public ShiroFilterFactoryBean shirFilter(SecurityManager securityManager) { ShiroFilterFactoryBean shiroFilterFactoryBean = new ShiroFilterFactoryBean(); shiroFilterFactoryBean.setSecurityManager(securityManager); Map<String, String> filterChainDefinitionMap = new HashMap<String, String>(); 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配置文件中的<bean>
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持易盾网络。

