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.pswp.cn/news/256441.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/256441.shtml
英文地址,請注明出處:http://en.pswp.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…

CSVN備份初體驗

備份方法挺多的&#xff0c;目前我知道有四種 其一&#xff1a; 首先復制舊csvn服務器上repositories下的版本庫文件夾到新csvn服務器repositories文件夾下面&#xff08;做以下修改時最好把csvn服務停掉&#xff09; 然后復制舊csvn的svn_auth_file到新csvn上&#xff08;相當…

機器學習——支持向量機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

超易懂數據庫范式

那些數據庫的書介紹的數據庫范式&#xff0c;實在是晦澀難懂&#xff0c;我在這里給出一個通俗的描述&#xff1a;1NF&#xff1a;一個table中的列是不可再分的&#xff08;即列的原子性&#xff09;2NF&#xff1a;一個table中的行是可以唯一標示的&#xff0c;&#xff08;即…

機器學習——支持向量機SVM之非線性模型(原問題轉化為對偶問題)

目錄 一、復習&#xff08;原問題、對偶問題、KKT條件、凸函數&#xff09; 二、將最優化問題標準化為原問題&#xff08;嚴格轉化為標準形式&#xff09; 1、原最優化問題 2、標準化后的問題 三、轉化為對偶問題&#xff08;注意變量的對應關系&#xff09; 四、對對偶問…

靜止的單攝像機無法得到像點的三維坐標詳解

我們知道在機器視覺中通常要使用的搭建的視覺測量系統對一個物體的尺寸、形變、以及三維形貌進行測量。一般按照攝像機的個數以及組成部分分為三類測量方法。分別為單目測量、雙目&#xff08;大于2為多目&#xff09;測量、以及結構光測量。 單目測量系統 顧名思義單目就指的…

一個比較笨的全文搜索的例子(分析結構用)-模糊查找

1 2 3 4 --在所有的char類型的列中模糊查找某個值。5 --**注意預先切換到當前的數據庫中。6 DECLARE TABLE_CATALOG NVARCHAR(MAX)--數據庫名7 ,TABLE_SCHEMA NVARCHAR(MAX)--架構名8 ,TABLE_NAME NVARCHAR(MAX)--表名9 ,COLUMN_NAME NVARCHAR(MAX)--列名 10 ,SQL N…

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

https://github.com/poderosaproject/poderosa上下載的一個開源工程&#xff0c;程序是在VS2012上編譯的&#xff0c;然后VS2015轉換后編譯失敗&#xff0c;報“未能加載文件或程序集“Poderosa.Core......”的錯誤 猜測是轉換的時候引用丟失了&#xff0c;于是添加引用 F:\...…

個人閱讀作業Week7

上了大學之后其實就沒有很多時間去讀書了&#xff0c;與其說軟工作業時給我們布置了一些任務&#xff0c;但是也是在另一方面讓我們得到了更多的知識的填補&#xff0c;因為平常能夠接觸的書籍很少&#xff0c;平常自己也是一個很不愛看書的人&#xff0c;所以我覺得這樣的作業…

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

圖像的像素&#xff1a; 圖像是由像素所組成的&#xff0c;像素的多少表明攝像機所含有的感光元件的多少。像素是指一張圖像中所有的像素數之和。 圖像分辨率&#xff1a; 是指表達方式也為“水平像素數垂直像素數” 像元尺寸&#xff1a; 是指一個像素在長和寬方向上所代表的實…

機器學習——支持向量機SVM實例(兵王問題,SVM求解步驟以及思路,不求解不編程)

目錄 一、問題描述&#xff08;兵王問題&#xff09; 二、步驟 1、獲得數據 2、樣本劃分&#xff08;訓練樣本和測試樣本&#xff09; 3、訓練樣本得到SVM模型 ? 1&#xff09;數據處理 2&#xff09;訓練樣本和測試樣本歸一化 3&#xff09;選擇核函數和調參 4&#…

單攝像機對于二維平面的測量

二維平面測量 首先我們應該知道什么是二維平面&#xff0c;二維平面簡單的說就是只含有x,y坐標系的平面&#xff0c;在z軸上沒有當量。這種一般出現在對于一個平面的寬度&#xff0c;長度&#xff0c;變形的測量&#xff0c;一般應用較多的場合是對一個規則的機械零件進行尺寸…

十六進制,輸出的時候怎樣控制所輸出字母的大小寫。

the first&#xff1a;printf("%x",10);printf("%X",10); 可以用x的大小寫來控制&#xff0c;所輸出字符的大小寫&#xff0c;真是一個巧妙的技能。轉載于:https://www.cnblogs.com/A-FM/p/4970114.html