<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" 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.xsd">
<context:property-placeholder location="classpath:jdbc.properties"/>
jdbc_driverClassName=com.mysql.jdbc.Driver jdbc_url=jdbc:mysql://localhost/testdb?useUnicode=true&characterEncoding=utf8 jdbc_username=root jdbc_password=123456
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init"
destroy-method="close" >
<property name="driverClassName">
<value>${jdbc_driverClassName}</value>
</property>
<property name="url">
<value>${jdbc_url}</value>
</property>
<property name="username">
<value>${jdbc_username}</value>
</property>
<property name="password">
<value>${jdbc_password}</value>
</property>
<!-- 连接池最大使用连接数 -->
<property name="maxActive">
<value>20</value>
</property>
<!-- 初始化连接大小 -->
<property name="initialSize">
<value>1</value>
</property>
<!-- 获取连接最大等待时间 -->
<property name="maxWait">
<value>60000</value>
</property>
<!-- 连接池最大空闲 -->
<property name="maxIdle">
<value>20</value>
</property>
<!-- 连接池最小空闲 -->
<property name="minIdle">
<value>3</value>
</property>
<!-- 自动清除无用连接 -->
<property name="removeAbandoned">
<value>true</value>
</property>
<!-- 清除无用连接的等待时间 -->
<property name="removeAbandonedTimeout">
<value>180</value>
</property>
<!-- 连接属性 -->
<property name="connectionProperties">
<value>clientEncoding=UTF-8</value>
</property>
</bean>
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
@Configuration
public class JdbcConfig{
@Value("${jdbc_url}")
public String jdbcUrl; //这里变量不能定义成static
@Value("${jdbc_username}")
public String username;
@Value("${jdbc_password}")
public String password;
}
@RequestMapping("/service/**")
@Controller
public class JdbcController{
@Autowired
private JdbcConfig Config; //引用统一的参数配置类
@Value("${jdbc_url}")
private String jdbcUrl; //直接在Controller引用
@RequestMapping(value={"/test"})
public ModelMap test(ModelMap modelMap) {
modelMap.put("jdbcUrl", Config.jdbcUrl);
return modelMap;
}
@RequestMapping(value={"/test2"})
public ModelMap test2(ModelMap modelMap) {
modelMap.put("jdbcUrl", this.jdbcUrl);
return modelMap;
}
}
{
jdbcUrl:"jdbc:mysql://localhost/testdb?useUnicode=true&characterEncoding=utf8"
}
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:util="http://www.springframework.org/schema/util" 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.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd">
<util:properties id="settings" location="classpath:config.properties"/>
gnss.server.url=http://127.0.0.1:8080/gnss/services/data-world/rest
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
@Component
public class Config {
@Value("#{settings['gnss.server.url']}")
public String GNSS_SERVER_URL;
}
@RequestMapping("/service2/**")
@Controller
public class ConfigController{
@Autowired
private Config Config; //引用统一的参数配置类
@RequestMapping(value={"/test"})
public ModelMap test(ModelMap modelMap) {
modelMap.put("gnss.service.url",Config.GNSS_SERVER_URL);
return modelMap;
}
}
{
"gnss.service.url":"http://127.0.0.1:8080/gnss/services/data-world/rest"
}
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
@Configuration
@PropertySource(value="classpath:config.properties")
public class Config {
@Value("${gnss.server.url}")
public String GNSS_SERVER_URL;
@Value("${gnss.server.url}")
public String jdbcUrl;
}
3.2、在controller中调用
@RequestMapping("/service2/**")
@Controller
public class ConfigController{
@Autowired
private Config Config; //引用统一的参数配置类
@RequestMapping(value={"/test"})
public ModelMap test(ModelMap modelMap) {
modelMap.put("gnss.service.url", Config.GNSS_SERVER_URL);
return modelMap;
}
}
{
"gnss.service.url":"http://127.0.0.1:8080/gnss/services/data-world/rest"
}
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:util="http://www.springframework.org/schema/util"
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.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util-4.0.xsd">
<!-- 引入jdbc配置文件 -->
<context:property-placeholder location="classpath:jdbc.properties"/>
<!-- 引入多配置文件 -->
<context:property-placeholder location="classpath:jdbc.properties,classpath:XXX.properties"/>
<!-- 通过util引入config配置文件 -->
<!-- <util:properties id="settings" location="classpath:config.properties" /> -->
<!-- 扫描文件(自动将servicec层注入) -->
<context:component-scan base-package="修改成你的Config类所在的package"/></beans>
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有