介绍spring3的注解方式和基本配置

 

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

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
 xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
 xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
    http://www.springframework.org/schema/aop
    http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
    http://www.springframework.org/schema/context    
    http://www.springframework.org/schema/context/spring-context-3.0.xsd
    http://www.springframework.org/schema/tx 
    http://www.springframework.org/schema/tx/spring-tx-3.0.xsd ">
<!-- 使用 annotation的配置 -->
 <context:annotation-config />
 <!-- 使用 annotation 自动注册bean,并检查@Controller, @Service, @Repository注解已被注入 -->
 <context:component-scan base-package="com.at" />
<!-- 配置文件属性容器 -->
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
   <property name="location">
     <value>classpath:jdbc.properties</value>
   </property>
    <property name="fileEncoding">
      <value>UTF-8</value>
    </property>
</bean>

<!-- 配置DataSource数据源 -->
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
 <property name="driverClassName" value="${jdbc.driverClass}" /><!-- com.mysql.jdbc.ReplicationDriver -->
 <property name="Url" value="${jdbc.jdbcUrl}" />
 <property name="username" value="${jdbc.user}" />
 <property name="password" value="${jdbc.password}" />
</bean>
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
         <property name="dataSource" ref="dataSource"></property>
     </bean>
<!-- 配置事务管理器 -->
  <bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
    <property name="dataSource" ref="dataSource"/>
  </bean>
<!-- 采用@Transactional注解方式使用事务   -->
        <!-- <tx:annotation-driven transaction-manager="txManager"/> -->

</beans>

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

 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

云焰

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

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

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

打赏作者

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

抵扣说明:

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

余额充值