Spring的@Value使用

@Value注解:注入基本数据类型以及它们的包装类和String类型数据的,支持注入Properties文件的键值对,等同 <proprty  name=”…” value=”${Key}”>。

配置步骤

项目结构图

在这里插入图片描述

CustomDao

package com.spring.dao;

public interface CustomDao {

	void insert();
}

CustomDaoImpl

package com.spring.dao.impl;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Repository;

import com.spring.dao.CustomDao;

@Repository
public class CustomDaoImpl implements CustomDao{
	
	@Value("${jdbc.driverName}")
	private String driverClassName;
	
	@Value("${jdbc.url}")
	private String url;
	
	@Value("${jdbc.username}")
	private String username;
	
	@Value("${jdbc.password}")
	private String password;
	
	@Value("10")
	private Integer maxActive;
	

	@Override
	public void insert() {
		System.out.println("driverClassName:"+driverClassName);
		System.out.println("url:"+url);
		System.out.println("username"+username);
		System.out.println("password:"+password);
		System.out.println("maxActive:"+maxActive);
	}

}

CustomServiceImpl

package com.spring.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.spring.dao.CustomDao;
import com.spring.service.CustomService;

@Service
public class CustomServiceImpl  implements CustomService{
	
	@Autowired
	private CustomDao customDao;

	@Override
	public void insert() {
		customDao.insert();
	}

}

CustomController

package com.spring.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import com.spring.service.CustomService;

@Controller
public class CustomController {

	@Autowired
	private CustomService customService;
	
	public void insert() {
		customService.insert();
	}
}

applicationContext.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-4.3.xsd">
        
        <!-- 声明扫描包以及子包的类。如果发现有组件注解的类,就创建对象,并加入到容器
        	配置Spring注解包扫描的位置
        	<context:component-scan base-package="com.spring"/>
        	base-package:对应要扫描包的位置,可以扫描多个包,使用逗号隔开
         -->
         <context:component-scan base-package="com.spring"/>
         
         <context:property-placeholder location="classpath:db.properties"/>
</beans>

db.properties

#批量修改alt+shift+a,使用鼠标多拉,修改完毕以后,alt+shift+a 还原
jdbc.driverName=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/gj1?characterEncoding=utf8
jdbc.username=root
jdbc.password=123456
jdbc.maxActive=10

测试代码

@Test
	public void testName() throws Exception {

		// 1.读取Spring配置文件,启动Spring框架,创建Spring容器对象
		ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");

		CustomController controller = applicationContext.getBean("customController", CustomController.class);
		controller.insert();
	}

测试结果图

在这里插入图片描述

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值