一、注解的作用
组件扫描:Spring能够从classpath下自动扫描、侦测和实例化具有特定注解的组件。
说白了就是通过注解可以实例化类。
二、注解中常用的组件(MVC+Component)
特定组件包括:
-@Component:基本注解,标识了一个受Spring管理的组件
-@Respository:标识持久层组件(相当于Model层)
-@Service:标识服务层(业务层)组件
-@Controller:标识表现层组件(相当于view层,这里用controller表示,实际上view层和controller层本来就可以结合在一起用)
对于扫描到的组件,Spring有默认的命名策略:使用非限定类名,第一个字母小写,也可以在注解中通过value属性值标识组件的名称。
当在组件类上使用了特定的注解以后,还需要在Spring的配置文件中声明<context:component-scan>
-base-package属性指定一个需要扫描的基类包,Spring容器将会扫描这个基类包里及其子包中的所有类
-当需要扫描多个包时,可以使用逗号分隔
-如果仅仅希望扫描特定的类而非基包下的所有类,可使用resource-pattern属性过滤特定的类
三、示例代码
1、持久层(Respository)代码
在工程下创建一个包com.atguigu.spring.beans.annotation.respository。
此包下有一个接口UserRespository,接口代码如下:
package com.atguigu.spring.beans.annotation.respository;
public interface UserRespository {
void save();
}
还有一个类UserRespositoryImpl继承UserRespository,代码如下:
package com.atguigu.spring.beans.annotation.respository;
import org.springframework.stereotype.Repository;
@Repository("userRepository")
public class UserRespositoryImpl implements UserRespository {
@Override
public void save() {
System.out.println("UserRespository Save...");
}
}
这里使用注解Repository,@Repository("userRepository")表示此注解指定的bean的id是userRepository,如果这里不指定bean的id,将默认使用与类名相同,但首字母小写的方式命名。
2、Service层
新建一个包:com.atguigu.spring.beans.annotation.service
在包下新建一个类:UserService,代码实现:
package com.atguigu.spring.beans.annotation.service;
import com.atguigu.spring.beans.annotation.respository.UserRespository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
@Service
public class UserService {
public void add(){
System.out.println("UserService add...");
}
}
3、Controller层
新建一个包:com.atguigu.spring.beans.annotation.controller
在包下新建一个类:UserController,代码实现:
package com.atguigu.spring.beans.annotation.controller;
import com.atguigu.spring.beans.annotation.service.UserService;
import org.springframework.stereotype.Controller;
@Controller
public class UserController {
public void execute(){
System.out.println("UserController execute...");
}
}
4、在resources文件夹下新建一个配置文件:beans-annotation.xml
配置文件的内容如下所示:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd">
<!-- 指定SpringIOC容器扫描的包 -->
<context:component-scan base-package="com.atguigu.spring.beans.annotation" ></context:component-scan>
<!-- 可以通过resource-pattern指定扫描的资源 -->
<!--
<context:component-scan base-package="com.atguigu.spring.beans.annotation"
resource-pattern="respository/*.class"></context:component-scan>
-->
<!--通过exclude-filter子节点指定排除部分表达式的组件-->
<!--
<context:component-scan base-package="com.atguigu.spring.beans.annotation">
<context:exclude-filter type="annotation"
expression="org.springframework.stereotype.Repository"/>
</context:component-scan>
-->
<!--通过include-filter子节点指定包含哪些表达式的组件-->
<!--该子节点需要use-default-filters配合使用-->
<!--
<context:component-scan base-package="com.atguigu.spring.beans.annotation"
use-default-filters="false">
<context:include-filter type="annotation"
expression="org.springframework.stereotype.Repository"/>
</context:component-scan>
-->
<!--
<context:component-scan base-package="com.atguigu.spring.beans.annotation">
<context:exclude-filter type="assignable"
expression="com.atguigu.spring.beans.annotation.respository.UserRespository"/>
</context:component-scan>
-->
<!--
<context:component-scan base-package="com.atguigu.spring.beans.annotation"
use-default-filters="false">
<context:include-filter type="assignable"
expression="com.atguigu.spring.beans.annotation.respository.UserRespository"/>
</context:component-scan>
-->
</beans>
说明:
base-package:指定SpringIOC容器扫描的基类包
resource-pattern:指定扫描的资源,可以指定扫描基类下的具体类,而非全部基类
exclude-filter:通过exclude-filter子节点指定排除部分表达式的组件
include-filter:通过include-filter子节点指定包含哪些表达式的组件,该子节点需要use-default-filters配合使用
此外,exclude-filter和include-filter还可以通过assignable指定类名而非注解名来实现,如
<context:component-scan base-package="com.atguigu.spring.beans.annotation">
<context:exclude-filter type="assignable"
expression="com.atguigu.spring.beans.annotation.respository.UserRespository"/>
</context:component-scan>
<context:component-scan base-package="com.atguigu.spring.beans.annotation"
use-default-filters="false">
<context:include-filter type="assignable"
expression="com.atguigu.spring.beans.annotation.respository.UserRespository"/>
</context:component-scan>
完整代码见: