课程链接:

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

Spring课程 Spring入门篇 4-6 Spring bean装配之基于java的容器注解说明--@ImportResource和@Value java与properties文件交互的更多相关文章

  1. Spring @Bean注解 (基于java的容器注解)

    基于java的容器注解,意思就是使用Java代码以及一些注解,就可以取代spring 的 xml配置文件. 1-@Configuration & @Bean的配合 @Configuration ...

  2. Spring Boot -01- 快速入门篇(图文教程)

    Spring Boot -01- 快速入门篇(图文教程) 今天开始不断整理 Spring Boot 2.0 版本学习笔记,大家可以在博客看到我的笔记,然后大家想看视频课程也可以到[慕课网]手机 app ...

  3. Spring实践系列-入门篇(一)

    本文主要介绍了在本地搭建并运行一个Spring应用,演示了Spring依赖注入的特性 1 环境搭建 1.1 Maven依赖 目前只用到依赖注入的功能,故以下三个包已满足使用. <properti ...

  4. Spring Cloud Alibaba入门篇

    学习条件 了解web三层架构 熟练应用SSM架构 了解Maven管理工具的使用 熟练使用SpringBoot,以及了解SpringBoot基本原理. 了解部分术语:应用.工具.耦合.负载等 温馨提示: ...

  5. Spring Data JPA 入门篇

    Spring Data JPA是什么 它是Spring基于ORM框架(如hibernate,Mybatis等).JPA规范(Java Persistence API)封装的一套 JPA应用框架,可使开 ...

  6. Spring Boot源码(四):Bean装配

    为了演示Spring中对象是如何创建并放到spring容器中,这里新建一个maven项目: 其中pom.xm文件中只引入了一个依赖: <dependencies> <dependen ...

  7. Spring学习(七)bean装配详解之 【通过注解装配 Bean】【自动装配的歧义解决】

    自动装配 1.歧义性 我们知道用@Autowired可以对bean进行注入(按照type注入),但如果有两个相同类型的bean在IOC容器中注册了,要怎么去区分对哪一个Bean进行注入呢? 如下情况, ...

  8. Spring学习(六)bean装配详解之 【通过注解装配 Bean】【基础配置方式】

    通过注解装配 Bean 1.前言 优势 1.可以减少 XML 的配置,当配置项多的时候,XML配置过多会导致项目臃肿难以维护 2.功能更加强大,既能实现 XML 的功能,也提供了自动装配的功能,采用了 ...

  9. Spring课程 Spring入门篇 4-8 Spring bean装配之基于java的容器注解说明--基于泛型的自动装配

    1 解析 1.1 什么是泛型? 1.2 泛型有什么作用? 1.3 泛型装配样式? 2 代码演练 2.1 泛型应用 1 解析 1.1 什么是泛型? Java泛型设计原则:只要在编译时期没有出现警告,那么 ...

随机推荐

  1. 【bzoj4832】[Lydsy1704月赛]抵制克苏恩 期望dp

    Description 小Q同学现在沉迷炉石传说不能自拔.他发现一张名为克苏恩的牌很不公平.如果你不玩炉石传说,不必担心,小Q 同学会告诉你所有相关的细节.炉石传说是这样的一个游戏,每个玩家拥有一个 ...

  2. 洛谷P2763 试题库问题(最大流)

    传送门 网络流界的一股清流啊……终于没那么变态了…… 考虑一下怎么建图.对于每一个类型,我们从$S$向他连边,容量为它所需的题数,表明它要可以有这么多题,对于每一道题目,我们从它对应的类型向他连边,容 ...

  3. eclipse创建Java项目时提示Open Associated Perspective?

    在eclipse中,原先使用python进行编程,需要新建java项目时,会提示如下信息: 消息框内翻译如下: Open Associated Perspective? --开放关联视角? This ...

  4. 网页footer背景(stick footer布局)

    今天遇到了一个有意思的问题,想在网站的foot里面加入一张背景图片,并且在footer的底部写下一些内容于是乎在footer添加了background,并设置了footer的大小 先说一下开始的做法: ...

  5. ML理论知识博文列表

    一文弄懂神经网络中的反向传播法——BackPropagation:http://www.cnblogs.com/charlotte77/p/5629865.html 人脸识别图库:http://vis ...

  6. AtCoder - 2566 优先队列

    Let N be a positive integer. There is a numerical sequence of length 3N, a=(a1,a2,…,a3N). Snuke is c ...

  7. python 获得毫秒级时间戳

    import time import datetime t = time.time() print (t) #原始时间数据 print (int(t)) #秒级时间戳 print (int(round ...

  8. C++_基础4-分支语句和逻辑运算符

    这一部分截取自<C++ Primer Plus>,内容比较简单,很多只取了一些主题关键词,有空再补充: 设计智能程序的一个关键是使程序具有决策能力. 前面一种方式是循环——程序决定是否继续 ...

  9. 119th LeetCode Weekly Contest Largest Perimeter Triangle

    Given an array A of positive lengths, return the largest perimeter of a triangle with non-zero area, ...

  10. (转)一条SQL更新语句是如何执行的

    名词 MySQL 里经常说到的 WAL 技术,Write-Ahead Logging 第一个日志模块 redo log 也叫日志重写,是InnoDB 引擎特有的日志 - write pos and c ...