龙空技术网

Spring Framework 基于注解装载IoC配置元信息

NiceEleven 26

前言:

目前大家对“如何给spring容器提供配置元数据”可能比较关心,同学们都需要知道一些“如何给spring容器提供配置元数据”的相关文章。那么小编在网摘上搜集了一些有关“如何给spring容器提供配置元数据””的相关知识,希望兄弟们能喜欢,我们一起来学习一下吧!

本篇我们来一起看一下Spring Framework 基于注解装载IoC配置元信息。这里我总结了以下内容:

接下来我们就通过实际案例一起实践一下:

# User BeanDefinition 定义user.(class) = org.eleven.thinking.in.spring.ioc.overview.domain.User# <property name="id" value="1"/>user.id = 1# <property name="name" value="eleven"/>user.name = eleven# <property name="city" value="CHANGSHA"/>user.city = CHANGSHA# <property name="workCities" value="BEIJING,HANGZHOU"/>user.workCities = BEIJING,CHANGSHA# <property name="lifeCities">## <list>## <value>BEIJING</value>## <value>CHANGSHA</value>##</list># </property>user.lifeCities = BEIJING,CHANGSHA# <property name="configFileLocation" value="classpath:/META-INF/user-config.properties"/>user.configFileLocation = classpath:/META-INF/user-config.properties/* * 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 * *      * * 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.eleven.thinking.in.spring.configuration.metadata;import org.eleven.thinking.in.spring.ioc.overview.domain.User;import org.springframework.beans.factory.annotation.Value;import org.springframework.context.annotation.*;import java.util.Map;/** * 基于 Java 注解 Spring IoC 容器元信息配置示例 * * @author <a href="mailto:eleven.hm@vip.163.com">eleven</a> * @since */// 将当前类作为 Configuration Class@ImportResource("classpath:/META-INF/dependency-lookup-context.xml")@Import(User.class)@PropertySource("classpath:/META-INF/user-bean-definitions.properties") // Java 8+ @Repeatable 支持@PropertySource("classpath:/META-INF/user-bean-definitions.properties")// @PropertySources(@PropertySource(...))public class AnnotatedSpringIoCContainerMetadataConfigurationDemo {    /**     * user.name 是 Java Properties 默认存在,当前用户:eleven,而非配置文件中定义"eleven"     * @param id     * @param name     * @return     */    @Bean    public User configuredUser(@Value("${user.id}") Long id, @Value("${user.name}") String name) {        User user = new User();        user.setId(id);        user.setName(name);        return user;    }    public static void main(String[] args) {        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();        // 注册当前类作为 Configuration Class        context.register(AnnotatedSpringIoCContainerMetadataConfigurationDemo.class);        // 启动 Spring 应用上下文        context.refresh();        // beanName 和 bean 映射        Map<String, User> usersMap = context.getBeansOfType(User.class);        for (Map.Entry<String, User> entry : usersMap.entrySet()) {            System.out.printf("User Bean name : %s , content : %s \n", entry.getKey(), entry.getValue());        }        // 关闭 Spring 应用上下文        context.close();    }}

标签: #如何给spring容器提供配置元数据