💎一站式轻松地调用各大LLM模型接口,支持GPT4、智谱、星火、月之暗面及文生图 广告
**1.SpringConfiguration** ~~~ package com.nobb; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Import; import org.springframework.context.annotation.PropertySource; @Configuration //扫描包配置:<context:component-scan base-package="com.nobb"/> @ComponentScan("com.nobb") //指定properties文件路径 @PropertySource("db.properties") //<import resource="a.xml"/> @Import(DBConfiguration.class) public class SpringConfiguration { } ~~~ **2.DBConfiguration** ~~~ package com.nobb; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Component; @Component("dbConfig") public class DBConfiguration { @Value("${jdbcUrl}") private String jdbcUrl; @Value("${driverClass}") private String driverClass; @Value("${user}") private String user; @Value("${password}") private String password; public String getJdbcUrl() { return jdbcUrl; } public void setJdbcUrl(String jdbcUrl) { this.jdbcUrl = jdbcUrl; } public String getDriverClass() { return driverClass; } public void setDriverClass(String driverClass) { this.driverClass = driverClass; } public String getUser() { return user; } public void setUser(String user) { this.user = user; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } } ~~~ **3.测试代码** ~~~ ApplicationContext ac = new AnnotationConfigApplicationContext(SpringConfiguration.class); DBConfiguration config = (DBConfiguration) ac.getBean("dbConfig"); System.out.println(config.getDriverClass()); ~~~