@Configuration
xml注入方式:
<bean id="person" class="com.bean.Person">
<property name="name" value="tyz"></property>
<property name="age" value="1"></property>
</bean>
获取方式:
ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("beans.xml");
Person bean = (Person) applicationContext.getBean("person"); //根据id
System.out.println(bean.toString());
Person bean1 = applicationContext.getBean(Person.class); //根据类型
System.out.println(bean1.toString());
注解注入方式:
@Configuration //标识为spring配置类
public class PersonConfig {
// 给容器注册一个Bean;类型为返回值;id默认为方法名
@Bean //可以定义其他名称 @Bean("person")
public Person person(){
return new Person("Xxx", 12);
}
}
获取方式:
AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(PersonConfig.class); //获取配置
Person bean = (Person) applicationContext.getBean("person"); //根据name
System.out.println(bean.toString());
Person bean1 = applicationContext.getBean(Person.class); //根据类型
System.out.println(bean1.toString());
运用的设计模式:单例( Singleton):在整个应用中,只创建bean的一个实例。
原型( Prototype):每次注入或者通过Spring应用上下文获取的时候,都会创建一个新的bean实例。
@ComponentScan
自动扫描组件注解,运行配置多个。
@ComponentScan(value = "com.web") //指定包路径
public class WebConfig {
}
在com.web下创建PersonController 、PersonService,指定@Controller、@Service注解
@Controller
public class PersonController {}
@Service
public class PersonService {}
测试自动扫描注入的Bean
@Test
public void test01(){
AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(WebConfig.class);
String[] beanDefinitionNames = applicationContext.getBeanDefinitionNames(); //获取注入到的Bean
for (String name : beanDefinitionNames) {
System.out.println(name); //打印:webConfig、personController、personService
}
}
配置扫描过滤的规则:FilterType枚举指定类型,如注解类型,classes为注解类
@ComponentScan(value = "com.web", excludeFilters = {
//指定排除扫描的注解,例如Service、Controller等
@ComponentScan.Filter(type = FilterType.ANNOTATION, classes = Controller.class)
})
xml方式:
<!-- 扫描包 排除Controller注解 -->
<context:component-scan base-package="com.web">
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller" />
</context:component-scan>
再次测试打印,排除掉personController。
配置扫描过滤的规则:只扫描某注解
@ComponentScan(value = "com.web", includeFilters = {
//指定只扫描的注解,例如Service、Controller等
@ComponentScan.Filter(type = FilterType.ANNOTATION, classes = Controller.class)
}, useDefaultFilters = false)
xml方式:
<!-- 扫描注解 只扫描Controller, use-default-filters 默认true 扫描全部 需关闭 -->
<context:component-scan base-package="com.web" use-default-filters="false">
<context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
再次测试打印,排除掉personService。
其他过滤方式:
例如指定Bean类型、Aspectj表达式、正则表达式、自定义方式,进行排除或者注入,
//指定Bean类型
@ComponentScan.Filter(type = FilterType.ASSIGNABLE_TYPE, classes = PersonService.class)
读取配置
配置信息
test:
abc: 123456
app: a123
server:
port: 1111
方法一:
@Value("${server.port}") String serverPort;
方法二:
@ConfigurationProperties(prefix = "test")匹配前缀,以test.开头
@PropertySource("application.yml") 配置读取的配置文件,application.yml默认会读取,可以不写
@Component
@PropertySource("application.yml")
@ConfigurationProperties(prefix = "test")
public class TestProperties {
private Integer abc;
private String app;
public Integer getAbc() {
return abc;
}
public void setAbc(Integer abc) {
this.abc = abc;
}
public String getApp() {
return app;
}
public void setApp(String app) {
this.app = app;
}
@Override
public String toString() {
return "TestProperties{" +
"abc=" + abc +
", app='" + app + '\'' +
'}';
}
}
配置文件参数静态访问
创建工具类TestPropertiesUtils
public class TestPropertiesUtils {
public static Integer abc;
public static String app;
public static void setConfigInfo(TestProperties configInfo){
abc = configInfo.getAbc();
app = configInfo.getApp();
}
}
在TestProperties中添加下列代码,初始化静态配置
@PostConstruct
public void init(){
TestPropertiesUtils.setConfigInfo(this);
}
测试
@SpringBootTest
public class SpringbootAnnotationApplicationTests {
@Autowired
TestProperties testProperties;
@Value("${server.port}")
String serverPort;
@Test
public void contextLoads() {
System.out.println(testProperties.toString());
System.out.println("serverPort = " + serverPort);
System.out.println("TestPropertiesUtils.abc = " + TestPropertiesUtils.abc);
System.out.println("TestPropertiesUtils.app = " + TestPropertiesUtils.app);
}
// 打印:
// TestProperties{abc=123456, app='a123'}
// serverPort = 1111
// TestPropertiesUtils.abc = 123456
// TestPropertiesUtils.app = a123
}