Spring学习十二----------Bean的配置之@ImportResource和@Value

© 版权声明:本文为博主原创文章,转载请注明出处

@ImportResource

-引入XML配置文件

@Value

-从配置文件中获取值

实例

1.项目结构

2.pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
	
	<modelVersion>4.0.0</modelVersion>
	
	<groupId>org.spring</groupId>
	<artifactId>Spring-ImportResource</artifactId>
	<packaging>war</packaging>
	<version>0.0.1-SNAPSHOT</version>
	<name>Spring-ImportResource Maven Webapp</name>
	<url>http://maven.apache.org</url>
	
	<!-- 统一开发环境 -->
	<properties>
		<spring.version>4.3.8.RELEASE</spring.version>
	</properties>
	
	<dependencies>
		<!-- junit依赖 -->
		<dependency>
			<groupId>junit</groupId>
			<artifactId>junit</artifactId>
			<version>4.12</version>
			<scope>test</scope>
		</dependency>
		<!-- spring核心依赖 -->
		<dependency>
		    <groupId>org.springframework</groupId>
		    <artifactId>spring-core</artifactId>
		    <version>${spring.version}</version>
		</dependency>
		<dependency>
		    <groupId>org.springframework</groupId>
		    <artifactId>spring-beans</artifactId>
		    <version>${spring.version}</version>
		</dependency>
		<dependency>
		    <groupId>org.springframework</groupId>
		    <artifactId>spring-context</artifactId>
		    <version>${spring.version}</version>
		</dependency>
	</dependencies>
	
	<build>
		<finalName>Spring-ImportResource</finalName>
	</build>
	
</project>

3.spring-importresource.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="org.spring.importresource"/>
        
</beans>

4.jdbc.properties

jdbc.url=127.0.0.1
#若使用username,则会获取系统的用户名
jdbc.username=root
jdbc.password=123456

5.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:/jdbc.properties"/>
        
</beans>

6.DriverManage.java

package org.spring.importresource;

public class DriverManage {

	public DriverManage(String url, String username, String password) {
		
		System.out.println("url:" + url);
		System.out.println("username:" + username);
		System.out.println("password:" + password);
		
	}
	
}

7.DriverConfig.java

package org.spring.importresource;

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")//引入xml配置文件
public class DriverConfig {

	@Value("${jdbc.url}")//从配置文件中取值
	private String url;
	
	@Value("${jdbc.username}")
	private String username;
	
	@Value("${jdbc.password}")
	private String password;
	
	@Bean
	public DriverManage myDriver() {
		
		return new DriverManage(url, username, password);
		
	}
	
}

8.TestBase.java

package org.spring.importresource.test;

import org.junit.After;
import org.junit.Before;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.util.StringUtils;

public class TestBase {

	private ClassPathXmlApplicationContext context;
	private String springXmlPath;
	
	/**
	 * 无参构造器
	 */
	public TestBase() {

	}
	
	/**
	 * 含参构造器,初始化spring配置文件路径
	 * 
	 * @param springXmlPath
	 * 						spring配置文件路径
	 */
	public TestBase(String springXmlPath) {
		
		this.springXmlPath = springXmlPath;
		
	}
	
	/**
	 * 初始化spring配置文件到IOC容器中
	 */
	@Before
	public void before() {
		
		if(StringUtils.isEmpty(springXmlPath)) {
			springXmlPath = "classpath:spring-*.xml";
		}
		context = new ClassPathXmlApplicationContext(springXmlPath.split("[,\\s]+"));
		context.start();
		
	}
	
	/**
	 * 销毁IOC容器
	 */
	@After
	public void after() {
		
		if(context != null){
			context.destroy();
		}
		
	}
	
	/**
	 * 根据bean ID获取bean对象
	 * 
	 * @param beanId
	 * 				bean ID
	 * @return
	 */
	public Object getBean(String beanId) {
		
		return context.getBean(beanId);
		
	}
	
}

9.TestSpringImportResource.java

package org.spring.importresource.test;

import org.junit.Test;
import org.spring.importresource.DriverManage;

public class TestSpringImportResource extends TestBase {

	public TestSpringImportResource() {
		
		super("classpath:spring-importresource.xml");
		
	}
	
	@Test
	public void testDriverManager() {
		
		DriverManage dm = (DriverManage) super.getBean("myDriver");
		System.out.println(dm.getClass().getName());
		
	}

}

10.效果预览

参考:http://www.imooc.com/video/4034

posted @ 2017-04-24 15:21  禁忌夜色153  阅读(585)  评论(0编辑  收藏  举报