Spring Bean装配:Bean注解实现以及定义

一、spring常用注解
1、常用注解
@Componet:通用注解,可用于任何bean,不推荐使用
@Reposity, @Service,@Controller更有针对性注解
@Reposity:  注解DAO类,即持久层
@Service: 注解Service类,即服务层

@Controller:Controller类,即控制层(MVC)


二、类的自动检测和Bean的注册
为了能够检测这些类病注册到相应的Bean需要 <context:annotation-config/> or  <context:annotation-scan/>
<context:annotation-config/>和<context:annotation-scan/>区别
<context:annotation-config/>:扫描方法和成员变量的注解
<context:annotation-scan/>:扫描整个类的注解,包含<context:annotation-config/>,推荐使用
举例:
<?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.xsd" >

        <context:component-scan base-package="org.example.beanannotation"></context:component-scan>

</beans>


三、定义Bean
Bean名称是由BeanNameGenerator生产的,分为:显示指定,非显示指定,自定义(需要包含无参数的构造器)
举例:
1.非显示
@Controller
public class UserAction extends BaseAction<User>{
……
}
2. 显示
@Service("userService")
public class UserServiceImpl implements UserService {
 ………
 }
3. 自定义

<beans >
        <context:component-scan base-package="org.example
                   name-generator="MyNameGenerator"></context:component-scan>
 </beans>

其他可以参见: http://www.cnblogs.com/xdp-gacl/p/3495887.html



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值