/* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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 * * http://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 org.geekbang.thinking.in.spring.resource; import org.geekbang.thinking.in.spring.resource.util.ResourceUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.ResourceLoaderAware; import org.springframework.context.annotation.AnnotationConfigApplicationContext; import org.springframework.context.support.AbstractApplicationContext; import org.springframework.core.io.Resource; import org.springframework.core.io.ResourceLoader; import javax.annotation.PostConstruct; import java.util.stream.Stream; /** * 注入 {@link ResourceLoader} 对象示例 * * @author <a href="mailto:[email protected]">Mercy</a> * @see ResourceLoader * @see Resource * @see Value * @see AnnotationConfigApplicationContext * @since */ public class InjectingResourceLoaderDemo implements ResourceLoaderAware { private ResourceLoader resourceLoader; // 方法一 @Autowired private ResourceLoader autowiredResourceLoader; // 方法二 @Autowired private AbstractApplicationContext applicationContext; // 方法三 @PostConstruct public void init() { System.out.println("resourceLoader == autowiredResourceLoader : " + (resourceLoader == autowiredResourceLoader)); System.out.println("resourceLoader == applicationContext : " + (resourceLoader == applicationContext)); } @Override public void setResourceLoader(ResourceLoader resourceLoader) { this.resourceLoader = resourceLoader; } public static void main(String[] args) { AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(); // 注册当前类作为 Configuration Class context.register(InjectingResourceLoaderDemo.class); // 启动 Spring 应用上下文 context.refresh(); // 关闭 Spring 应用上下文 context.close(); } }