Skip to content

Commit

Permalink
feat: 自动装配类调整
Browse files Browse the repository at this point in the history
  • Loading branch information
fuhouyu committed Oct 28, 2024
1 parent 07824c9 commit d9d436b
Show file tree
Hide file tree
Showing 15 changed files with 121 additions and 91 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@
package com.fuhouyu.framework.cache;

import org.springframework.boot.autoconfigure.cache.CacheProperties;
import org.springframework.boot.context.properties.ConfigurationPropertiesScan;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;

/**
Expand All @@ -29,9 +29,9 @@
* @author fuhouyu
* @since 2024/8/13 21:00
*/
@Configuration
@Import({RedisCacheAutoConfiguration.class, CaffeineCacheAutoconfiguration.class})
@EnableConfigurationProperties(CacheProperties.class)
@ConfigurationPropertiesScan(basePackageClasses = CacheAutoConfiguration.class)
public class CacheAutoConfiguration {


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.autoconfigure.data.redis.RedisAutoConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
Expand All @@ -47,6 +48,7 @@
name = "cache-service-type",
havingValue = "redis")
@AutoConfigureAfter(RedisAutoConfiguration.class)
@Configuration
public class RedisCacheAutoConfiguration {


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils;
Expand All @@ -58,6 +59,7 @@
@ConditionalOnMissingBean(KmsService.class)
@EnableConfigurationProperties(KmsDefaultProperties.class)
@RequiredArgsConstructor
@Configuration
public class DefaultKmsAutoConfiguration {

private final KmsDefaultProperties properties;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

package com.fuhouyu.framework.kms;

import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;

/**
Expand All @@ -27,8 +27,8 @@
* @author fuhouyu
* @since 2024/8/17 17:27
*/
@ComponentScan(basePackageClasses = KmsAutoConfiguration.class)
@Import({DefaultKmsAutoConfiguration.class})
@Configuration
public class KmsAutoConfiguration {


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,20 +19,22 @@
import com.fuhouyu.framework.log.core.LogRecordAspectj;
import com.fuhouyu.framework.log.core.LogRecordStoreService;
import com.fuhouyu.framework.log.properties.LogRecordProperties;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.ConfigurationPropertiesScan;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.context.expression.BeanFactoryResolver;
import org.springframework.util.Assert;

import java.util.ArrayList;
import java.util.List;


/**
* <p>
* 日志记录的自动装配类
Expand All @@ -41,11 +43,12 @@
* @author fuhouyu
* @since 2024/8/14 12:16
*/
@ConfigurationPropertiesScan(basePackages = "com.fuhouyu.framework.log.properties")
@EnableConfigurationProperties(LogRecordProperties.class)
@Configuration(proxyBeanMethods = false)
@ConditionalOnProperty(prefix = LogRecordProperties.LOG_RECORD_PREFIX,
name = "enabled",
havingValue = "true")
@ComponentScan(basePackageClasses = LogRecordAutoConfiguration.class)
@RequiredArgsConstructor
public class LogRecordAutoConfiguration implements InitializingBean {

private final LogRecordProperties logRecordProperties;
Expand All @@ -54,11 +57,6 @@ public class LogRecordAutoConfiguration implements InitializingBean {

private final BeanFactory beanFactory;

public LogRecordAutoConfiguration(LogRecordProperties logRecordProperties, ApplicationContext applicationContext, BeanFactory beanFactory) {
this.logRecordProperties = logRecordProperties;
this.applicationContext = applicationContext;
this.beanFactory = beanFactory;
}

@Override
public void afterPropertiesSet() throws Exception {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,9 @@
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.Objects;

Expand All @@ -46,6 +48,8 @@
@ConditionalOnClass(OSS.class)
@RequiredArgsConstructor
@Slf4j
@EnableConfigurationProperties(AliYunOssProperties.class)
@Configuration
public class AliYunOssAutoConfiguration implements InitializingBean {

private final ResourceProperties resourceProperties;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
import com.fuhouyu.framework.resource.service.impl.LocalFileServiceImpl;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.core.io.Resource;
import org.springframework.context.annotation.Configuration;

/**
* <p>
Expand All @@ -30,6 +30,7 @@
* @author fuhouyu
* @since 2024/8/18 17:28
*/
@Configuration(proxyBeanMethods = false)
public class LocalFileResourceAutoConfiguration {


Expand All @@ -39,7 +40,7 @@ public class LocalFileResourceAutoConfiguration {
* @return bean对象
*/
@Bean
@ConditionalOnMissingBean(Resource.class)
@ConditionalOnMissingBean(ResourceService.class)
public ResourceService localFileResourceService() {
return new LocalFileServiceImpl();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,9 @@

package com.fuhouyu.framework.resource;

import org.springframework.boot.context.properties.ConfigurationPropertiesScan;
import org.springframework.context.annotation.ComponentScan;
import com.fuhouyu.framework.resource.properties.ResourceProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;

/**
Expand All @@ -28,9 +29,9 @@
* @author fuhouyu
* @since 2024/8/16 18:36
*/
@ComponentScan(basePackageClasses = ResourceAutoConfiguration.class)
@Import({AliYunOssAutoConfiguration.class, LocalFileResourceAutoConfiguration.class})
@ConfigurationPropertiesScan(basePackages = "com.fuhouyu.framework.resource.properties")
@EnableConfigurationProperties(ResourceProperties.class)
@Configuration
public class ResourceAutoConfiguration {

}
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@
import com.fuhouyu.framework.resource.exception.ResourceException;
import com.fuhouyu.framework.resource.model.*;
import com.fuhouyu.framework.resource.service.ResourceService;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.TestPropertySource;
import org.springframework.util.Assert;

import java.io.*;
import java.nio.charset.StandardCharsets;
Expand Down Expand Up @@ -59,7 +59,7 @@ class ResourceServiceTest {
void testResourceUpload() throws ResourceException {
PutResourceResult putResourceResult =
resourceService.uploadFile(new PutResourceRequest(BUCKET_NAME, objectKey, FILE_INPUT_STREAM));
Assert.notNull(putResourceResult, "文件上传失败");
Assertions.assertNotNull(putResourceResult, "文件上传失败");
}


Expand All @@ -68,14 +68,14 @@ void testResourceDownload() throws ResourceException {
String filePath = LOCAL_FILE_PARENT + "resource_download.xml";
DownloadResourceResult downloadResourceResult =
resourceService.downloadFile(new DownloadResourceRequest("./", "pom.xml", filePath, 1000));
Assert.notNull(downloadResourceResult, "文件下载失败");
Assertions.assertNotNull(downloadResourceResult, "文件下载失败");
FileUtil.deleteFileIfExists(Path.of(filePath));
}

@Test
void testResourceDelete() throws ResourceException {
resourceService.deleteFile(BUCKET_NAME, objectKey);
Assert.isTrue((!resourceService.doesObjectExist(BUCKET_NAME, objectKey)), "文件未被删除");
Assertions.assertTrue((!resourceService.doesObjectExist(BUCKET_NAME, objectKey)), "文件未被删除");
}

@Test
Expand All @@ -84,7 +84,7 @@ void testGetFile() throws ResourceException {
objectKey);
GetResourceResult getResourceResult = resourceService.getFile(getResourceRequest);
InputStream objectContent = getResourceResult.getObjectContent();
Assert.notNull(objectContent, "资源文件下载失败");
Assertions.assertNotNull(objectContent, "资源文件下载失败");
// 测试下载文件的写入
String localFilePath = LOCAL_FILE_PARENT + "get_file.txt";
try (objectContent;
Expand All @@ -101,12 +101,12 @@ void testGetFile() throws ResourceException {
void testUploadFile() throws ResourceException {
PutResourceRequest putResourceRequest = new PutResourceRequest(BUCKET_NAME, objectKey, FILE_INPUT_STREAM);
PutResourceResult putResourceResult = this.resourceService.uploadFile(putResourceRequest);
Assert.notNull(putResourceResult, "返回结果为空");
Assertions.assertNotNull(putResourceResult, "返回结果为空");

GetResourceRequest getResourceRequest = new GetResourceRequest(BUCKET_NAME, objectKey);
GetResourceResult getResourceResult = resourceService.getFile(getResourceRequest);
ResourceMetadata resourceMetadata = getResourceResult.getResourceMetadata();
Assert.notNull(resourceMetadata, "文件元数据为空");
Assertions.assertNotNull(resourceMetadata, "文件元数据为空");
}


Expand All @@ -117,14 +117,14 @@ void testInitUploadId() throws ResourceException, IOException {
InitiateUploadMultipartRequest initiateUploadMultipartRequest = new InitiateUploadMultipartRequest(BUCKET_NAME,
objectKey);
InitiateUploadMultipartResult initiateUploadMultipartResult = resourceService.initiateMultipartUpload(initiateUploadMultipartRequest);
Assert.notNull(initiateUploadMultipartResult, "初始化上传id 返回的结果为空");
Assertions.assertNotNull(initiateUploadMultipartResult, "初始化上传id 返回的结果为空");

uploadPartFile(localFilePath, initiateUploadMultipartResult.getUploadId());

ListMultipartRequest listMultipartRequest = new ListMultipartRequest(BUCKET_NAME);
listMultipartRequest.setUploadId(initiateUploadMultipartResult.getUploadId());
ListMultipartResult listMultipartResult = resourceService.listParts(listMultipartRequest);
Assert.notNull(listMultipartResult, "列出分片 返回的文件不正确");
Assertions.assertNotNull(listMultipartResult, "列出分片 返回的文件不正确");


UploadCompleteMultipartRequest uploadCompleteMultipartRequest = new UploadCompleteMultipartRequest(BUCKET_NAME);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@
package com.fuhouyu.framework.security;

import com.fuhouyu.framework.security.properties.OpenPlatformAuthProperties;
import org.springframework.boot.context.properties.ConfigurationPropertiesScan;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Configuration;

/**
* <p>
Expand All @@ -27,7 +28,8 @@
* @author fuhouyu
* @since 2024/8/15 16:23
*/
@ConfigurationPropertiesScan(basePackageClasses = OpenPlatformAuthProperties.class)
@Configuration
@EnableConfigurationProperties(OpenPlatformAuthProperties.class)
public class OpenPlatformAutoConfiguration {


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.context.annotation.Primary;
import org.springframework.security.authentication.AuthenticationManager;
Expand All @@ -44,7 +44,7 @@
* @author fuhouyu
* @since 2024/8/15 16:22
*/
@ComponentScan(basePackageClasses = SecurityAutoConfiguration.class)
@Configuration
@Import({OpenPlatformAutoConfiguration.class})
@AutoConfigureAfter(CacheAutoConfiguration.class)
public class SecurityAutoConfiguration {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/*
* Copyright 2024-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.fuhouyu.framework.security;

import com.fuhouyu.framework.security.core.authentication.refreshtoken.RefreshAuthenticationProvider;
import com.fuhouyu.framework.security.token.TokenStore;
import org.springframework.boot.test.context.TestComponent;
import org.springframework.context.annotation.Bean;
import org.springframework.security.authentication.AuthenticationProvider;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.provisioning.InMemoryUserDetailsManager;
import org.springframework.web.client.RestTemplate;

import java.util.Collection;
import java.util.List;

/**
* <p>
*
* </p>
*
* @author fuhouyu
* @since 2024/10/28 21:21
*/
@TestComponent
class BaseComponent {

@Bean
public AuthenticationProvider refreshTokenAuthenticationProvider(TokenStore tokenStore) {
return new RefreshAuthenticationProvider(tokenStore);
}

@Bean
public RestTemplate restTemplate() {
return new RestTemplate();
}

@Bean
public UserDetailsService userDetailsService() {
UserDetails userDetails = new UserDetails() {
@Override
public Collection<? extends GrantedAuthority> getAuthorities() {
return List.of();
}

@Override
public String getPassword() {
return "{noop}admin";
}

@Override
public String getUsername() {
return "admin";
}
};
return new InMemoryUserDetailsManager(userDetails);
}
}
Loading

0 comments on commit d9d436b

Please sign in to comment.