这篇文章主要介绍了Spring如何使用注解的方式创建bean,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下

创新互联服务项目包括盂县网站建设、盂县网站制作、盂县网页制作以及盂县网络营销策划等。多年来,我们专注于互联网行业,利用自身积累的技术优势、行业经验、深度合作伙伴关系等,向广大中小型企业、政府机构等提供互联网行业的解决方案,盂县网站推广取得了明显的社会效益与经济效益。目前,我们服务的客户以成都为中心已经辐射到盂县省份的部分城市,未来相信会继续扩大服务区域并继续获得客户的支持与信任!
第一种使用配置类的方式
1、创建一个bean
package com.springbean;
public class Person {
private String name;
private Integer age ;
public Person(String name, Integer age) {
this.name = name;
this.age = age;
}
public void setName(String name) {
this.name = name;
}
public void setAge(Integer age) {
this.age = age;
}
public String getName() {
return name;
}
public Integer getAge() {
return age;
}
@Override
public String toString() {
return "Person{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
}2、创建配置类:
import com.springbean.Person;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class PersonConfig {
@Bean //@Bean("myperson") 这是设置bean的名字
public Person person(){ System.out.println("已经创建实例");
return new Person("张三",20); } }3、测试
import com.spring.config.PersonConfig;
import com.springbean.Person;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
public class ApplicationTest {
public static void main(String[] args) {
ApplicationContext applicationContext = new AnnotationConfigApplicationContext(PersonConfig.class);
Person bean = applicationContext.getBean(Person.class);
System.out.println(bean);
//获取bean的类型,默认是方法名,需要修改就在配置类中@Bean里面加上名字
String[] beanNamesForType = applicationContext.getBeanNamesForType(Person.class);
for (String beanType : beanNamesForType){
System.out.println(beanType);
}
}
}和xml配置文件一样,默认的bean是单例的,如果需要改变为prototype,xml配置文件里是加上scope="prototype",这里PersonConfig配置类中需要加上注解@Scope("prototype")。
介绍一下bean的几种类型的作用域。
不加注解测试:
ApplicationContext applicationContext = new AnnotationConfigApplicationContext(PersonConfig.class);
Person bean = applicationContext.getBean(Person.class);
Person bean2 = applicationContext.getBean(Person.class);
System.out.println(bean==bean2);//打印结果为true加上注解@Scope("prototype")测试:
ApplicationContext applicationContext = new AnnotationConfigApplicationContext(PersonConfig.class);
Person bean = applicationContext.getBean(Person.class);
Person bean2 = applicationContext.getBean(Person.class);
System.out.println(bean==bean2);
//打印结果为fale我们也可以改变单例时ioc加载的时候就创建实例,只要在我们的PersonConfig配置类中加上@Lazy注解,使用懒加载。测试
public class ApplicationTest {
public static void main(String[] args) {
ApplicationContext applicationContext = new AnnotationConfigApplicationContext(PersonConfig.class);
/* Person bean = applicationContext.getBean(Person.class);
Person bean2 = applicationContext.getBean(Person.class);
System.out.println(bean==bean2);*/
/*
String[] beanNamesForType = applicationContext.getBeanNamesForType(Person.class);
for (String beanType : beanNamesForType){
System.out.println(beanType);
}*/
}
}这是时打印栏将不会打印出“已经创建实例”,就实现的单例情况下的懒加载。
第二种使用@import注解的方式
新建一个student类
public class Student {
}在配置类PersonConfig上使用@Import注解,这里面可以传入一个数组,用大括号{}
@Configuration
@Import({Student.class})
public class PersonConfig {测试:
public class DemoTest {
ApplicationContext applicationContext = new AnnotationConfigApplicationContext(PersonConfig.class);
@Test
public void test(){
Student bean = applicationContext.getBean(Student.class);
System.out.println(bean);
}
}打印结果:com.springbean.Student@2c34f934 ,注入成功
还可以在@Import中加入ImportSelector的实现类来实现bean的注入
创建Parent和Teacher类
public class Parent {
}
public class Teacher {
}创建ImportSelector的实现类MyImportSelector,返回需要注入的bean,这里是全类名
public class myImportSelector implements ImportSelector{
@Override
public String[] selectImports(AnnotationMetadata annotationMetadata) {
return new String[]{"com.springbean.Parent","com.springbean.Teacher"};
}
}修改PersonConfig,这里传入实现类MyImportSelector
@Configuration
@Import({Student.class, myImportSelector.class})
public class PersonConfig {测试:
Parent parent = applicationContext.getBean(Parent.class);
Teacher teacher = applicationContext.getBean(Teacher.class);
System.out.println(parent);
System.out.println(teacher);打印结果:
com.springbean.Parent@3b2cf7ab com.springbean.Teacher@2aa5fe93
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持创新互联。