在我的 Spring Boot 应用程序中,我使用调用存储过程的方法实现了以下类。
@Component
@ConfigurationProperties(prefix = "spring")
public class FmTrfUtil {
static int returnVal;
@Value("${spring.datasource.url}")
static String url;
public static int insertFmTrfs(List<String> trfs, String source) {
System.out.println(url);
EntityManager em = Persistence.createEntityManagerFactory("RIStore_FM").createEntityManager();
Session session = em.unwrap( Session.class );
final String[] trfArray = trfs.toArray(new String[trfs.size()]);
final String src = source;
session.doWork( new Work(){
public void execute(Connection conn) throws SQLException {
CallableStatement stmt = null;
OracleConnection oraCon = conn.unwrap(OracleConnection.class);
Array array = oraCon.createARRAY("VARCHAR2_TAB_T", trfArray);
stmt = conn.prepareCall("{? = call FM_TRF_UTIL.process_fm_trf(?,?)}");
stmt.registerOutParameter(1, Types.INTEGER);
stmt.setArray(2, array);
stmt.setString(3, src);
stmt.execute();
returnVal = stmt.getInt(1);
}
});
return returnVal;
}
}
由于调用存储过程需要数据库连接,我需要从 application.properties 中加载这些对应的属性值:
spring.profiles.active=dev
spring.datasource.url=jdbc:oracle:thin:@ldap://xxx:389/risdev3, cn=OracleContext,dc=x,dc=net
spring.datasource.username=owner
spring.datasource.password=owner987
基于以下关于类似问题的文章,Spring boot - Application.properties 中的自定义变量和在您自己的类中使用 Spring-Boot 配置属性和Spring Boot @ConfigurationProperties 示例,我为我的类添加了这个注释@ConfigurationProperties(prefix = "spring")
(数据库连接的属性都有“弹簧”作为前缀)。但是,当我使用如下测试类运行它时,出现错误“应用程序必须提供 JDBC 连接”,这意味着 application.properties 中的属性没有被拾取。
@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = RistoreWebApplication.class, initializers = ConfigFileApplicationContextInitializer.class)
public class FmTrfUtilTest {
@Test
public void test() {
List<String> trfs = new ArrayList<String>();
trfs.add("TRF000001");
trfs.add("TRF000002");
int ret = FmTrfUtil.insertFmTrfs(trfs, "SARC");
assertTrue(ret > 0);
}
}
为了@ConfigurationProperties
工作,我也添加了 Maven 依赖spring-boot-configuration-processor
项。为什么它仍然不起作用?我错过了什么?