SpringBoot学习(3)-refresh() 敲重点

入口处
((AbstractApplicationContext) applicationContext).refresh();
可以看见这里是执行了 AbstractApplicationContext 的refresh 方法。
本文的 wen环境 ,因此是 ServletWebServerApplicationContext,话不多少,最终执行的还是 父类AbstractApplicationContext的方法

prepareRefresh();

			// Tell the subclass to refresh the internal bean factory.
			//告诉子类刷新内部bean工厂
			//这里没有很明白刷新的意义
			ConfigurableListableBeanFactory beanFactory = obtainFreshBeanFactory();

			// Prepare the bean factory for use in this context.
			//准备bean工厂,以便在此上下文中使用。
			/*这里其他乱七八糟的都没看,但是看见了熟悉的 
			beanFactory.addBeanPostProcessor(new ApplicationContextAwareProcessor(this));
			那大致意思就理解了,给容器一个东西,比如说这里添加的bean的处理器
			*/
			prepareBeanFactory(beanFactory);

			try {
				// Allows post-processing of the bean factory in context subclasses.
				//允许在上下文子类中对bean工厂进行后处理。
				/*
				简单看了下,这里是给不同的环境的上下文提供了添加一些后置处理器的口子,
				这里说的允许 其实就是添加一些后置处理器进去
				既然这里是允许不同容器给自己添加的后置,那自定义的后置处理器是在哪里加入容器的???,难道是通过包扫描进去的?
				*/
				postProcessBeanFactory(beanFactory);

				// Invoke factory processors registered as beans in the context.
				//调用上下文中注册为bean的工厂处理器。
				/**
				BeanFactoryPostProcessors
				这里
				1.将 @Configuration类添加容器
				2.将 @Configuration 的内部配置的@bean加载进容器
				注意这里并没有实例化
				疑问:
					BeanFactoryPostProcessors 是什么时候加到容器里边的
				答:发现 是BeanFactoryPostProcessors这个,那么调试这个类注加入入进容器的时机就可以了
				org.springframework.context.annotation.internalConfigurationAnnotationProcessor
				调试发现,这货实在创建上下文的时候,就注册进去了。
				而且,还发现 以internal 开头的处理器,都是Spring在启动的时候,自己初始化进去的,而且注册的名字还是被改变了
				的,比如 org.springframework.context.annotation.internalConfigurationAnnotationProcessor 
				的实际的类名是ConfigurationClassPostProcessor
				*/
				invokeBeanFactoryPostProcessors(beanFactory);

				// Register bean processors that intercept bean creation.
				//注册拦截bean创建的bean处理器。
				registerBeanPostProcessors(beanFactory);

				// Initialize message source for this context.
				//初始化此上下文的消息源。
				initMessageSource();

				// Initialize event multicaster for this context.
				//初始化此上下文的事件多播程序。
				initApplicationEventMulticaster();

				// Initialize other special beans in specific context subclasses.
				//在特定上下文子类中初始化其他特殊bean
				onRefresh();

				// Check for listener beans and register them.
				//检查侦听器bean并注册它们。
				registerListeners();

				// Instantiate all remaining (non-lazy-init) singletons.
				//实例化所有剩余的(非惰性初始化)单例。
				/*
				*这里实例化的所有的单例的bean 实际上就是在 invokeBeanFactoryPostProcessors 通过 	
				ConfigurationClassPostProcessor 中包扫描 加载进容器的bean
				*/
				finishBeanFactoryInitialization(beanFactory);

				// Last step: publish corresponding event.
				finishRefresh();
			}

理清楚了:
我们通过注解加载进容器中的bean,实际上实在 invokeBeanFactoryPostProcessors 的过程中,通过ConfigurationClassPostProcessor的扫描注册上去,而ConfigurationClassPostProcessor是springBoot在启动中,创建容器的时候就,加载了这个后置处理器,完成包扫描加载任务,之后在finishBeanFactoryInitialization中完成对 所有注册的bean的实例化。

下一次理清楚:
1.BeanFactoryPostProcessors 跟BeanPostProcessors 的区别
2.理清楚自动注入的实现实现过程。
3.以及理解Spring中的钩子函数的种类

Spring Security OAuth2 Authorization Server 是一个基于 Spring Security 的 OAuth2 认证服务器,用于管理 OAuth2 模式下的授权和令牌。 要将 Spring Boot 与 Spring Security OAuth2 Authorization Server 集成,可以遵循以下步骤: 1. 添加依赖 在 pom.xml 文件中添加以下依赖: ```xml <dependency> <groupId>org.springframework.security</groupId> <artifactId>spring-security-oauth2-authorization-server</artifactId> <version>0.2.1</version> </dependency> ``` 2. 配置认证服务器 创建一个配置类,用于配置 OAuth2 认证服务器。这个类需要继承 AuthorizationServerConfigurerAdapter 类,并且实现 configure 方法。 ```java @Configuration public class OAuth2AuthorizationServerConfig extends AuthorizationServerConfigurerAdapter { @Override public void configure(ClientDetailsServiceConfigurer clients) throws Exception { // 配置客户端信息 clients.inMemory() .withClient("client") .secret("{noop}secret") .authorizedGrantTypes("authorization_code", "refresh_token") .redirectUris("http://localhost:8080/client") .scopes("read", "write"); } @Override public void configure(AuthorizationServerSecurityConfigurer security) throws Exception { // 配置安全性 security.checkTokenAccess("isAuthenticated()"); } @Override public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception { // 配置端点 endpoints.authenticationManager(authenticationManager); } } ``` 上面的代码中,我们配置了一个名为 "client" 的客户端,使用了授权码模式和刷新令牌模式。授权成功后,将重定向到 "http://localhost:8080/client" 页面。 3. 配置 Spring Security 为了使 OAuth2 认证服务器正常工作,需要配置 Spring Security。可以创建一个配置类,用于配置 Spring Security。 ```java @Configuration @EnableWebSecurity public class SecurityConfig extends WebSecurityConfigurerAdapter { @Override protected void configure(HttpSecurity http) throws Exception { // 配置 HTTP 安全性 http.authorizeRequests() .antMatchers("/oauth2/authorize").authenticated() .and().formLogin().and().csrf().disable(); } @Override protected void configure(AuthenticationManagerBuilder auth) throws Exception { // 配置身份认证管理器 auth.inMemoryAuthentication() .withUser("user").password("{noop}password").roles("USER"); } } ``` 在上面的代码中,我们配置了 HTTP 安全性和身份认证管理器。只有经过身份认证的用户才能访问 "/oauth2/authorize" 端点。 4. 启动应用程序 现在可以启动应用程序,并访问 "http://localhost:8080/oauth2/authorize?response_type=code&client_id=client&redirect_uri=http://localhost:8080/client" 来进行授权。授权成功后,将重定向到 "http://localhost:8080/client" 页面。 以上就是整合 Spring Boot 和 Spring Security OAuth2 Authorization Server 的基本步骤。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值