java中的接口是类吗
264
2023-06-16
谈谈Spring 注入properties文件总结
spring提供了多种方式来注入properties文件,本文做一个简单的总结。
在Spring配置文件中引入
方式一
通过
方式二
通过
1、MySQL.properties
#
ds1.jdbc.driverClassName=com.mysql.jdbc.Driver
ds1.jdbc.url=jdbc:mysql://localhost:3306/process?useUnicode=true&characterEncoding=UTF-8&zeroDateTimeBehavior=convertToNull
ds1.jdbc.username=root
ds1.jdbc.password=root
ds2.jdbc.driverClassName=com.mysql.jdbc.Driver
ds2.jdbc.url=jdbc:mysql://localhost:3306/process?useUnicode=true&characterEncoding=UTF-8&zeroDateTimeBehavior=convertToNull
ds2.jdbc.username=root
ds2.jdbc.password=root
2、applicationContext.xml
xsi:schemaLocation="
http://springframework.org/schema/beans
http://springframework.org/schema/beans/spring-beans.xsd
http://springframework.org/schema/util
http://springframework.org/schema/util/spring-util.xsd
http://springframework.org/schema/context
http://springframework.org/schema/context/spring-context.xsd"
default-lazy-init="false">
xsi:schemaLocation="
http://springframework.org/schema/beans
http://springframework.org/schema/beans/spring-beans.xsd
http://springframework.org/schema/util
http://springframework.org/schema/util/spring-util.xsd
http://springframework.org/schema/context
http://springframework.org/schema/context/spring-context.xsd"
default-lazy-init="false">
在代码中注入
方式一
1、config.properties
name=ricky
age=27
password=root
2、applicationContext.xml
class="org.springframework.beans.factory.config.PropertiesFactoryBean">
class="org.springframework.beans.factory.config.PropertiesFactoryBean">
3、使用@Value注解
package com.ricky.codelab.springmvc.domain;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
/**
* ${DESCRIPTION}
*
* @author Ricky Fung
* @create 2016-08-08 15:49
*/
@Component("userService")
public class UserServiceImpl implements IUserService {
private final Logger logger = LoggerFactory.getLogger(getClass());
@Value("#{config[name]}")
private String name;
@Value("#{config[age]}")
private Integer age;
@Value("#{config[password]}")
private String password;
@Override
public void login(String username){
System.out.println("name:"+name+",age="+age+",password="+password);
}
}
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。
发表评论
暂时没有评论,来抢沙发吧~