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

来源:互联网 发布:扫描ip地址软件 编辑:程序博客网 时间:2024/05/17 01:05

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 {@PostConstructpublic void init(){System.out.println("初始化");}@PreDestroypublic void destory(){System.out.println("摧毁");}}



 

原创粉丝点击