Spring如何使用注解的方式创建bean
这篇文章主要介绍了Spring如何使用注解的方式创建bean,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
第一种使用配置类的方式
1、创建一个bean
packagecom.springbean;
publicclassPerson{
privateStringname;
privateIntegerage;
publicPerson(Stringname,Integerage){
this.name=name;
this.age=age;
}
publicvoidsetName(Stringname){
this.name=name;
}
publicvoidsetAge(Integerage){
this.age=age;
}
publicStringgetName(){
returnname;
}
publicIntegergetAge(){
returnage;
}
@Override
publicStringtoString(){
return"Person{"+
"name='"+name+'\''+
",age="+age+
'}';
}
}
2、创建配置类:
importcom.springbean.Person;
importorg.springframework.context.annotation.Bean;
importorg.springframework.context.annotation.Configuration;
@Configuration
publicclassPersonConfig{
@Bean//@Bean("myperson")这是设置bean的名字
publicPersonperson(){System.out.println("已经创建实例");
returnnewPerson("张三",20);}}
3、测试
importcom.spring.config.PersonConfig;
importcom.springbean.Person;
importorg.springframework.context.ApplicationContext;
importorg.springframework.context.annotation.AnnotationConfigApplicationContext;
publicclassApplicationTest{
publicstaticvoidmain(String[]args){
ApplicationContextapplicationContext=newAnnotationConfigApplicationContext(PersonConfig.class);
Personbean=applicationContext.getBean(Person.class);
System.out.println(bean);
//获取bean的类型,默认是方法名,需要修改就在配置类中@Bean里面加上名字
String[]beanNamesForType=applicationContext.getBeanNamesForType(Person.class);
for(StringbeanType:beanNamesForType){
System.out.println(beanType);
}
}
}
和xml配置文件一样,默认的bean是单例的,如果需要改变为prototype,xml配置文件里是加上scope="prototype",这里PersonConfig配置类中需要加上注解@Scope("prototype")。
介绍一下bean的几种类型的作用域。
- singleton:单实例(默认),ioc容器启动时就会创建对象放到ioc容器中,以后每次获取都是直接从ioc容器中获取,ioc容器可以简单理解为map
- prototype:多实例(原型),ioc容器启动并不会去调用方法创建对象,而是每次我们获取对象的时候,才会调用方法去创建。
- requst:同一次请求创建一个实例
- session:同一个session创建一个实例
不加注解测试:
ApplicationContextapplicationContext=newAnnotationConfigApplicationContext(PersonConfig.class); Personbean=applicationContext.getBean(Person.class); Personbean2=applicationContext.getBean(Person.class); System.out.println(bean==bean2);//打印结果为true
加上注解@Scope("prototype")测试:
ApplicationContextapplicationContext=newAnnotationConfigApplicationContext(PersonConfig.class); Personbean=applicationContext.getBean(Person.class); Personbean2=applicationContext.getBean(Person.class); System.out.println(bean==bean2); //打印结果为fale
我们也可以改变单例时ioc加载的时候就创建实例,只要在我们的PersonConfig配置类中加上@Lazy注解,使用懒加载。测试
publicclassApplicationTest{
publicstaticvoidmain(String[]args){
ApplicationContextapplicationContext=newAnnotationConfigApplicationContext(PersonConfig.class);
/*Personbean=applicationContext.getBean(Person.class);
Personbean2=applicationContext.getBean(Person.class);
System.out.println(bean==bean2);*/
/*
String[]beanNamesForType=applicationContext.getBeanNamesForType(Person.class);
for(StringbeanType:beanNamesForType){
System.out.println(beanType);
}*/
}
}
这是时打印栏将不会打印出“已经创建实例”,就实现的单例情况下的懒加载。
第二种使用@import注解的方式
新建一个student类
publicclassStudent{
}
在配置类PersonConfig上使用@Import注解,这里面可以传入一个数组,用大括号{}
@Configuration
@Import({Student.class})
publicclassPersonConfig{
测试:
publicclassDemoTest{
ApplicationContextapplicationContext=newAnnotationConfigApplicationContext(PersonConfig.class);
@Test
publicvoidtest(){
Studentbean=applicationContext.getBean(Student.class);
System.out.println(bean);
}
}
打印结果:com.springbean.Student@2c34f934,注入成功
还可以在@Import中加入ImportSelector的实现类来实现bean的注入
创建Parent和Teacher类
publicclassParent{
}
publicclassTeacher{
}
创建ImportSelector的实现类MyImportSelector,返回需要注入的bean,这里是全类名
publicclassmyImportSelectorimplementsImportSelector{
@Override
publicString[]selectImports(AnnotationMetadataannotationMetadata){
returnnewString[]{"com.springbean.Parent","com.springbean.Teacher"};
}
}
修改PersonConfig,这里传入实现类MyImportSelector
@Configuration
@Import({Student.class,myImportSelector.class})
publicclassPersonConfig{
测试:
Parentparent=applicationContext.getBean(Parent.class); Teacherteacher=applicationContext.getBean(Teacher.class); System.out.println(parent); System.out.println(teacher);
打印结果:
com.springbean.Parent@3b2cf7ab com.springbean.Teacher@2aa5fe93
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。
声明:本文内容来源于网络,版权归原作者所有,内容由互联网用户自发贡献自行上传,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任。如果您发现有涉嫌版权的内容,欢迎发送邮件至:czq8825#qq.com(发邮件时,请将#更换为@)进行举报,并提供相关证据,一经查实,本站将立刻删除涉嫌侵权内容。