山西中宇建设集团网站,湘潭做网站 搜搜磐石网络,wordpress获取title,网站建设基础课件Bean 和 Static
Static 没有办法控制在多个并发请求的时候#xff0c;只生成一个实例。有多少个请求就会生成多少个 实例#xff0c;那么此时如果用 Bean的方式来注解#xff0c;就会启用SpringBoot中的单例模式#xff0c;从始至终都只有一个实例
静态方法我们都知道Bean 和 Static
Static 没有办法控制在多个并发请求的时候只生成一个实例。有多少个请求就会生成多少个 实例那么此时如果用 Bean的方式来注解就会启用SpringBoot中的单例模式从始至终都只有一个实例
静态方法我们都知道那么Bean的方式怎么使用呢
Configuration
Slf4j
public class CephConfiguration {Value(#{${ceph.hosts}.split(,)})private ListString hosts;Value(${ceph.accessKey})private String accessKey;Value(${ceph.secretKey})private String secretKey;/*** 连接ceph*/Beanpublic AmazonS3 cephClient() {AmazonS3 amazonS3 null;AWSCredentialsProvider credentialsProvider new AWSCredentialsProvider() {Overridepublic AWSCredentials getCredentials() {return new BasicAWSCredentials(accessKey, secretKey);}Overridepublic void refresh() {}};ClientConfiguration clientConfiguration new ClientConfiguration();clientConfiguration.setProtocol(Protocol.HTTP);AwsClientBuilder.EndpointConfiguration endpointConfiguration new AwsClientBuilder.EndpointConfiguration(hosts.get(0), null);amazonS3 AmazonS3ClientBuilder.standard().withCredentials(credentialsProvider).withEndpointConfiguration(endpointConfiguration).withClientConfiguration(clientConfiguration).build();log.info(成功连接进入 ceph!);return amazonS3;}}
然后引入的时候直接
//注意这里不需要引用configuration类而是引用这个bean
Autowired
private AmazonS3 cephClient;