我们之前都了解过spring基于xml的配置,我们也可以通过配置类来完成基于xml的配置,我们会在下面以一个例子来讲述一下Spring基于java的配置。
首先第一步准备工作:
1)创建一个Dog类
1 packagecom.youzicha.pojo;2
3 public classDog {4 privateString dogName;5
6 publicString getDogName() {7 returndogName;8 }9
10 public voidsetDogName(String dogName) {11 this.dogName =dogName;12 }13
14 @Override15 publicString toString() {16 return "Dog{" +
17 "dogName='" + dogName + '\'' +
18 '}';19 }20 }
2)再创建一个People类
1 packagecom.youzicha.pojo;2
3 public classPeople {4 privateString name;5 privateDog dog;6
7 publicString getName() {8 returnname;9 }10
11 public voidsetName(String name) {12 this.name =name;13 }14
15 publicDog getDog() {16 returndog;17 }18
19 public voidsetDog(Dog dog) {20 this.dog =dog;21 }22
23 @Override24 publicString toString() {25 return "People{" +
26 "name='" + name + '\'' +
27 ", dog=" + dog +
28 '}';29 }30 }
注:我们下面的工作就是通过Spring基于java的配置完成IOC(控制反转)以及DI(依赖注入)
3)新建一个类,通过@Configuration,指定当前类为配置类,这个类等同于spring配置的xml。
1 packagecom.youzicha.config;2
3 importorg.springframework.context.annotation.Configuration;4
5 @Configuration6 public classbeans {7
8 }
4)通过@Bean,把当前方法的返回值,作为bean对象存入Spring的容器当中。
1 packagecom.youzicha.config;2
3 importcom.youzicha.pojo.Dog;4 importcom.youzicha.pojo.People;5 importorg.springframework.context.annotation.Bean;6 importorg.springframework.context.annotation.Configuration;7
8 @Configuration9 public classbeans {10 @Bean(name = "dog")11 //name属性用于指定bean的id,当不写时,默认值为当前方法的方法名。
12 publicDog createDog(){13 Dog dog = newDog();14 dog.setDogName("小白");15 returndog;16 }17 @Bean(name = "people")18 //name属性用于指定bean的id,当不写时,默认值为当前方法的方法名。
19 publicPeople createPeople(Dog dog){20 People people = newPeople();21 people.setName("不好喝的柚子茶");22 people.setDog(dog);23 returnpeople;24 }25 }
注意:当我们使用注解配置方法时,如果方法有参数时,Spring框架会去容器中寻找有没有可用的bean对象,查找方式和@Autowied相同。
5)上面配置默认为单例模式,bean的实例化,通过构造方法进行实例化,再存入Spring容器中。
首先验证单例模式:
测试代码:
1 importcom.youzicha.config.beans;2 importcom.youzicha.pojo.Dog;3 importorg.junit.Test;4 importorg.springframework.context.ApplicationContext;5 importorg.springframework.context.annotation.AnnotationConfigApplicationContext;6
7
8 public classMyTest {9 @Test10 public voidtest(){11 ApplicationContext context = new AnnotationConfigApplicationContext(beans.class);12 Dog dog = context.getBean("dog", Dog.class);13 Dog dog1 = context.getBean("dog", Dog.class);14 System.out.println(dog==dog1);15 }16 }
因为我们没有重写Dog类的equals方法,所以比较的是内存地址。
运行结果:
综上所述,默认是单例模式,如何设置不是单例模式呢,我们就用到一个新的注解@Scope
6)@Scope里面有一个属性为scopeName,我们可以通过赋值来设置,当scopeName属性的值为prototype时,它就不再是单例模式了。
为了让结果更明显,我们在createDog方法上设置@Scope注解的scopeName属性的值为prototype,createPeople则不设置。
准备代码:
1 packagecom.youzicha.config;2
3 importcom.youzicha.pojo.Dog;4 importcom.youzicha.pojo.People;5 importorg.springframework.context.annotation.Bean;6 importorg.springframework.context.annotation.Configuration;7 importorg.springframework.context.annotation.Scope;8
9 @Configuration10 public classbeans {11 @Bean(name = "dog")12 //name属性用于指定bean的id,当不写时,默认值为当前方法的方法名。
13 @Scope(scopeName = "prototype")14 publicDog createDog(){15 Dog dog = newDog();16 dog.setDogName("小白");17 returndog;18 }19 @Bean(name = "people")20 //name属性用于指定bean的id,当不写时,默认值为当前方法的方法名。
21 publicPeople createPeople(Dog dog){22 People people = newPeople();23 people.setName("不好喝的柚子茶");24 people.setDog(dog);25 returnpeople;26 }27 }
测试代码:
1 importcom.youzicha.config.beans;2 importcom.youzicha.pojo.Dog;3 importcom.youzicha.pojo.People;4 importorg.junit.Test;5 importorg.springframework.context.ApplicationContext;6 importorg.springframework.context.annotation.AnnotationConfigApplicationContext;7
8
9 public classMyTest {10 @Test11 public voidtest(){12 ApplicationContext context = new AnnotationConfigApplicationContext(beans.class);13 Dog dog = context.getBean("dog", Dog.class);14 Dog dog1 = context.getBean("dog", Dog.class);15 System.out.println("设置Scope为prototype");16 System.out.println(dog==dog1);17 System.out.println("============================================");18 System.out.println("没有设置Scope属性");19 People people = context.getBean("people", People.class);20 People people1 = context.getBean("people", People.class);21 System.out.println(people==people1);22 }23 }
运行结果: