Inject properties file content with spring annotations and do unit testing with junit4+spring

Source: Internet
Author: User

look at the working directory first and then explain

1, the establishment of config.properties, my config.properties content is as follows:


Author_name=luolinproject_info= This project is mainly used to write some demo



2. Configure the spring configuration file, read the properties file, and set the encoding format. You can see from my project structure that I used two spring configuration files, In fact, no other content in the Spring-context.xml configuration, just configure scan Com.eya.property This package, you may have questions about why the package scan is not directly configured in Spring-mvc.xml to scan Com.eya. Actually it's a habit problem that different things do different things in Spring-mvc.xml I only configured to scan Com.eya.controller this package.


<!--use annotations to inject values in properties--><bean id= "setting" class= " Org.springframework.beans.factory.config.PropertiesFactoryBean "><property name=" Locations "><list ><value>classpath:config.properties</value></list></property><!--Set the encoding format-- <property name= "fileencoding" value= "UTF-8" ></property></bean>



3. Preparation and The Configproperty.java file that corresponds to the value in the Config.properties file. Annotated @comopnent ("Configproperty") gives this class to the spring container management, and specifies that the component name is configproperty.


This shows that the internal format of the @Value annotations is #{beanid[propertykey]} , here's Beanid is the ID value specified when configuring Propertiesfactorybean in the second step, PropertyKey is and the key in the config.properties corresponds.

/** * */package Com.eya.property;import Org.springframework.beans.factory.annotation.value;import org.springframework.stereotype.component;/** * config.properties file mapping class * @author Luolin * * @version $id:    Configproperty.java,v 0.1 August 7, 2015 PM 2:10:44 luolin EXP $ */@Component ("Configproperty") public class Configproperty {    /** Author name */@Value ("#{setting[author_name]}") Private String authorname;    /** Project Information */@Value ("#{setting[project_info]}") Private String projectInfo;    /** * @return The authorname * * * public String Getauthorname () {return authorname; }/** * @param authorname the AuthorName to set */public void Setauthorname (String authorname) {thi    S.authorname = AuthorName;    }/** * @return the ProjectInfo * */Public String Getprojectinfo () {return projectInfo;        }/** * @param projectInfo the ProjectInfo to set */public void Setprojectinfo (String projectInfo) { This.projectinfo = ProjectInfo; }}


4, write unit test, test whether inject success. Here is the way to JUNIT4 + spring annotations as an exercise.


/** *  */package com.eya.property;import javax.annotation.resource;import org.junit.test;import Org.junit.runner.runwith;import Org.springframework.test.context.contextconfiguration;import org.springframework.test.context.junit4.springjunit4classrunner;/** * JUNIT4 + Spring annotations for unit testing, The test obtains the value of the properties file through spring annotations * @author Luolin * * @version $id: configpropertytest.java,v 0.1 August 7, 2015 PM 2:21:26 luolin Ex P $ */@RunWith (Springjunit4classrunner.class) @ContextConfiguration (locations={"Classpath:spring-mvc.xml", " Classpath:spring-context.xml "}) public class Configpropertytest {    @Resource (name =" Configproperty ")    private Configproperty Configproperty;        /**     * Test spring annotations Get the value of the properties file     */    @Test public    Void Test () {        System.out.println ( Configproperty.getauthorname ());        System.out.println (Configproperty.getprojectinfo ());}    }


Run Results



during the problem of garbled, because I changed the config.properties code to UTF-8, started not in the configuration of spring files when the code is specified, so garbled, and then I looked down Propertiesfactorybean source code, in its parent class found the setting encoding properties, set to the corresponding encoding on it.



Inject properties file content with spring annotations and do unit testing with junit4+spring

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.