介绍spring3的注解方式和基本配置
2012-12-09 20:03
375 查看
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> |
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; } } |
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; } } |
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; } } |
<?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> |
<?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> |
相关文章推荐
- Spring学习(二十二) Bean配置的三种方式(XML、注解、Java类)介绍与对比
- Spring学习----------Bean配置的三种方式(XML、注解、Java类)介绍与对比
- spring+sprinmvc+mybatis基本整合(二)--基于spring注解mybais非注解非Mapper接口方式
- 在Spring3 MVC中五步配置集成注解方式Hibernate3
- 05 Spring Aop实例(AOP 如此简单)@Aspect、@Around 注解方式配置
- 使用注解方式配置 AOP 的基本步骤(复习)
- Spring的注解方式和配置文件方式能共存
- 原始方式:spring四种依赖注入方式 |注解方式:Spring零配置通过注解实现Bean依赖注入
- Spring(10)用注解方式不用配置文件方式
- Java程序员从笨鸟到菜鸟之(七十二)细谈Spring(四)利用注解实现spring基本配置详解
- Spring 管理hibernate事物,xml配置,注解 两种实现方式
- 7 -- Spring的基本用法 -- 11... 基于XML Schema的简化配置方式
- Spring3 MVC 注解(一)---注解基本配置及@controller和 @RequestMapping 常用解释
- Spring——容器,注入(配置和注解两种方式)
- spring注解方式,使用jax-ws配置webservice,适合小白。看不会你打死我!
- 04-Spring-使用ioc注解方式配置bean
- spring整合ehcache 注解实现查询缓存,并实现实时缓存更新或删除 写在前面:上一篇博客写了spring cache和ehcache的基本介绍,个人建议先把这些最基本的知识了解了才能对今天
- java注解方式(不用xml)配置web框架spring+SpringMVC
- Spring 3.0.5 MVC 基于注解ehcache.xml 配置方式
- 05 Spring Aop实例(AOP 如此简单)@Aspect、@Around 注解方式配置