Java注释的使用

《Java注释的使用》 author:heguikun 2010-11-9
1.@Resource
使用@Resource后免去了xml的代码,和类的get()  set()方法
如下:
-----在Action中写法
import javax.annotation.Resource;//导入
public class PetAction extends DispatchAction {
 @Resource  private  AllBiz allBiz=null;//找到合适类型注入
public ActionForward doCheckUserExists(ActionMapping mapping,ActionForm form, HttpServletRequest request,
HttpServletResponse response) throws IOException {
   String petName = request.getParameter("petName");//存在隐藏表单中
   List list=allBiz.query("from  PetInfo where petName='"+petName+"'");
   System.out.println(list.size());
return null;
}
-----对于的Sping配置文件
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
  xmlns:context="http://www.springframework.org/schema/context"
 xsi:schemaLocation="http://www.springframework.org/schema/context
          http://www.springframework.org/schema/context/spring-context-2.5.xsd">
 <context:annotation-config /><!--表示打开注解-->
<bean name="/Petdiary" class="com.yourcompany.struts.action.DiaryAction"/><!--他根据这个Action找@Resourc-->
----以上就可以完成装配
如果想在xml中省去不写<bean name="/Petdiary" class="com.yourcompany.struts.action.DiaryAction"/>
请在这个类前加 :@Controller("/Petdiary")   //表示这个类交个Spring管理import org.springframework.stereotype.Controller;
如果在某个类前加上:@Repository("别名") 这个类不需要在xml 出现,Spring也会自己找到并实例化
给需要该对象的类<context:component-scan base-package="com"/>//要在Xml中写这句话,叫它Spring扫描找到@Repository()
 如下:
@Repository("allBiz")
public class AllBizImpl extends HibernateDaoSupport implements AllBiz {
}
xml中就不用出现<bean id="allBiz" class="com.aptech.jb.epet.biz.Impl.AllBizImpl"/>
 但是@Repository("allBiz") 要和 @Resource  等配合使用,要么就不成功
2.
// @Autowired() @Qualifier("allBiz") 配合 跟 @Resource 效果一样 ,required=false意思是空也不抛出异常
                @Autowired(required=false)
   @Qualifier("allBiz")
   private  AllBiz allBiz;//get()set()方法都不用写

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

heguikun

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

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

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

打赏作者

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

抵扣说明:

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

余额充值