import org.springframework.stereotype.Service;
@Service
public class StudentService {
public void study(){
System.out.println("学生学习Java");
}
}
import org.springframework.stereotype.Service;
@Service
public class TeacherService {
private StudentService studentService;
public TeacherService(StudentService studentService){
this.studentService=studentService;
}
public void teach(){
studentService.study();
}
}
import org.springframework.context.annotation.Bean;
//import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
@Configuration
//@ComponentScan
public class Config {
@Bean(name="student")
public StudentService studentService(){
return new StudentService();
}
@Bean(name="teacher")
public TeacherService teacherService(StudentService studentService){
return new TeacherService(studentService);
}
}
public class StudentService {
public void study(){
System.out.println("学生在学习Java");
}
}
public class TeacherService {
private StudentService studentService;
public StudentService getStudentService() {
return studentService;
}
public void setStudentService(StudentService studentService) {
this.studentService = studentService;
}
public void teach(){
studentService.study();
}
}
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class Config {
@Bean
public StudentService student(){
return new StudentService();
}
@Bean
public TeacherService teacher(){
TeacherService teacherService = new TeacherService();
teacherService.setStudentService(student());
return teacherService;
}
}
import java.util.Iterator;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
public class TestMain {
public static void main(String[] args) {
AnnotationConfigApplicationContext acac = new AnnotationConfigApplicationContext(Config.class);
TeacherService teacher = acac.getBean(TeacherService.class);
teacher.teach();
Iterator<String> i = acac.getBeanFactory().getBeanNamesIterator();
while(i.hasNext()){
System.out.println(i.next());
}
acac.close();
}
}
七月 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
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有