SpringBoot配置参数绑定@[email protected]
程序员文章站
2022-03-09 19:00:08
...
将spring配置文件中配置的参数绑定给组件的属性
作用
- 将spring配置文件中配置的参数绑定给组件的属性
实现
步骤
- 容器中注册组件
- 参数绑定
注册组件
- @Component
- @Repository
- @Service
- @Controller
- @RestController
- @[email protected]
- 等等
参数绑定种类
- 单个属性绑定:组件[email protected]
- 批量属性绑定:组件[email protected]
- 批量属性绑定:配置类@[email protected]
说明:容器组件@[email protected]也是可以实现配置参数绑定功能,但是从系统设计角度不推荐这么做;就像@[email protected]也能实现组件注册功能一样能实现但是不推荐
@Value和@ConfigurationProperties示例
- 待绑定属性的容器组件
package com.ultra.config;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
@Component
@ConfigurationProperties(prefix = "user1")
public class User {
private String name;
@Value("use1.nickname")
private String nickname;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "User{" +
"name='" + name + '\'' +
", nickname='" + nickname + '\'' +
'}';
}
}
- spring配置文件
user1.name=XiaoMing
user1.nickname=XiaoBaiLong
- 测试类/启动类
package com.ultra;
import com.ultra.config.User;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
/**
* @author admin
*/
@SpringBootApplication
public class SpringbootWebApplication {
public static void main(String[] args) {
ConfigurableApplicationContext applicationContext = SpringApplication.run(SpringbootWebApplication.class, args);
User user = applicationContext.getBean(User.class);
System.out.println(user);
}
}
- 结果
配置类@[email protected]示例
- 待绑定属性的容器组件
package com.ultra.config;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
@Component@ConfigurationProperties(prefix = "user1")
public class User {
private String name;
@Value("use1.nickname")
private String nickname;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "User{" +
"name='" + name + '\'' +
", nickname='" + nickname + '\'' +
'}';
}
}
- spring配置文件
user1.name=XiaoMing
user1.nickname=XiaoBaiLong
- 配置类
package com.ultra.config;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Configuration;
@Configuration
@EnableConfigurationProperties({User.class})
public class UserConfig {
}
- 测试类/启动类
package com.ultra;
import com.ultra.config.User;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
/**
* @author admin
*/
@SpringBootApplication
public class SpringbootWebApplication {
public static void main(String[] args) {
ConfigurableApplicationContext applicationContext = SpringApplication.run(SpringbootWebApplication.class, args);
User user = applicationContext.getBean(User.class);
System.out.println(user);
}
}
- 结果
上一篇: win10纯净版和正版的区别是什么