spring(二)profile的使用

4 篇文章 0 订阅
Profile 的含义其实是“一组对于用户进行描述的数据”,所以个人觉得“档案”应该是最贴切的。
在spring中使用profile的方法用两种java注释和xml配置

 

@Configuration
@ComponentScan(basePackages = "com.websystique.spring")
public class AppConfig {
	
	@Autowired
	public DataSource dataSource;
	

}
public interface DatabaseConfig {

	DataSource createDataSource();
	
}

@Profile("Development")
@Configuration
public class DevDatabaseConfig implements DatabaseConfig {

	@Bean
	public DataSource createDataSource() {
		System.out.println("Creating DEV database");
		DriverManagerDataSource dataSource = new DriverManagerDataSource();
		/*
		 * Set MySQL specific properties for Development Environment
		 */
		return dataSource;
	}

}

@Profile("Production")
@Configuration
public class ProductionDatabaseConfig implements DatabaseConfig {

 
	@Bean
	public DataSource createDataSource() {
		System.out.println("Creating Production database");
		DriverManagerDataSource dataSource = new DriverManagerDataSource();
		/*
		 * Set ORACLE specific properties for Production environment
		 */
		return dataSource;
	}

}

测试类

public class AppMain {
	
	public static void main(String args[]){
 
    	AnnotationConfigApplicationContext  context = new AnnotationConfigApplicationContext();
 		//Sets the active profiles
 	    context.getEnvironment().setActiveProfiles("Development");
 		//Scans the mentioned package[s] and register all the @Component available to Spring
 		context.scan("com.websystique.spring"); 
        context.refresh();
        context.close();
 	}

}

xml的配置方法

prod-config-context.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"
        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd">
 
     
    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="com.mysql.jdbc.Driver" />
        <property name="url" value="jdbc:mysql://localhost:3306/websystique" />
        <property name="username" value="myuser" />
        <property name="password" value="mypassword" />
    </bean>
 
</beans>

app-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-4.0.xsd
                            http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">
 
     
    <context:component-scan base-package="com.websystique.spring"/>
     
    <beans profile="Development">
        <import resource="dev-config-context.xml"/>
    </beans>
 
    <beans profile="Production">
        <import resource="prod-config-context.xml"/>
    </beans>
 
</beans>

dev-config-context.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"
        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd">
 
     
    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="com.mysql.jdbc.Driver" />
        <property name="url" value="jdbc:mysql://localhost:3306/websystique" />
        <property name="username" value="myuser" />
        <property name="password" value="mypassword" />
    </bean>
 
</beans>


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值