Spring 使用Annotation完成bean的配置

[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]
	<?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>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值