在 classpath 中扫描组件:
组件扫描(component scanning): Spring 能够从 classpath 下自动扫描 , 侦测和实例化具有特定注解的组件。
特定组件包括:
- @Component:基本注解 , 标识了一个受 Spring 管理的组件。
- @Repository:标识持久层组件。
- @Service:标识服务层(业务层)组件。
- @Controller:标识表现层组件。
对于扫描到的组件 , Spring 有默认的命名策略:使用非限定类名 , 第一个字母小写。也可以通过 value 属性值标识组件的名称。
1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns="http://www.springframework.org/schema/beans" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xmlns:context="http://www.springframework.org/schema/context" 5 xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd 6 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> 7 8 <!-- 指定 Spring IOC 容器扫描的包--> 9 <context:component-scan base-package="com.itdjx.spring.annotation"> 10 11 </context:component-scan> 12 13 </beans>
注意:一定要添加 context 命名空间。
1 package com.itdjx.spring.annotation; 2 3 import org.springframework.stereotype.Component; 4 5 /** 6 * @author Wáng Chéng Dá 7 * @create 2017-03-03 8:44 8 */ 9 @Component 10 public class TestObject { 11 }
1 package com.itdjx.spring.annotation.controller; 2 3 import org.springframework.stereotype.Controller; 4 5 /** 6 * @author Wáng Chéng Dá 7 * @create 2017-03-03 9:28 8 */ 9 @Controller 10 public class UserController { 11 12 public void execute() { 13 System.out.println("I am UserController's execute method..."); 14 } 15 }
1 package com.itdjx.spring.annotation.service; 2 3 import org.springframework.stereotype.Service; 4 5 /** 6 * @author Wáng Chéng Dá 7 * @create 2017-03-03 9:26 8 */ 9 @Service 10 public class UserService { 11 12 public void add() { 13 System.out.println("I am UserService's add method..."); 14 } 15 }
1 package com.itdjx.spring.annotation.repository; 2 3 import org.springframework.stereotype.Repository; 4 5 /** 6 * @author Wáng Chéng Dá 7 * @create 2017-03-03 9:24 8 */ 9 @Repository("userRepository") 10 public class UserRepositoryImpl implements UserRepository { 11 12 @Override 13 public void sava() { 14 System.out.println("I am UserRepositoryImpl's sava method..."); 15 } 16 }
1 package com.itdjx.spring.annotation.repository; 2 3 /** 4 * @author Wáng Chéng Dá 5 * @create 2017-03-03 9:23 6 */ 7 public interface UserRepository { 8 9 void sava(); 10 11 }
1 package com.itdjx.spring.annotation; 2 3 import com.itdjx.spring.annotation.controller.UserController; 4 import com.itdjx.spring.annotation.repository.UserRepository; 5 import com.itdjx.spring.annotation.service.UserService; 6 import org.springframework.context.ApplicationContext; 7 import org.springframework.context.support.ClassPathXmlApplicationContext; 8 9 /** 10 * @author Wáng Chéng Dá 11 * @create 2017-03-03 9:34 12 */ 13 public class Main { 14 15 public static void main(String[] args) { 16 ApplicationContext cxt = new ClassPathXmlApplicationContext("beans-annotation.xml"); 17 TestObject testObject = (TestObject) cxt.getBean("testObject"); 18 System.out.println(testObject); 19 20 UserController userController = (UserController) cxt.getBean("userController"); 21 System.out.println(userController); 22 23 UserService userService = (UserService) cxt.getBean("userService"); 24 System.out.println(userService); 25 26 UserRepository userRepository = (UserRepository) cxt.getBean("userRepository"); 27 System.out.println(userRepository); 28 29 } 30 }
控制台输出:
com.itdjx.spring.annotation.TestObject@27ba32 |
注意:Spring 默认的命名策略:使用非限定类名 , 第一个字母小写。也可以通过 value 属性值标识组件的名称。
若不修改 @Repository("userRepository") 默认的 value 值 , bean 的 id 值是 userRepositoryImpl , UserRepository userRepository = (UserRepository) cxt.getBean("userRepository"); 会抛出找不到 userRepository 这个 bean 节点。
异常:
Exception in thread "main" org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'userRepository' available |