Spring基于java的配置
我们之前都了解过spring基于xml的配置,我们也可以通过配置类来完成基于xml的配置,我们会在下面以一个例子来讲述一下Spring基于java的配置。
首先第一步准备工作:
1)创建一个Dog类
1 package com.youzicha.pojo; 2 3 public class Dog { 4 private String dogName; 5 6 public String getDogName() { 7 return dogName; 8 } 9 10 public void setDogName(String dogName) { 11 this.dogName = dogName; 12 } 13 14 @Override 15 public String toString() { 16 return "Dog{" + 17 "dogName='" + dogName + '\'' + 18 '}'; 19 } 20 }
2)再创建一个People类
1 package com.youzicha.pojo; 2 3 public class People { 4 private String name; 5 private Dog dog; 6 7 public String getName() { 8 return name; 9 } 10 11 public void setName(String name) { 12 this.name = name; 13 } 14 15 public Dog getDog() { 16 return dog; 17 } 18 19 public void setDog(Dog dog) { 20 this.dog = dog; 21 } 22 23 @Override 24 public String toString() { 25 return "People{" + 26 "name='" + name + '\'' + 27 ", dog=" + dog + 28 '}'; 29 } 30 }
注:我们下面的工作就是通过Spring基于java的配置完成IOC(控制反转)以及DI(依赖注入)
3)新建一个类,通过@Configuration,指定当前类为配置类,这个类等同于spring配置的xml。
1 package com.youzicha.config; 2 3 import org.springframework.context.annotation.Configuration; 4 5 @Configuration 6 public class beans { 7 8 }
4)通过@Bean,把当前方法的返回值,作为bean对象存入Spring的容器当中。
1 package com.youzicha.config; 2 3 import com.youzicha.pojo.Dog; 4 import com.youzicha.pojo.People; 5 import org.springframework.context.annotation.Bean; 6 import org.springframework.context.annotation.Configuration; 7 8 @Configuration 9 public class beans { 10 @Bean(name = "dog") 11 //name属性用于指定bean的id,当不写时,默认值为当前方法的方法名。 12 public Dog createDog(){ 13 Dog dog = new Dog(); 14 dog.setDogName("小白"); 15 return dog; 16 } 17 @Bean(name = "people") 18 //name属性用于指定bean的id,当不写时,默认值为当前方法的方法名。 19 public People createPeople(Dog dog){ 20 People people = new People(); 21 people.setName("不好喝的柚子茶"); 22 people.setDog(dog); 23 return people; 24 } 25 }
注意:当我们使用注解配置方法时,如果方法有参数时,Spring框架会去容器中寻找有没有可用的bean对象,查找方式和@Autowied相同。
5)上面配置默认为单例模式,bean的实例化,通过构造方法进行实例化,再存入Spring容器中。
首先验证单例模式:
测试代码:
1 import com.youzicha.config.beans; 2 import com.youzicha.pojo.Dog; 3 import org.junit.Test; 4 import org.springframework.context.ApplicationContext; 5 import org.springframework.context.annotation.AnnotationConfigApplicationContext; 6 7 8 public class MyTest { 9 @Test 10 public void test(){ 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 package com.youzicha.config; 2 3 import com.youzicha.pojo.Dog; 4 import com.youzicha.pojo.People; 5 import org.springframework.context.annotation.Bean; 6 import org.springframework.context.annotation.Configuration; 7 import org.springframework.context.annotation.Scope; 8 9 @Configuration 10 public class beans { 11 @Bean(name = "dog") 12 //name属性用于指定bean的id,当不写时,默认值为当前方法的方法名。 13 @Scope(scopeName = "prototype") 14 public Dog createDog(){ 15 Dog dog = new Dog(); 16 dog.setDogName("小白"); 17 return dog; 18 } 19 @Bean(name = "people") 20 //name属性用于指定bean的id,当不写时,默认值为当前方法的方法名。 21 public People createPeople(Dog dog){ 22 People people = new People(); 23 people.setName("不好喝的柚子茶"); 24 people.setDog(dog); 25 return people; 26 } 27 }
测试代码:
1 import com.youzicha.config.beans; 2 import com.youzicha.pojo.Dog; 3 import com.youzicha.pojo.People; 4 import org.junit.Test; 5 import org.springframework.context.ApplicationContext; 6 import org.springframework.context.annotation.AnnotationConfigApplicationContext; 7 8 9 public class MyTest { 10 @Test 11 public void test(){ 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 }
运行结果: