@Component
@Component是一个通用的注解,@Repository、@Service和@Controller等都是@Component的特例,在不同的环境下,我们选择用不同的注解,但是都用@Component也是可以的。
- @Repository:用于持久层,DAO,处理与数据库有关的操作
- @Service:服务层、进行一些业务逻辑操作
- @Controller:控制层,主要是spring mvc的操作
被这些注解标注的bean的name默认是类名的首字母小写,其他不变,然后service是可以自定义名字的,用value属性
@Service(value="test")
public class ServiceTest{
//默认的bean的name是serviceTest
//........
}
自动检测
被上述注解标注了以后,最好在配置文件里加入搜索路径。
<context:component-scan base-package="org.exp.*"/>
另外配置了上述配置,默认开启了<context:annotation-config>
我们就不用再加上<context:annotation-config>
了