[size=medium][color=red][b]------------了解 Annotation------------------[/b][/color][/size]
[size=small][color=olive] Annotation 中:
时机:不想在xml中配置bean , 则可以给我们的类加上spring组件注解,然后配置下spring的扫描器就可以实现bean 的自动载入。
spring 2.5之后引用了更多典型化注解:
@Component :是所有管理组建的通用形式
@Service :服务专用的
@Controller :控制层
@Repository :DAO层专用
使用annotation 配置步骤:
1、在xml中配置如下:
方式一:不使用默认的filter[/color][/size]
[size=small][color=olive] Annotation 中:
时机:不想在xml中配置bean , 则可以给我们的类加上spring组件注解,然后配置下spring的扫描器就可以实现bean 的自动载入。
spring 2.5之后引用了更多典型化注解:
@Component :是所有管理组建的通用形式
@Service :服务专用的
@Controller :控制层
@Repository :DAO层专用
使用annotation 配置步骤:
1、在xml中配置如下:
方式一:不使用默认的filter[/color][/size]
<?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:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd ">
<!-- 打开spring 的annotation:注释,注解 -->
<context:annotation-config />
<!-- 指定位置 :配置spring的扫描器,用来实现bean的自动载入-->
<!-- 设定:不实用默认的扫描方式 -->
<context:component-scan base-package="com.svse"
use-default-filters="false">
<!-- 扫描符合@Service @Repository的类 expression="" 指定扫描类所在的包位置-->
<context:include-filter type="annotation"
expression="org.springframework.stereotype.Service" />
<context:include-filter type="annotation"
expression="org.springframework.stereotype.Repository" />
<context:include-filter type="annotation"
expression="org.springframework.stereotype.Controller" />
</context:component-scan>
</beans>
方式二:使用默认的filter,并使用spring中的filter资源进行过滤:
<?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:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd ">
<!-- 打开spring 的annotation:注释,注解 -->
<context:annotation-config />
<!-- 指定位置 :配置spring的扫描器,用来实现bean的自动载入-->
<!-- 设定:不实用默认的扫描方式 -->
<context:component-scan base-package="com.svse"
use-default-filters="true">
<context:include-filter type="regex"
expression=".impl.*" />
<context:include-filter type="regex"
expression=".action.*" />
<context:include-filter type="regex"
expression=".dao.*" />
</context:component-scan>
</beans>