@Configuration //告诉spring这是一个配置类
@ComponentScan
- value指定要扫描的包
- Filter[] excludeFilters() default {}; 扫描的时候按照规则排除哪些
@ComponentScan(value = "com.ysy",excludeFilters = @ComponentScan.Filter(type = FilterType.ANNOTATION,classes = {Controller.class,Service.class}))
- includeFilters 扫描的时候按照规则只包含哪些
- ASSIGNABLE_TYPE按照指定类型进行扫描
使用aspectJ表达式
REGEX:正则表达式
custom:自定义规则
package com.ysy.config;
import com.ysy.Person;
import com.ysy.service.BookService;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.FilterType;
import org.springframework.stereotype.Controller;
/**
* @author shanyangyang
* @date 2020/5/22
* 配置类等于以前的配置文件
*/
@Configuration //告诉spring这是一个配置类
@ComponentScan(value = "com.ysy",includeFilters = @ComponentScan.Filter(
type = FilterType.CUSTOM,classes = MyTypeFilter.class
// type=FilterType.ASSIGNABLE_TYPE,classes = BookService.class
// type = FilterType.ANNOTATION,classes = Controller.class
),useDefaultFilters = false)
//value指定要扫描的包
//Filter[] excludeFilters() default {}; 扫描的时候按照规则排除哪些
//@ComponentScan(value = "com.ysy",excludeFilters = @ComponentScan.Filter(type = FilterType.ANNOTATION,classes = {Controller.class,
// Service.class}))
//includeFilters 扫描的时候按照规则只包含哪些
//ASSIGNABLE_TYPE按照指定类型进行扫描
//使用aspectJ表达式
//REGEX:正则表达式
//custom:自定义规则
public class MainConfig {
@Bean("person") //给容器中注册一个bean,为返回值的类型,id默认是方法名
public Person person01(){
return new Person("lisi",20);
}
}
实体Bean
package com.ysy;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.PropertySource;
/**
* @author shanyangyang
* @date 2020/5/22
*/
public class Person {
//使用@value赋值
//1、基本数值
//2、可以写SPEL表达式 #{}
//3、可以写${},取出配置文件中的值(运行的环境变量中的值)
@Value("ysy")
private String name;
@Value("#{10+18}")
private Integer age;
@Value("${person.nickName}")
private String nickName;
public Person() {
}
public Person(String name, Integer age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public String getNickName() {
return nickName;
}
public void setNickName(String nickName) {
this.nickName = nickName;
}
@Override public String toString() {
return "Person{" + "name='" + name + '\'' + ", age=" + age + ", nickName='" + nickName + '\'' + '}';
}
}
测试类
package com.ysy.test;
import com.ysy.Person;
import com.ysy.config.MainConfig;
import com.ysy.config.MainConfig2;
import org.junit.Test;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.core.env.ConfigurableEnvironment;
import java.util.Map;
/**
* @author shanyangyang
* @date 2020/5/22
*/
public class IOCTest {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(MainConfig2.class);
@Test
public void testImport(){
printBeans(context);
}
public void printBeans(AnnotationConfigApplicationContext context){
String[] beanDefinitionNames = context.getBeanDefinitionNames();
for(String name:beanDefinitionNames){
System.out.println(name);
}
//工厂Bean获取的是调用getObject创建的对象
Object colorFactoryBean = context.getBean("colorFactoryBean");
System.out.println("Bean的类型==="+colorFactoryBean.getClass());
Object colorFactoryBean1 = context.getBean("&colorFactoryBean");
System.out.println("Bean的类型==="+colorFactoryBean1.getClass());
}
// @Test
// public void test03(){
// //获取环境变量和操作系统
// ConfigurableEnvironment environment = context.getEnvironment();
// //System.out.println(environment.getProperty("os.name"));
// String[] beanNamesForTypes = context.getBeanNamesForType(Person.class);
// for (String beanNameType:beanNamesForTypes){
// System.out.println(beanNameType);
// }
//
// Map<String, Person> beansOfType = context.getBeansOfType(Person.class);
// System.out.println(beansOfType);
// }
// @Test
// public void test01(){
// AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(MainConfig.class);
//
// String[] definitionNames = context.getBeanDefinitionNames();
// for(String name:definitionNames){
// System.out.println(name);
// }
// }
// @Test
// public void test02(){
// AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(MainConfig2.class);
//
String[] definitionNames = context.getBeanDefinitionNames();
for(String name:definitionNames){
System.out.println(name);
}
// System.out.println("容器初始化完成");
// Object person = context.getBean("person");
// Object person1 = context.getBean("person");
// System.out.println(person == person1);
// }
}