分享

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

 小傅哥 2021-12-13

spring笔记

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

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

@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("摧毁");
}
}

    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多