Spring Learning---@ImportResource & @Value annotations for Java class-based configuration Beans

Source: Internet
Author: User

Learn how to use @importresource and @Value annotations for resource file reads

Example:

First create a Mydrivermanager class (simulates reading database configuration information)

Package Com.beanannotation;public class Mydrivermanager {public Mydrivermanager (String url,string username,string Password) {System.out.println ("URL:" +url); System.out.println ("Username:" +username); System.out.println ("Password:" +password);}}

Create Storeconfig

Package Com.beanannotation;import Org.springframework.beans.factory.annotation.value;import Org.springframework.context.annotation.bean;import Org.springframework.context.annotation.configuration;import Org.springframework.context.annotation.ImportResource, @Configuration @importresource ("Classpath:config.xml") public class Storeconfig {@Value ("${url}") private string URL, @Value ("${username}") private string Username; @Value ("${ Password} ") private String password; @Beanpublic mydrivermanager Mydrivermanager () {return new Mydrivermanager (URL, Username,password);}}

XML configuration (context:property-placeholder specifies the location of the resource file)

<?xml version= "1.0" encoding= "UTF-8"?> <beans xmlns= "Http://www.springframework.org/schema/beans"        Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"        xmlns:context= "Http://www.springframework.org/schema /context "        xsi:schemalocation=" Http://www.springframework.org/schema/beans/            http Www.springframework.org/schema/beans/spring-beans-4.1.xsd            Http://www.springframework.org/schema/context            http://www.springframework.org/schema/context/spring-context-4.1.xsd ">             <context: Property-placeholder location= "classpath:config.properties"/>      <context:component-scan base-package= " Com.beanannotation ">     </context:component-scan>             </beans>

Create a resource file Config.properties

url=127.0.0.1username=rootpassword=123456

Unit tests:

Package Com.beanannotation;import Org.junit.test;import Org.springframework.context.applicationcontext;import Org.springframework.context.support.classpathxmlapplicationcontext;public class UnitTest {@Testpublic void Test () { ApplicationContext context = new Classpathxmlapplicationcontext ("Classpath:spring-beanannotation.xml");  Mydrivermanager service= (Mydrivermanager) Context.getbean ("Mydrivermanager"); System.out.println (Service.getclass (). GetName ());}}

Results:

2015-7-7 17:22:25 org.springframework.context.support.ClassPathXmlApplicationContext Preparerefresh Info: Refreshing Org[email protected]36b8bef7:startup Date [Tue Jul 17:22:25 CST 2015]; Root of context hierarchy2015-7-7 17:22:25 Org.springframework.beans.factory.xml.XmlBeanDefinitionReader Loadbeandefinitions info: Loading XML Bean Definitions from class path resource [config.xml]2015-7-7 17:22:25 Org.springfram Ework.beans.factory.xml.XmlBeanDefinitionReader loadbeandefinitions Info: Loading XML bean definitions from class path resource [config.xml]2015-7-7 17:22:25 Org.springframework.context.support.PropertySourcesPlaceholderConfigurer Loadproperties Info: Loading properties file from class path resource [Config.properties]2015-7-7 17:22:25 Org.springframework.context.support.PropertySourcesPlaceholderConfigurer loadproperties Info: Loading Properties File From class path resource [Config.properties]url:127.0.0.1username:administratorpassword: 123456com.beanannotation.mydrivermanageR 

PS: Did you find that the result is abnormal----username is not root but the current user name of the computer

@Value ("${username}") will fetch the name of the current user when the value is taken

So in the future use, to avoid the use of username

So, modify config.properties and Storeconfig.

jdbc.url=127.0.0.1jdbc.username=rootjdbc.password=123456

Package Com.beanannotation;import Org.springframework.beans.factory.annotation.value;import Org.springframework.context.annotation.bean;import Org.springframework.context.annotation.configuration;import Org.springframework.context.annotation.ImportResource, @Configuration @importresource ("Classpath:config.xml") public class Storeconfig {@Value ("${jdbc.url}") private string URL; @Value ("${jdbc.username}") private string username;@ Value ("${jdbc.password}") private String password; @Beanpublic mydrivermanager Mydrivermanager () {return new Mydrivermanager (Url,username,password);}}

Test Ibid.:

Results:

2015-7-7 17:26:41 org.springframework.context.support.ClassPathXmlApplicationContext Preparerefresh Info: Refreshing Org[email protected]32bf7190:startup Date [Tue Jul 17:26:41 CST 2015]; Root of context hierarchy2015-7-7 17:26:41 Org.springframework.beans.factory.xml.XmlBeanDefinitionReader Loadbeandefinitions info: Loading XML Bean Definitions from class path resource [config.xml]2015-7-7 17:26:41 Org.springfram Ework.beans.factory.xml.XmlBeanDefinitionReader loadbeandefinitions Info: Loading XML bean definitions from class path resource [config.xml]2015-7-7 17:26:41 Org.springframework.context.support.PropertySourcesPlaceholderConfigurer Loadproperties Info: Loading properties file from class path resource [Config.properties]2015-7-7 17:26:41 Org.springframework.context.support.PropertySourcesPlaceholderConfigurer loadproperties Info: Loading Properties File From class path resource [Config.properties]url:127.0.0.1username:rootpassword: 123456com.beanannotation.mydrivermanager

It's going to be all right.

Spring Learning---@ImportResource & @Value annotations for Java class-based configuration Beans

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.