spring3增加的注解方式
@Service用于标注业务层组件 @Repository用于标注数据访问组件,即DAO组件 @Controller用于标注控制层组件,如Struts中的Action @Component泛指组件,当组件不要好归类时,可以使用这个注解进行标注
注意点如下:
1、可以使用诸如@Service("personDao")修改bean名称,而它默认的是将首字母小写的类名作为<bean>名称 2、若要更改<bean>作用域的话,可以使用@Scope("prototype")注解来修改<bean>作用域
一般使用@Resource注解,而不要使用@Autowired注解 因为@Autowired注解是Spring提供的,而@Resource注解是J2EE提供的 在JDK6中就已经包含@Resource注解了,所以它没有跟Spring紧密耦合
一个配置spring+struts+jdbc的实现实例
spring.xml
dao类
import java.sql.ResultSet; import java.sql.SQLException; import java.util.List;
import javax.annotation.Resource;
import org.apache.commons.lang.StringUtils; import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.jdbc.core.RowMapper; import org.springframework.stereotype.Repository;
import com.at.fee.model.FeeInfo; @Repository public class FeeInfoDao { @Resource private JdbcTemplate jdbcTemplate; public List<FeeInfo> retrieveFeeInfos(){ // List<FeeInfo> listFeeInfo=new ArrayList<FeeInfo>(); String sql ="select * from ts_fee_info t where 1=1 "; List listFeeInfo= jdbcTemplate.query(sql, new FeeInfoMapper()); return listFeeInfo; }
}
service类
import java.util.List;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;
import com.at.fee.dao.FeeInfoDao; import com.at.fee.model.FeeInfo; @Service public class FeeInfoService { @Resource private FeeInfoDao feeInfoDao;//=new FeeInfoDao() public List<FeeInfo> searchFeeInfos(){ List<FeeInfo> listFeeInfo=feeInfoDao.retrieveFeeInfos(); return listFeeInfo; }
}
action类
import java.io.IOException; import java.util.List;
import javax.annotation.Resource; import javax.servlet.http.HttpServletResponse;
import net.sf.json.JSONArray; import net.sf.json.JSONObject;
import org.apache.struts2.ServletActionContext; import org.springframework.context.annotation.Scope; import org.springframework.stereotype.Controller;
import com.at.fee.model.FeeInfo; import com.at.fee.service.FeeInfoService; import com.at.tsframe.base.BaseStruts2Action;
@Scope("prototype") @Controller("feeInfoAction") public class FeeInfoAction extends BaseStruts2Action { private static final long serialVersionUID = -1793059480215383708L; @Resource(name= "feeInfoService") private FeeInfoService feeInfoService;
public String searchFeeInfo() throws IOException{ List<FeeInfo> listFeeInfo=feeInfoService.searchFeeInfos(); JSONObject result=new JSONObject(); JSONArray rows = JSONArray.fromObject(listFeeInfo); result.put("rows", rows); result.put("total", listFeeInfo.size()); String jsonResult = result.toString(); HttpServletResponse response = ServletActionContext.getResponse(); response.getWriter().write(jsonResult); return null; }
}
struts配置
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd ">
<struts>
<!-- struts.devMode : 是否设置为开发模式 true:是开发模式,否则不是 注:在开发模式下,修改Struts的配置文件后不需要重新启动Tomcat服务器即生效。 否则修改Struts配置文件后需要重新启动Tomcat服务器才生效。
--> <constant name="struts.devMode" value="false" /> <!-- 关闭动态方法调用,(关闭action名 + 感叹号 + 方法名进行方法调用,如login!checkLogin.action,调用Action名为login类中的checkLogin方法) --> <constant name="struts.enable.DynamicMethodInvocation" value="false" /> <constant name="struts.configuration.xml.reload" value="true"/> <!-- 该属性设置Struts 2是否支持动态方法调用,该属性的默认值是true。如果需要关闭动态方法调用,则可设置该属性为false --> <constant name="struts.enable.DynamicMethodInvocation" value="true"/> <constant name="struts.objectFactory" value="spring" /> <constant name="struts.action.extension" value="do" /> <constant name="struts.i18n.encoding" value="utf-8"/> <!-- 加载模块 --> <include file="struts_fee.xml"/> <include file="struts_spec.xml"/> </struts>
struts_fee.xml
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd ">
<struts> <package name="fee" extends="struts-default" namespace="/fee"> <action name="searchFeeInfo" class="feeInfoAction" method="searchFeeInfo"> <!-- <result name="success">/feeInfoIndex.jsp</result> --> </action> </package> </struts>
参考:http://www.cnblogs.com/yl2755/archive/2012/05/06/2486752.html