Spring学习-01 Spring利用注解注册Bean到容器中
01.01 依赖配置(pom.xml)
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
</dependencies>
01.02 一个Entity(Person.java)
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Person {
private Integer id;
private String name;
private Integer age;
}
01.03 一个Service(PersonService.java)
该类会生成一个bean,交给Spring容器管理
@Service("personService") //
public class PersonService {
public void say() {
Person person = new Person(1, "zhangsan", 30);
System.out.println(person.toString());
}
}
01.04 配置类(MainConfig.java)
该类声明为配置类,并用@ComponentScan
指定要扫描的包,@Bean
@Service
,@Controller
,@Component
等注解的类就会生成一个bean给容器管理.
//配置类==配置文件
@Configuration //告诉Spring这是一个配置类
@ComponentScan("com.min.spring")
public class MainConfig {
}
@ ComponentScan
的用法
@ComponentScan(value="com.min.spring",
includeFilters = { //代表字扫描的内容
@Filter(type=FilterType.ANNOTATION,classes={Controller.class})
},
useDefaultFilters = false) //在写includeFilters是,必需为false
//@ComponentScan value:指定要扫描的包
//excludeFilters = Filter[] :指定扫描的时候按照什么规则排除那些组件
//includeFilters = Filter[] :指定扫描的时候只需要包含哪些组件
//FilterType.ANNOTATION:按照注解
//FilterType.ASSIGNABLE_TYPE:按照给定的类型;
//FilterType.ASPECTJ:使用ASPECTJ表达式
//FilterType.REGEX:使用正则指定
//FilterType.CUSTOM:使用自定义规则
@ ComponentScans
的用法
就是组合ComponentScan
@ComponentScans(
value = { @ComponentScan(xxx),
@ComponentScan(xxx)}
)
01.05 主类(AppMain.java)来测试
public class AppMain {
public static void main(String[] args) {
//创建容器,利用注解方式配置,MainConfig中用@ComponentScan指定了扫描包,
//PersonService类上用@Servie标注,代表会生成对象交给容器管理
ApplicationContext context = new AnnotationConfigApplicationContext(MainConfig.class);
//获取容器中对象
PersonService personService = context.getBean(PersonService.class);
//获取对象在容器中的名字
//@Servie注解可以指定生成的bean的名字,默认为第一个字母小写的类名
String[] names = context.getBeanNamesForType(PersonService.class);
//打印对象在容器中的名字
for (String name : names) {
System.out.println("BeanName=" + name);
}
//调用对象的方法
personService.say();
}
}
执行的输出结果
BeanName=personService
Person(id=1, name=张三, age=30)
- ApplicationContext.getBean(classname)用法的说明
-
可以利用
接口
来接受对象,-
如果容器中接口只有一个实现对象的话,
classname
直接可以用接口类
的类名 -
如果接口有多个实现对象的话,
classname
直接可以用``实现类`的,也可以使用
ApplicationContext.getBean(beanname,接口classname)
来获取ApplicationContext context = new AnnotationConfigApplicationContext(MainConfig.class); PersonService service1 = context.getBean("personServiceImpl",PersonService.class); PersonService service2 = context.getBean("person2ServiceImpl",PersonService.class); service1.say(); //Person(id=1, name=张三, age=30) service2.say(); Person(id=2, name=李四, age=20)
-
-
当然也可以用实现类来接受对象
01.06 利用JUnit来测试
public class ConfigTest {
@Test
public void testConfig1() {
ApplicationContext context
= new AnnotationConfigApplicationContext(MainConfig.class);
String[] names = context.getBeanDefinitionNames();
for (String name : names) {
System.out.println(name);
}
}
}
- 执行的输出结果
org.springframework.context.annotation.internalConfigurationAnnotationProcessor
org.springframework.context.annotation.internalAutowiredAnnotationProcessor
org.springframework.context.annotation.internalCommonAnnotationProcessor
org.springframework.context.event.internalEventListenerProcessor
org.springframework.context.event.internalEventListenerFactory
mainConfig
person2ServiceImpl
personServiceImpl