spring@PropertySource用法

v测试例子

复制代码
package com.hjzgg.auth.config;import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.core.env.Environment;
import org.springframework.util.StringUtils;import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;/*** Created by hujunzheng on 2017/6/22.*/
@Configuration
@PropertySource("classpath:conf/config.properties")
public class SystemConfig {public static class User {public static String NAME;public static int AGE;public static int USER_ID;public static String ADDRESS;}@Autowiredprivate Environment env;@Beanpublic SystemConfig oauthSystemConfig() throws IllegalAccessException {setStaticPropertiesValue(SystemConfig.class);Class<?>[] clazzs = SystemConfig.class.getClasses();for (Class clazz: clazzs) {setStaticPropertiesValue(clazz);}return null;}private void setStaticPropertiesValue(Class<?> clazz) throws IllegalAccessException {Field[] fields = clazz.getDeclaredFields();for (Field field : fields) {String key = field.getName().toLowerCase().replace("_", ".");String value = env.getProperty(key);if (StringUtils.isEmpty(value)) {continue;}if (field.getType() == List.class) {String[] values = value.split(",");List<String> vlist = new ArrayList<String>();for (String tvalue : values) {if (!StringUtils.isEmpty(tvalue)) {vlist.add(tvalue);}}field.set(null, vlist);}if (field.getType() == String[].class) {String[] values = value.split(",");List<String> vlist = new ArrayList<String>();for (String tvalue : values) {if (!StringUtils.isEmpty(tvalue)) {vlist.add(tvalue);}}field.set(null, vlist.toArray(new String[] {}));}if (field.getType() == String.class) {field.set(null, value);}if (field.getType() == Integer.class) {field.set(null, Integer.valueOf(value));}if (field.getType() == Float.class) {field.set(null, Float.valueOf(value));}if (field.getType() == Double.class) {field.set(null, Double.valueOf(value));}}}
}
复制代码

vConfiguration源码说明

复制代码
package org.springframework.context.annotation;import java.lang.annotation.Documented;import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;/*** Indicates that a class declares one or more {@link Bean @Bean} methods and may be processed* by the Spring container to generate bean definitions and service requests for those* beans at runtime, for example:* <pre class="code">* &#064;Configuration* public class AppConfig {*     &#064;Bean*     public MyBean myBean() {*         // instantiate, configure and return bean ...*     }* }</pre>** <h2>Bootstrapping {@code @Configuration} classes</h2>* <h3>Via {@code AnnotationConfigApplicationContext}</h3>* {@code @Configuration} classes are typically bootstrapped using either* {@link AnnotationConfigApplicationContext} or its web-capable variant,* {@link org.springframework.web.context.support.AnnotationConfigWebApplicationContext* AnnotationConfigWebApplicationContext}.* A simple example with the former follows:* <pre class="code">* AnnotationConfigApplicationContext ctx =*     new AnnotationConfigApplicationContext();* ctx.register(AppConfig.class);* ctx.refresh();* MyBean myBean = ctx.getBean(MyBean.class);* // use myBean ...</pre>** See {@link AnnotationConfigApplicationContext} Javadoc for further details and see* {@link org.springframework.web.context.support.AnnotationConfigWebApplicationContext* AnnotationConfigWebApplicationContext} for {@code web.xml} configuration instructions.** <h3>Via Spring {@code <beans>} XML</h3>* <p>As an alternative to registering {@code @Configuration} classes directly against an* {@code AnnotationConfigApplicationContext}, {@code @Configuration} classes may be* declared as normal {@code <bean>} definitions within Spring XML files:* <pre class="code">* {@code* <beans>*    <context:annotation-config/>*    <bean class="com.acme.AppConfig"/>* </beans>}</pre>** In the example above, {@code <context:annotation-config/>} is required in order to* enable {@link ConfigurationClassPostProcessor} and other annotation-related* post processors that facilitate handling {@code @Configuration} classes.** <h3>Via component scanning</h3>* <p>{@code @Configuration} is meta-annotated with {@link Component @Component}, therefore* {@code @Configuration} classes are candidates for component scanning (typically using* Spring XML's {@code <context:component-scan/>} element) and therefore may also take* advantage of {@link Autowired @Autowired}/{@link javax.inject.Inject @Inject}* at the field and method level (but not at the constructor level).* <p>{@code @Configuration} classes may not only be bootstrapped using* component scanning, but may also themselves <em>configure</em> component scanning using* the {@link ComponentScan @ComponentScan} annotation:* <pre class="code">* &#064;Configuration* &#064;ComponentScan("com.acme.app.services")* public class AppConfig {*     // various &#064;Bean definitions ...* }</pre>** See {@link ComponentScan @ComponentScan} Javadoc for details.*** <h2>Working with externalized values</h2>* <h3>Using the {@code Environment} API</h3>* Externalized values may be looked up by injecting the Spring* {@link org.springframework.core.env.Environment Environment} into a* {@code @Configuration} class using the {@code @Autowired} or the {@code @Inject}* annotation:* <pre class="code">* &#064;Configuration* public class AppConfig {*     &#064Inject Environment env;**     &#064;Bean*     public MyBean myBean() {*         MyBean myBean = new MyBean();*         myBean.setName(env.getProperty("bean.name"));*         return myBean;*     }* }</pre>** Properties resolved through the {@code Environment} reside in one or more "property* source" objects, and {@code @Configuration} classes may contribute property sources to* the {@code Environment} object using* the {@link org.springframework.core.env.PropertySources @PropertySources} annotation:* <pre class="code">* &#064;Configuration* &#064;PropertySource("classpath:/com/acme/app.properties")* public class AppConfig {*     &#064Inject Environment env;**     &#064;Bean*     public MyBean myBean() {*         return new MyBean(env.getProperty("bean.name"));*     }* }</pre>** See {@link org.springframework.core.env.Environment Environment}* and {@link PropertySource @PropertySource} Javadoc for further details.** <h3>Using the {@code @Value} annotation</h3>* Externalized values may be 'wired into' {@code @Configuration} classes using* the {@link Value @Value} annotation:* <pre class="code">* &#064;Configuration* &#064;PropertySource("classpath:/com/acme/app.properties")* public class AppConfig {*     &#064Value("${bean.name}") String beanName;**     &#064;Bean*     public MyBean myBean() {*         return new MyBean(beanName);*     }* }</pre>** This approach is most useful when using Spring's* {@link org.springframework.context.support.PropertySourcesPlaceholderConfigurer* PropertySourcesPlaceholderConfigurer}, usually enabled via XML with* {@code <context:property-placeholder/>}.  See the section below on composing* {@code @Configuration} classes with Spring XML using {@code @ImportResource},* see {@link Value @Value} Javadoc, and see {@link Bean @Bean} Javadoc for details on working with* {@code BeanFactoryPostProcessor} types such as* {@code PropertySourcesPlaceholderConfigurer}.** <h2>Composing {@code @Configuration} classes</h2>* <h3>With the {@code @Import} annotation</h3>* <p>{@code @Configuration} classes may be composed using the {@link Import @Import} annotation,* not unlike the way that {@code <import>} works in Spring XML. Because* {@code @Configuration} objects are managed as Spring beans within the container,* imported configurations may be injected using {@code @Autowired} or {@code @Inject}:* <pre class="code">* &#064;Configuration* public class DatabaseConfig {*     &#064;Bean*     public DataSource dataSource() {*         // instantiate, configure and return DataSource*     }* }** &#064;Configuration* &#064;Import(DatabaseConfig.class)* public class AppConfig {*     &#064Inject DatabaseConfig dataConfig;**     &#064;Bean*     public MyBean myBean() {*         // reference the dataSource() bean method*         return new MyBean(dataConfig.dataSource());*     }* }</pre>** Now both {@code AppConfig} and the imported {@code DatabaseConfig} can be bootstrapped* by registering only {@code AppConfig} against the Spring context:** <pre class="code">* new AnnotationConfigApplicationContext(AppConfig.class);</pre>** <h3>With the {@code @Profile} annotation</h3>* {@code @Configuration} classes may be marked with the {@link Profile @Profile} annotation to* indicate they should be processed only if a given profile or profiles are* <em>active</em>:* <pre class="code">* &#064;Profile("embedded")* &#064;Configuration* public class EmbeddedDatabaseConfig {*     &#064;Bean*     public DataSource dataSource() {*         // instantiate, configure and return embedded DataSource*     }* }** &#064;Profile("production")* &#064;Configuration* public class ProductionDatabaseConfig {*     &#064;Bean*     public DataSource dataSource() {*         // instantiate, configure and return production DataSource*     }* }</pre>** See {@link Profile @Profile} and {@link org.springframework.core.env.Environment Environment}* Javadoc for further details.** <h3>With Spring XML using the {@code @ImportResource} annotation</h3>* As mentioned above, {@code @Configuration} classes may be declared as regular Spring* {@code <bean>} definitions within Spring XML files. It is also possible to* import Spring XML configuration files into {@code @Configuration} classes using* the {@link ImportResource @ImportResource} annotation. Bean definitions imported from XML can be* injected using {@code @Autowired} or {@code @Import}:* <pre class="code">* &#064;Configuration* &#064;ImportResource("classpath:/com/acme/database-config.xml")* public class AppConfig {*     &#064Inject DataSource dataSource; // from XML**     &#064;Bean*     public MyBean myBean() {*         // inject the XML-defined dataSource bean*         return new MyBean(this.dataSource);*     }* }</pre>** <h3>With nested {@code @Configuration} classes</h3>* {@code @Configuration} classes may be nested within one another as follows:* <pre class="code">* &#064;Configuration* public class AppConfig {*     &#064;Inject DataSource dataSource;**     &#064;Bean*     public MyBean myBean() {*         return new MyBean(dataSource);*     }**     &#064;Configuration*     static class DatabaseConfig {*         &#064;Bean*         DataSource dataSource() {*             return new EmbeddedDatabaseBuilder().build();*         }*     }* }</pre>** When bootstrapping such an arrangement, only {@code AppConfig} need be registered* against the application context. By virtue of being a nested {@code @Configuration}* class, {@code DatabaseConfig} <em>will be registered automatically</em>. This avoids* the need to use an {@code @Import} annotation when the relationship between* {@code AppConfig} {@code DatabaseConfig} is already implicitly clear.** <p>Note also that nested {@code @Configuration} classes can be used to good effect* with the {@code @Profile} annotation to provide two options of the same bean to the* enclosing {@code @Configuration} class.** <h2>Configuring lazy initialization</h2>* <p>By default, {@code @Bean} methods will be <em>eagerly instantiated</em> at container* bootstrap time.  To avoid this, {@code @Configuration} may be used in conjunction with* the {@link Lazy @Lazy} annotation to indicate that all {@code @Bean} methods declared within* the class are by default lazily initialized. Note that {@code @Lazy} may be used on* individual {@code @Bean} methods as well.** <h2>Testing support for {@code @Configuration} classes</h2>* The Spring <em>TestContext framework</em> available in the {@code spring-test} module* provides the {@code @ContextConfiguration} annotation, which as of Spring 3.1 can* accept an array of {@code @Configuration} {@code Class} objects:* <pre class="code">* &#064;RunWith(SpringJUnit4ClassRunner.class)* &#064;ContextConfiguration(classes={AppConfig.class, DatabaseConfig.class})* public class MyTests {**     &#064;Autowired MyBean myBean;**     &#064;Autowired DataSource dataSource;**     &#064;Test*     public void test() {*         // assertions against myBean ...*     }* }</pre>** See TestContext framework reference documentation for details.** <h2>Enabling built-in Spring features using {@code @Enable} annotations</h2>* Spring features such as asynchronous method execution, scheduled task execution,* annotation driven transaction management, and even Spring MVC can be enabled and* configured from {@code @Configuration}* classes using their respective "{@code @Enable}" annotations. See* {@link org.springframework.scheduling.annotation.EnableAsync @EnableAsync},* {@link org.springframework.scheduling.annotation.EnableScheduling @EnableScheduling},* {@link org.springframework.transaction.annotation.EnableTransactionManagement @EnableTransactionManagement},* {@link org.springframework.context.annotation.EnableAspectJAutoProxy @EnableAspectJAutoProxy},* and {@link org.springframework.web.servlet.config.annotation.EnableWebMvc @EnableWebMvc}* for details.** <h2>Constraints when authoring {@code @Configuration} classes</h2>* <ul>*    <li>&#064;Configuration classes must be non-final*    <li>&#064;Configuration classes must be non-local (may not be declared within a method)*    <li>&#064;Configuration classes must have a default/no-arg constructor and may not*        use {@link Autowired @Autowired} constructor parameters. Any nested configuration classes*        must be {@code static}* </ul>** @author Rod Johnson* @author Chris Beams* @since 3.0* @see Bean* @see Profile* @see Import* @see ImportResource* @see ComponentScan* @see Lazy* @see PropertySource* @see AnnotationConfigApplicationContext* @see ConfigurationClassPostProcessor* @see org.springframework.core.env.Environment* @see org.springframework.test.context.ContextConfiguration*/
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Component
public @interface Configuration {/*** Explicitly specify the name of the Spring bean definition associated* with this Configuration class.  If left unspecified (the common case),* a bean name will be automatically generated.** <p>The custom name applies only if the Configuration class is picked up via* component scanning or supplied directly to a {@link AnnotationConfigApplicationContext}.* If the Configuration class is registered as a traditional XML bean definition,* the name/id of the bean element will take precedence.** @return the specified bean name, if any* @see org.springframework.beans.factory.support.DefaultBeanNameGenerator*/String value() default "";}
复制代码









本文转自 小眼儿 博客园博客,原文链接:http://www.cnblogs.com/hujunzheng/p/7066216.html,如需转载请自行联系原作者

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/256441.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

机器学习——支持向量机SVM之非线性模型(低维到高维映射)

目录 一、非线性模型的最优化问题 1、非线性模型最优化模型 2、两个概念 1&#xff09;正则项&#xff08;regularization term&#xff09; 2&#xff09;调参参数 2、高维映射 1&#xff09;定义及作用 2&#xff09;高维映射后的最优化模型 3&#xff09;异或问题&…

html表单中get与post之间的区别

当用户在 HTML 表单 (HTML Form) 中输入信息并提交之后&#xff0c;有两种方法将信息从浏览器传送到 Web 服务器 (Web Server)。 一种方法是通过 URL&#xff0c;另外一种是在 HTTP Request 的 body 中。 前一种方法&#xff0c;我们使用 HTML Form 中的 method "get&quo…

世界坐标系,摄像机坐标系、图像坐标系关系汇总

**摄像机标定&#xff1a;**在计算机视觉研究领域&#xff0c;摄像机标定是一个重要的环节。摄像机标定就是求取摄像机内外参数的过程。 世界坐标系&#xff1a;绝对坐标系&#xff0c;一般的三维场景都由这个坐标系来表示。摄像机可以放置在环境中的任何位置&#xff0c;因此可…

SpringMVC-HelloWorld

2&#xff0e;5、Hello World入门 2.5.1、准备开发环境和运行环境&#xff1a; ☆开发工具&#xff1a;eclipse ☆运行环境&#xff1a;tomcat6.0.20 ☆工程&#xff1a;动态web工程&#xff08;springmvc-chapter2&#xff09; ☆spring框架下载&#xff1a; spring-framework…

机器学习——支持向量机SVM之非线性模型(原问题和对偶问题)

目录 一、原问题&#xff08;prime problem&#xff09; 二、原问题的对偶问题&#xff08;dual problem&#xff09; 1、定义一个辅助函数 2、定义对偶问题 >>>问题1&#xff1a;上面说到遍历w&#xff0c;那w的取值范围和取值步长是怎样的&#xff1f;即遍历的…

(转)Apache Rewrite 详解

(转)Apache Rewrite 详解参考文档&#xff1a;http://man.chinaunix.net/newsoft/ApacheManual/mod/mod_rewrite.htmlApache Rewrite 详解一 入门RewriteEngine onRewriteRule !\.(js|ico|gif|jpg|png|css)$ index.php就这两行. 然后就完成了URL重写功能了. 首先服务器是需要支…

python轮廓函数的使用

在图像的处理中有时候需要对图像中的目标区域提出出轮廓 读取图像 调用OpenCV的库使用cv.imread()来读取图像。 图像为 灰度化 二值化 提取轮廓时&#xff0c;图像一般都是二值化后的图像。在本次程序中选用cv2.THRESH_BINARY的二值化方式。即将大于阈值的部分设定为255&am…

Intent Bundle页面跳转信息的传递

MainActivity LoginActivity LoginLayout 转载于:https://www.cnblogs.com/xiaolei121/p/5846644.html

机器学习——支持向量机SVM之非线性模型(原问题转化为对偶问题)

目录 一、复习&#xff08;原问题、对偶问题、KKT条件、凸函数&#xff09; 二、将最优化问题标准化为原问题&#xff08;严格转化为标准形式&#xff09; 1、原最优化问题 2、标准化后的问题 三、转化为对偶问题&#xff08;注意变量的对应关系&#xff09; 四、对对偶问…

静止的单摄像机无法得到像点的三维坐标详解

我们知道在机器视觉中通常要使用的搭建的视觉测量系统对一个物体的尺寸、形变、以及三维形貌进行测量。一般按照摄像机的个数以及组成部分分为三类测量方法。分别为单目测量、双目&#xff08;大于2为多目&#xff09;测量、以及结构光测量。 单目测量系统 顾名思义单目就指的…

未能加载文件或程序集“Poderosa.Core

https://github.com/poderosaproject/poderosa上下载的一个开源工程&#xff0c;程序是在VS2012上编译的&#xff0c;然后VS2015转换后编译失败&#xff0c;报“未能加载文件或程序集“Poderosa.Core......”的错误 猜测是转换的时候引用丢失了&#xff0c;于是添加引用 F:\...…

图像的像素、分辨率、像元尺寸、大小、清晰度的关系

图像的像素&#xff1a; 图像是由像素所组成的&#xff0c;像素的多少表明摄像机所含有的感光元件的多少。像素是指一张图像中所有的像素数之和。 图像分辨率&#xff1a; 是指表达方式也为“水平像素数垂直像素数” 像元尺寸&#xff1a; 是指一个像素在长和宽方向上所代表的实…

机器学习——支持向量机SVM实例(兵王问题,SVM求解步骤以及思路,不求解不编程)

目录 一、问题描述&#xff08;兵王问题&#xff09; 二、步骤 1、获得数据 2、样本划分&#xff08;训练样本和测试样本&#xff09; 3、训练样本得到SVM模型 ​ 1&#xff09;数据处理 2&#xff09;训练样本和测试样本归一化 3&#xff09;选择核函数和调参 4&#…

Django的安装

Django是Python的一款Web开源框架&#xff0c;所以Django是依赖于Python的&#xff0c;首先要安装Python。 Python安装 官网地址&#xff1a;http://www.python.org/download/ 在安装Python的时候&#xff0c;会有人纠结&#xff0c;是要安装Python2还是Python3呢&#xff1f;其…

机器学习——支持向量机SVM之多分类问题

目录 方法1&#xff1a;改造目标函数与限制条件 方法2&#xff1a;一类对其他类&#xff08;类数为N&#xff0c;需要建立N个SVM模型&#xff09; 情形1&#xff1a;多个SVM模型结果交集得出确切归类 情形2&#xff1a;多个SVM模型结果交集没有得出确切归类 方法3&#xff…

python3版本无法加载reload解决办法NameError: name 'reload' is not defined

很多人在运行八点法求基础矩阵问题时&#xff0c;都会遇到NameError: name ‘reload’ is not defined的错误 只需在最前面加上from imp import reload即可

hdu 2612 Find a way(bfs)

Problem DescriptionPass a year learning in Hangzhou, yifenfei arrival hometown Ningbo at finally. Leave Ningbo one year, yifenfei have many people to meet. Especially a good friend Merceki. Yifenfei’s home is at the countryside, but Merceki’s home is in …

七桥问题

怎么不重复地走完连接两座岛和陆地的七座桥&#xff1f; 简化为以下&#xff1a; 答案是不能走完的。 奇点&#xff1a;这个点有奇数条线汇聚于此 偶点&#xff1a;这个点有奇数条线汇聚于此 七桥问题——一笔画问题 若一个图形全部是偶点或者只有2个奇点&#xff08;没有…

office2016打开PPT出现解决VBE6EXT.OLB不能被加载问题的解决办法

第一步 打开路径C:\Program Files (x86)\Microsoft Office\root\VFS\ProgramFilesCommonX86\Microsoft Shared\VBA。找到VBA只要是默认安装路径均一样。 第二步 打开VBA6找到VBE6EXT.OLB将其复制到VBA7.1中。 第三步 打开VBA7.1找到VBE7.DLL将其复制到VBA6中。 第四步…