💎一站式轻松地调用各大LLM模型接口,支持GPT4、智谱、星火、月之暗面及文生图 广告
~~~ package com.like.config; import lombok.Data; import org.springframework.boot.context.properties.ConfigurationProperties; @ConfigurationProperties(prefix = "jdbc") @Data //使用lombok public class JdbcProperties { private String Driver; private String url; private String username; private String password; } ~~~ ~~~ package com.like.config; import com.alibaba.druid.pool.DruidDataSource; import org.springframework.boot.context.properties.EnableConfigurationProperties; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import javax.sql.DataSource; @Configuration @EnableConfigurationProperties(JdbcProperties.class) //使用配置类 public class SpringConfig { @Bean public DataSource createDataSource(JdbcProperties jdbcProperties) { DruidDataSource dataSource = new DruidDataSource(); dataSource.setDriverClassName(jdbcProperties.getDriver()); dataSource.setUrl(jdbcProperties.getUrl()); dataSource.setUsername(jdbcProperties.getUsername()); dataSource.setPassword(jdbcProperties.getPassword()); return dataSource; } } ~~~ ~~~ package com.like.controller; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.ResponseBody; import javax.sql.DataSource; @Controller public class HelloController { @Autowired private DataSource dataSource; @GetMapping("/index") @ResponseBody public String index() { System.out.println(dataSource); return "hello"; } } ~~~