1、组件添加
1.1 @Configuration
@Configuration(proxyBeanMethods = true)
最佳实战
- 配置 类组件之间无依赖关系用Lite模式加速容器启动过程,减少判断
- 配置类组件之间有依赖关系,方法会被调用得到之前单实例组件,用Full模式
主启动类测试
package com.springboot;
import com.springboot.Entity.ConfigUser;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
@SpringBootApplication(scanBasePackages = "com.springboot")
@MapperScan("com.springboot.Mapper")
public class SpringbootApplication {
public static void main(String[] args) {
ConfigurableApplicationContext context = SpringApplication.run(SpringbootApplication.class, args);
String[] names = context.getBeanDefinitionNames();
for (String name:names) {
System.out.println(name);
}
ConfigUser configUser1 =context.getBean("pige",ConfigUser.class);
System.out.println("组件为:"+configUser1);
ConfigUser configUser2 =context.getBean("pige",ConfigUser.class);
System.out.println(configUser1 == configUser2);
}
}
1. 2 原始实现
- @Bean、@Component、@Controller、@Service、@Repository
1.3 @ComponentScan、@Import
- @ComponentScan:扫描
- @Import:在配置类中导入组件,可以为新注册,也可以为依赖中已有的
@Import({User.class, Depart.class})
1.4 @Conditional
条件装配:满足Conditional指定的条件,则进行组件注入
例如: 如果存在为pige的组件则注册组件user01,否则不注册
@Bean
@ConditionalOnBean(name = "pige")
public ConfigUser user01() {
return new ConfigUser("yan", 18);
}
@Bean(name = "pige")
public ConfigPet pet(){
return new ConfigPet("pige");
}
2 、原生配置文件引入
@ImportResource("classpath:beans.xml")
3、配置绑定
将properties配置文件装配到JavaBean中
3.1 @Component + @ConfigurationProperties
在实体类中添加注解(@Component + @ConfigurationProperties)引入配置,在配置文件中添加配置
package com.springboot.Entity;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
@Data
@AllArgsConstructor
@NoArgsConstructor
@Component
@ConfigurationProperties(prefix = "student")
public class ConfigStudent {
private Long id;
private String name;
private Integer age;
}
#对象赋值
student:
id: 1
name: pige
age: 17
###############布尔型
# happy: true
###############日期
# birth: 1998/09/09
###############map
# maps: {k1: v1,k2: v2}
###############list
# lists:
# - play
# - music
# - girl
package com.springboot.Controller;
import com.springboot.Entity.ConfigStudent;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/configstudent")
public class ConfigStudentController {
@Autowired
ConfigStudent configStudent;
@RequestMapping("/student")
public ConfigStudent configStudent(){
return configStudent;
}
}
浏览器访问:http://localhost:8003/configstudent/student 显 示 结 果:{"id":1,"name":"pige","age":17}
3.2 @EnableConfigurationProperties + @ConfigurationProperties
在配置类中开启属性配置(@EnableConfigurationProperties)功能,在实体类中引入配置( @ConfigurationProperties)
@EnableConfigurationProperties(ConfigStudent.class)
package com.springboot.Entity;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
@Data
@AllArgsConstructor
@NoArgsConstructor
@ConfigurationProperties(prefix = "student")
public class ConfigStudent {
private Long id;
private String name;
private Integer age;
}
4、总结
配置类:
package com.springboot.Config;
import com.springboot.Entity.*;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.context.annotation.ImportResource;
@Configuration(proxyBeanMethods = true)
@Import({User.class, Depart.class})
@ImportResource("classpath:beans.xml")
@EnableConfigurationProperties(ConfigStudent.class)
public class MyConfig {
@Bean
@ConditionalOnBean(name = "pige")
public ConfigUser user01() {
return new ConfigUser("yan", 18);
}
@Bean(name ="pige")
public ConfigPet pet() {
return new ConfigPet("pige");
}
}
实体类:
package com.springboot.Entity;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@AllArgsConstructor
@NoArgsConstructor
public class ConfigUser {
private String name;
private Integer age;
}
package com.springboot.Entity;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@NoArgsConstructor
@AllArgsConstructor
public class ConfigPet {
private String petName;
}
package com.springboot.Entity;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
@Data
@AllArgsConstructor
@NoArgsConstructor
@ConfigurationProperties(prefix = "student")
public class ConfigStudent {
private Long id;
private String name;
private Integer age;
}
application文件:
server:
port: 8003
#对象赋值
student:
id: 1
name: pige
age: 17
###############布尔型
# happy: true
###############日期
# birth: 1998/09/09
###############map
# maps: {k1: v1,k2: v2}
###############list
# lists:
# - play
# - music
# - girl
|