1 解析
1.1 这两个注解应用在什么地方
1.2 应用方式
1.3 xml方式实现取值
2 代码演练
2.1 @ImportResource和@Value代码演练
1 解析
1.1 这两个注解应用在什么地方
接口调用,java与properties文件交互获取url和用户名密码等配置信息
1.2 应用方式
java类通过调用@Importresource找到xml,通过xml配置properties
示例:
@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; }
xml配置:
<context:property-placeholder location="classpath:/config.properties" ignore-unresolvable="true"/>
config.properties配置:
jdbc.url = 127.0.0.1 jdbc.userName = root jdbc.passWord = root
1.3 xml方式实现取值
<context:property-placeholder location="classpath:/config.properties" ignore-unresolvable="true"/> <bean class="com.ddwei"> <property name="url" value="${jdbc.url}"/> <property name="username" value="${jdbc.username}"/> <property name="password" value="${jdbc.password}"/> </bean>
2 代码演练
2.1 @ImportResource和@Value代码演练
测试类:
package com.imooc.test.beanannotation; import org.junit.Test; import com.imooc.beanannotation.javabased.MyDriverManager; import com.imooc.test.base.UnitTestBase; public class TestJavaBased extends UnitTestBase{ public TestJavaBased(){ super("classpath*:spring-beanannotation.xml"); } @Test public void testStoreConfig(){ System.out.println(super.getbean("store").getClass().getName()); } @Test public void testMyDriverStore(){ MyDriverManager myDriverStore =super.getbean("myDriverStore"); System.out.println(myDriverStore.getClass().getName()); } }
实体类:
package com.imooc.beanannotation.javabased; 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; public String getUrl() { return url; } public void setUrl(String url) { this.url = url; } public String getUserName() { return userName; } public void setUserName(String userName) { this.userName = userName; } public String getPassWord() { return passWord; } public void setPassWord(String passWord) { this.passWord = passWord; } // @Bean(name="store",initMethod="init",destroyMethod="destroy") // public Store getStringStore(){ // return new StringStore(); // } /** * 此处作为实体类: * 通过@bean将myDriverStore注解到spring容器中,供TestJavaBased调用 * @return */ @Bean public MyDriverManager myDriverStore() { return new MyDriverManager(url, userName, passWord); } }
关联类:
package com.imooc.beanannotation.javabased; /** * 测试基类 * 被StoreConfig 以bean注解的方式引入 * @author weijingli * */ public class MyDriverManager { /** * 有参构造方法,被StoreConfig调用 * @param url * @param userName * @param passWord */ public MyDriverManager(String url,String userName,String passWord) { System.out.println("This url is "+url); System.out.println("This userName is "+userName); System.out.println("This url passWord "+passWord); } }
config.xml
<?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.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <context:property-placeholder location="classpath:/config.properties" ignore-unresolvable="true"/> </beans>
config.properties:
jdbc.url = 127.0.0.1 jdbc.userName = root jdbc.passWord = root
spring-beanannotation.xml:
<?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.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <context:component-scan base-package="com.imooc.beanannotation"></context:component-scan> </beans>
打印结果:
三月 30, 2019 5:49:45 下午 org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh 信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@789df61d: startup date [Sat Mar 30 17:49:45 CST 2019]; root of context hierarchy 三月 30, 2019 5:49:45 下午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions 信息: Loading XML bean definitions from URL [file:/F:/xiangmu3/Xin/FuQiang/Spring/ddwei-dao/target/classes/spring-beanannotation.xml] 三月 30, 2019 5:49:46 下午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions 信息: Loading XML bean definitions from class path resource [config.xml] This url is 127.0.0.1 This userName is root This url passWord root com.imooc.beanannotation.javabased.MyDriverManager 三月 30, 2019 5:49:47 下午 org.springframework.context.support.ClassPathXmlApplicationContext doClose 信息: Closing org.springframework.context.support.ClassPathXmlApplicationContext@789df61d: startup date [Sat Mar 30 17:49:45 CST 2019]; root of context hierarchy
原文地址:https://www.cnblogs.com/1446358788-qq/p/10628297.html
时间: 2024-10-16 10:01:39