Spring(3)--使用注解定义Bean

使用注解定义Bean

@Component("user")
public class UserImpl implements User{}

@Component(“user”) 相当于

除了@Component()Spring还提供了三个特殊注解

  1. @Repository 用于标注Dao类
  2. @Service 用于标注业务类
  3. @Controller 用于标注控制器类

Spring提供了@Autowired注解实现Bean的装配

@Service("userService")
public class UserServiceImpl(){
	@Autowired
	private User u;
}

如果容器中有一个以上相同类型的Bean时 @Autowired就不可以用了 ,所以要用**@Qualifier(“user”)** 这里的user表示上面注解定义的bean

@Service("userService")
public class UserServiceImpl(){
	@Qualifier("user") 
	private User u;
}

加载注解定义的Bean(ApplicationContext.xml文件中 两个注解间用逗号隔开)

<context:component-scan base-package="包名1,包名2"/>

使用java标准注解完成装配
为属性注入bean
@Resource(name=“bean的id”)
@Resource
不写后面括号就是匹配字段名或setter方法

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值