Spring教程
程序目录结构:
--Config
--Main
--user
--UserDao
--UserService
--UserController
--item
--ItemDao
--ItemService
--ItemController
1、组件类
package spring.example.anno.user; import org.springframework.stereotype.Repository; @Repository public class UserDao { } package spring.example.anno.user; import org.springframework.stereotype.Service; @Service public class UserService { } package spring.example.anno.user; import org.springframework.stereotype.Controller; @Controller public class UserController { } package spring.example.anno.item; import org.springframework.stereotype.Repository; @Repository public class ItemDao { } package spring.example.anno.item; import org.springframework.stereotype.Service; @Service public class ItemService { } package spring.example.anno.item; import org.springframework.stereotype.Controller; @Controller public class ItemController { }
2、Config类
package spring.example.anno; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; @Configuration @ComponentScan(value = {"spring.example.anno.user", "spring.example.anno.item"}) public class Config { }
3、Main类
package spring.example.anno; import org.springframework.context.ApplicationContext; import org.springframework.context.annotation.AnnotationConfigApplicationContext; public class Main { public static void main(String[] args) { ApplicationContext context=new AnnotationConfigApplicationContext(Config.class); String[] beanDefinitionNames = context.getBeanDefinitionNames(); for(String name:beanDefinitionNames){ System.out.println(name); } } }
输出结果:
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 userController userDao userService itemController itemDao itemService