spring基础系列--JavaConfig配置

出处地址:http://www.cnblogs.com/V1haoge/p/7171011.html

  早以前,Spring推荐使用XML的方式来定义Bean及Bean之间的装配规则,但是在Spring3之后,Spring提出的强大的JavaConfig这种类型安全的Bean装配方式,它基于Java代码的灵活性,使得装配的过程也变得及其灵活。

  使用JavaConfig来装配Bean拥有其自己的一套规则,我们在这里来看一看:

1、规则

规则一:@Configuration注解

  我们在定义JavaConfig类时,都会在其上加注@Configuration注解,来表明这是一个配置类,@Configuration注解底层是@Component注解,而且这个注解会被AnnotationConfigApplicationContext来进行加载,AnnotationConfigApplicationContext是ApplicationContext的一个具体实现,代表依据配置注解启动应用上下文。

规则二:@ComponentScan注解

  我们使用JavaConfig的目的是为了实现以前XML配置实现的功能,首先就是组件扫描功能,将我们使用特定注解标注的类统一扫描加载到Spring容器,这一功能就是依靠@ComponentScan注解来实现的,我们可以为其指定位置参数来指定要扫描的包。

规则三:@Bean注解

  使用@Bean注解我们可以实现XML配置中手动配置第三方Bean的功能,这里我们使用方法来定义Bean,并在方法前面加注@Bean注解,表示要将该方法返回的对象加载到Spring容器中,这样就对我们的方法定义带来了一些限制,这些限制包括方法的大概格式:

    1-方法带返回值,且返回类型为你要加载的第三方类类型

    2-方法的名称为默认的Bean的name,如果要自定义Bean的name,可以使用@Bean注解的name属性。

    3-要实现注入只需要将要注入的Bean的类型作为参数,调用该类的带参数的构造器构建这个Bean,或者采用第二种方式:先创建这个类的对象,然后调用该对象的set方法进行注入,以被注入的Bean的方法为参数

规则验证:

首先我们创建几个测试类

针对第一种注入方式:

1-StudentService

复制代码

1 import org.springframework.stereotype.Service;2 3 @Service4 public class StudentService {5     public void study(){6         System.out.println("学生学习Java");7     }8 }

复制代码

2-TeacherService 

复制代码

 1 import org.springframework.stereotype.Service; 2  3 @Service 4 public class TeacherService { 5      6     private StudentService studentService; 7      8     public TeacherService(StudentService studentService){ 9         this.studentService=studentService;10     }11     12     public void teach(){13         studentService.study();14     }15 }

复制代码

3-Config:这是针对第一种注入方式而设,需要在TeacherService 中定义带参数的构造器

复制代码

 1 import org.springframework.context.annotation.Bean; 2 //import org.springframework.context.annotation.ComponentScan; 3 import org.springframework.context.annotation.Configuration; 4  5 @Configuration 6 //@ComponentScan 7 public class Config { 8      9     @Bean(name="student")10     public StudentService studentService(){11         return new StudentService();12     }13     14     @Bean(name="teacher")15     public TeacherService teacherService(StudentService studentService){16         return new TeacherService(studentService);17     }18     19 }

复制代码

针对第二种注入方式:

1-StudentService

复制代码

1 public class StudentService {2     3     public void study(){4         System.out.println("学生在学习Java");5     }6     7 }

复制代码

2-TeacherService

复制代码

 1 public class TeacherService { 2          3     private StudentService studentService; 4  5     public StudentService getStudentService() { 6         return studentService; 7     } 8  9     public void setStudentService(StudentService studentService) {10         this.studentService = studentService;11     }12     13     public void teach(){14         studentService.study();15     }16     17 }

复制代码

3-Config:这是采用第二种注入方式:需要在TeacherService中提供set方法

复制代码

 1 import org.springframework.context.annotation.Bean; 2 import org.springframework.context.annotation.Configuration; 3  4 @Configuration 5 public class Config { 6      7     @Bean 8     public StudentService student(){ 9         return new StudentService();10     }11     12     @Bean13     public TeacherService teacher(){14         TeacherService teacherService = new TeacherService();15         teacherService.setStudentService(student());16         return teacherService;17     }18     19 }

复制代码

4-测试类:TestMain

复制代码

 1 import java.util.Iterator; 2  3 import org.springframework.context.annotation.AnnotationConfigApplicationContext; 4  5 public class TestMain { 6  7     public static void main(String[] args) { 8         AnnotationConfigApplicationContext acac = new AnnotationConfigApplicationContext(Config.class); 9         TeacherService teacher = acac.getBean(TeacherService.class);10         teacher.teach();11         Iterator<String> i = acac.getBeanFactory().getBeanNamesIterator();12         while(i.hasNext()){13             System.out.println(i.next());14         }15         acac.close();16     }17 18 }

复制代码

执行结果:

复制代码

七月 14, 2017 4:10:56 下午 org.springframework.context.annotation.AnnotationConfigApplicationContext prepareRefresh
信息: Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@7e6cbb7a: startup date [Fri Jul 14 16:10:56 CST 2017]; root of context hierarchy
学生学习Java
org.springframework.context.annotation.internalConfigurationAnnotationProcessor
org.springframework.context.annotation.internalAutowiredAnnotationProcessor
org.springframework.context.annotation.internalRequiredAnnotationProcessor
org.springframework.context.annotation.internalCommonAnnotationProcessor
org.springframework.context.event.internalEventListenerProcessor
org.springframework.context.event.internalEventListenerFactory
config
student
teacher
environment
systemProperties
systemEnvironment
org.springframework.context.annotation.ConfigurationClassPostProcessor.importRegistry
messageSource
applicationEventMulticaster
lifecycleProcessor
七月 14, 2017 4:10:59 下午 org.springframework.context.annotation.AnnotationConfigApplicationContext doClose
信息: Closing org.springframework.context.annotation.AnnotationConfigApplicationContext@7e6cbb7a: startup date [Fri Jul 14 16:10:56 CST 2017]; root of context hierarchy

复制代码

  该测试结果中打印出的是Spring上下文中所有加载的Bean的名称(name)。


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章