spring 笔记持续更新2012年9月16日 11:54:45

spring笔记

点击下载立体结构笔记http://download.csdn.net/detail/yao__shun__yu/4608595

点击进入我的个人编程网站,初步建设中http://monkeycode.yy.cn8u.cn/

@Resource注入

1、构造方法注入

2、set注入

1、如果想把@Resource放在 private HelloSpring helloSpring 前面,那么就需要有一个空的构造方法,
否则就会报异常(org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate

public class TAnnotation {
	@Resource private HelloSpring helloSpring;
	/**
	 * 这种注入必须有一个空的构造方法
	 */
	public TAnnotation(){};
	
	public TAnnotation(HelloSpring helloSpring) {
		this.helloSpring = helloSpring;
	}



2、set注入,对应的要有的set方法,把@Resource写在set方法上面

import javax.annotation.Resource;

public class TAnnotation {
	private HelloSpring helloSpring;
	
	public void doShow(){
		helloSpring.doShowMessage();
	}
	
	@Resource 
	public void setHelloSpring(HelloSpring helloSpring) {
		this.helloSpring = helloSpring;
	}
	
}


 


注解

@Service	用于标注业务业务层组件
@Controller	用于标注控制层组件(Struts action)
@Repository	用于标注数据访问组件dao
@Component	用于泛指组件
例子
----
xml
<context:component-scan base-package="com.xiaofu.bean.imp"></context:component-scan>

@Service("personService") @Scope("prototype")
public class PersonServiceBean {

	@PostConstruct
	public void init(){
		System.out.println("初始化");
	}
	
	@PreDestroy
	public void destory(){
		System.out.println("摧毁");
	}
}



 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小傅哥

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值