马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有账号?立即注册
x
方式一:XML
设置方式一:
- <!--引入外部属性文件1-->
- <bean class="org.springframework.context.support.PropertySourcesPlaceholderConfigurer">
- <property name="locations" value="classpath:jdbc.properties"/>
- </bean>
复制代码 设置方式一:
- <!--引入外部属性文件2-->
- <context:property-placeholder location="classpath:jdbc.properties"/>
复制代码 设置文件:
- prop.driverClass=com.mysql.jdbc.Driver
- prop.url=jdbc:mysql://localhost:3306/userDb
- prop.userName=root
- prop.password=abc123
复制代码 \
动态取值:
- <!--配置连接池-->
- <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
- <property name="url" value="${prop.url}"/>
- <property name="username" value="${prop.userName}"/>
- <property name="password" value="${prop.password}"/>
- <property name="driverClassName" value="${prop.driverClass}"/>
- </bean>
复制代码 方式一:@ConfigurationProperties(prefix = "xxx")+@Component
- @ConfigurationProperties(prefix = "person")
- @Component
- @Data
- public class Person {
- private String userName;
- private Boolean boss;
- private Date birth;
- private Integer age;
- private Pet pet;
- private String[] interests;
- private List<String> animal;
- private Map<String, Object> score;
- private Set<Double> salarys;
- private Map<String, List<Pet>> allPets;
- }
复制代码 设置文件:application.properties
- person.user-name=zhangsan
- person.boss=true
- person.birth=2019/12/9
- person.age=18
- person.pet.name=阿狗
- person.pet.weight=99.99
- person.interests=篮球,足球
- person.animal=阿猫,阿狗
- person.score.english=80
- person.score.math=80
- person.salarys=9999.98,9999.99
- person.all-pets.sick[0].name=阿狗;
- person.all-pets.sick[0].weight=99.99
- person.all-pets.health[0].name=阿花
- person.all-pets.health[1].name=阿明
- person.all-pets.health[0].weight=199.99
- person.all-pets.health[1].weight=200
- #https://blog.csdn.net/qq_35754073/article/details/136606186
复制代码 测试:
- @Autowired
- Person person;
-
- @Test
- void test() {
- System.out.println(person);
- }
复制代码
方式三:@ConfigurationProperties+@EnableConfigurationProperties(xxx.class)
- @ConfigurationProperties(prefix = "person")
- @Data
- public class Person {
- private String userName;
- private Boolean boss;
- private Date birth;
- private Integer age;
- private Pet pet;
- private String[] interests;
- private List<String> animal;
- private Map<String, Object> score;
- private Set<Double> salarys;
- private Map<String, List<Pet>> allPets;
- }
复制代码- @EnableConfigurationProperties(Person.class)
- @Configuration
- public class MyConfig {
-
- @Bean
- public Person person(Person person) {
- System.out.println(person);
- return person;
- }
- }
复制代码 测试:
- @Autowired
- MyConfig myConfig;
复制代码
方式四:@Value+application.properties
设置文件:
- myconfig.name=1111
- myconfig.pwd=2222
复制代码 测试:application.properties
- @Value("${myconfig.name}")
- private String name;
- @Value("${myconfig.pwd}")
- private String pwd;
- @Test
- void test2() {
- System.out.println(name);
- System.out.println(pwd);
- }
复制代码
方式四:@Value+xxx.properties自界说设置文件
设置:other.properties
- other.name=3333
- other.pwd=4444
复制代码- @PropertySources({
- @PropertySource("classpath:other.properties")
- })
- @Configuration
- public class OtherConfig {
-
复制代码 测试:
- @Value("${other.name}")
- private String otherName;
- @Value("${other.pwd}")
- private String otherPwd;
- @Test
- void test3() {
- System.out.println(otherName);
- System.out.println(otherPwd);
- }
复制代码
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。 |