package com.example.demo.config;
import com.example.demo.domain.Student;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.*;
import org.springframework.core.env.Environment;
@Configuration
@PropertySource("classpath:my.properties")
@Import(MyConfig2.class)
public class MyConfig {
@Autowired
private Environment environment;
@Profile("dev")
@Bean("student")
public Student getStudent(){
Student student = new Student();
student.setAge(Integer.parseInt( environment.getProperty("age")));
student.setName(environment.getProperty("name"));
return student;
}
}
其中,student這個(gè)bean只有指定了profile才會(huì)裝配,比如在application.properties中:
spring.profiles.active=dev
@Profile注解不僅可以用在@Bean上,還可以用在注解類上,用在注解類時(shí)文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-617641.html
package com.example.demo.config;
import com.example.demo.domain.Teacher;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
@Configuration
@Profile("dev2")
public class MyConfig2 {
@Bean
public Teacher getTeacher(){
Teacher teacher = new Teacher();
teacher.setName("david");
teacher.setAge(32);
return teacher;
}
}
只有設(shè)定了profile為指定的值,該配置類中的所有bean才會(huì)裝配文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-617641.html
到了這里,關(guān)于SpringBoot復(fù)習(xí):(7)@Profile注解的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!