??xml version="1.0" encoding="utf-8" standalone="yes"?>性日韩欧美在线视频,亚洲欧美日韩成人高清在线一区,国内精品美女av在线播放http://www.aygfsteel.com/huamengxing/category/25304.html专注于javazh-cnSat, 19 Jan 2008 04:58:59 GMTSat, 19 Jan 2008 04:58:59 GMT60c3p0 + spring 配置http://www.aygfsteel.com/huamengxing/archive/2008/01/18/176315.html华梦?/dc:creator>华梦?/author>Fri, 18 Jan 2008 11:47:00 GMThttp://www.aygfsteel.com/huamengxing/archive/2008/01/18/176315.htmlhttp://www.aygfsteel.com/huamengxing/comments/176315.htmlhttp://www.aygfsteel.com/huamengxing/archive/2008/01/18/176315.html#Feedback0http://www.aygfsteel.com/huamengxing/comments/commentRss/176315.htmlhttp://www.aygfsteel.com/huamengxing/services/trackbacks/176315.html很多人都认ؓ比DBCP优秀?br />
<bean id="c3p0DataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
      <property name="driverClass">
        <value>${jdbc.driverClassName}</value>
      </property>
      <property name="jdbcUrl">
        <value>${jdbc.url}</value>
      </property>
      <property name="user">
       <value>${jdbc.username}</value>
      </property>
      <property name="password">
       <value>${jdbc.password}</value>
      </property>
      <property name="initialPoolSize"><value>10</value></property>
      <property name="minPoolSize"><value>5</value></property>
      <property name="maxPoolSize"><value>30</value></property>
      <property name="acquireIncrement"><value>5</value></property>
      <property name="maxIdleTime"><value>10</value></property>
      <property name="maxStatements"><value>0</value></property>
    </bean>

以下几个参数是用的基本配置参数Q?br />initialPoolSizeQ?
Number of Connections a pool will try to acquire upon startup. Should be between minPoolSize and maxPoolSize
q接池初始化时获取的链接敎ͼ介于minPoolSize和maxPoolSize之间

minPoolSizeQ?br />Minimum number of Connections a pool will maintain at any given time.
最链接数

maxPoolSizeQ?br />Maximum number of Connections a pool will maintain at any given time.
最大连接数

acquireIncrementQ?br />Determines how many connections at a time c3p0 will try to acquire when the pool is exhausted.
在当前连接数耗尽的时候,一ơ获取的新的q接?br />
maxIdleTimeQ?br />Seconds a Connection can remain pooled but unused before being discarded. Zero means idle connections never expire.
最大空闲的旉Q单位是U,无用的链接再q时后会被回?br />

]]>
Acegi安全pȝ详解http://www.aygfsteel.com/huamengxing/archive/2008/01/14/175290.html华梦?/dc:creator>华梦?/author>Mon, 14 Jan 2008 12:19:00 GMThttp://www.aygfsteel.com/huamengxing/archive/2008/01/14/175290.htmlhttp://www.aygfsteel.com/huamengxing/comments/175290.htmlhttp://www.aygfsteel.com/huamengxing/archive/2008/01/14/175290.html#Feedback0http://www.aygfsteel.com/huamengxing/comments/commentRss/175290.htmlhttp://www.aygfsteel.com/huamengxing/services/trackbacks/175290.html阅读全文

]]>
Spring 异常控制http://www.aygfsteel.com/huamengxing/archive/2007/09/13/144781.html华梦?/dc:creator>华梦?/author>Thu, 13 Sep 2007 03:23:00 GMThttp://www.aygfsteel.com/huamengxing/archive/2007/09/13/144781.htmlhttp://www.aygfsteel.com/huamengxing/comments/144781.htmlhttp://www.aygfsteel.com/huamengxing/archive/2007/09/13/144781.html#Feedback0http://www.aygfsteel.com/huamengxing/comments/commentRss/144781.htmlhttp://www.aygfsteel.com/huamengxing/services/trackbacks/144781.html异常控制

  Author: 江南白衣

1.业务异常c?/h2>

      1.所以业务异常类z于BusinessException基类?/p>

      2.原则上,要进行相同处理的异常分ؓ一c,用ERROR_CODE标识不同?/p>

      3.出错信息l一写在errors.propertiesQ以ERROR_CODEZ键,支持i18NQ由基类提供默认的getMessage()函数?/p>

      参考BussinessException.java和OrderException.java?/p>

2.Servlet规范里的异常控制

 2.1按error-codel一定义错误面

<error-page>
  <error-code>404</error-code>
  <location>/404.jsp</location>
</error-page>
<error-page>
  <error-code>500</error-code>
  <location>/error.jsp</location>
</error-page>

2.2按异常类型定义单独错误页?/h3>
<error-page>
  <exception-type>org.sprngside.bookstore.UserNotFound</exception-type>
  <location>/userNotFound.jsp</location>
</error-page>

2.3 在JSP里单独定义错误页?/h3>
<@ errorPage="error.jsp">

3.Spring MVC里的异常控制

   spring-mvc可在xxx-serverlet.xml里定义default?按Excepitoncd影射的错误页? 和Servlet规范?主要作了Spring特色的JSP路径转向和日志记?参见bookstore-servlet.xml

  <bean id="exceptionResolver" class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">
        <property name="defaultErrorView" value="/error.jsp"/>
        <property name="exceptionMappings">
            <props>
                <prop key="org.springside.framework.base.BusinessException">/businessError.jsp</prop>
            </props>
        </property>
    </bean>

4. error.jsp的处?/h2>

         error.jsp会同时处理jsp,servlet,和spring抛过来的异常

         其中jsp的异常在exception 变量?

         servlet的异常在(Exception)request.getAttribute("javax.servlet.error.exception")

         spring的异常在(Exception) request.getAttribute("exception")

         使用 (String) request.getAttribute("javax.servlet.error.request_uri")获得 request_uri
         使用 logger.error(exception.getMessage(), exception); 记录整个异常?/p>

]]>FilterChainProxy 详解http://www.aygfsteel.com/huamengxing/archive/2007/09/12/144528.html华梦?/dc:creator>华梦?/author>Wed, 12 Sep 2007 08:20:00 GMThttp://www.aygfsteel.com/huamengxing/archive/2007/09/12/144528.htmlhttp://www.aygfsteel.com/huamengxing/comments/144528.htmlhttp://www.aygfsteel.com/huamengxing/archive/2007/09/12/144528.html#Feedback0http://www.aygfsteel.com/huamengxing/comments/commentRss/144528.htmlhttp://www.aygfsteel.com/huamengxing/services/trackbacks/144528.htmlFilterChainProxy
java.lang.Objectextended by org.acegisecurity.util.FilterChainProxy
All Implemented Interfaces:
Filter, InitializingBean, ApplicationContextAware

public class FilterChainProxy
extends Object
implements Filter, InitializingBean, ApplicationContextAware

Delegates Filter requests to a list of Spring-managed beans.

通过一pd的由spring托管的beans Q代理过滤请求?br />FilterChainProxy 通过在web.xml中定义的FilterToBeanProxy 被加载,FilterChainProxy 会通过init(FilterConfig), destroy() and doFilter(ServletRequest, ServletResponse, FilterChain)调用Q调用每一个在其中定义的过滤器?br />
The FilterChainProxy is loaded via a standard FilterToBeanProxy declaration in web.xml. FilterChainProxy will then pass init(FilterConfig), destroy() and doFilter(ServletRequest, ServletResponse, FilterChain) invocations through to each Filter defined against FilterChainProxy.

 
    FilterChainProxy  是通过一个标准的FilterInvocationDefinitionSource 来实现配|的Q每个可能的FilterChainProxy应该服务的URI模式都必进?br />            W一个搭配的URI模式会被用来定义处理请求的所有的qo器,是说只适配W一l过滤器Q后面的其他qo器将无效?br />FilterChainProxy
is configured using a standard FilterInvocationDefinitionSource. Each possible URI pattern that FilterChainProxy should service must be entered. The first matching URI pattern located by FilterInvocationDefinitionSource for a given request will be used to define all of the Filters that apply to that request. NB: This means you must put most specific URI patterns at the top of the list, and ensure all Filters that should apply for a given URI pattern are entered against the respective entry. The FilterChainProxy will not iterate the remainder of the URI patterns to locate additional Filters. The FilterInvocationDefinitionSource described the applicable URI pattern to fire the filter chain, followed by a list of configuration attributes. Each configuration attribute's ConfigAttribute.getAttribute() corresponds to a bean name that is available from the application context.

FilterChainProxy respects normal handling of Filters that elect not to call Filter.doFilter(javax.servlet.ServletRequest, javax.servlet.ServletResponse, javax.servlet.FilterChain), in that the remainder of the origial or FilterChainProxy-declared filter chain will not be called.

It is particularly noted the Filter lifecycle mismatch between the servlet container and IoC container. As per FilterToBeanProxy JavaDocs, we recommend you allow the IoC container to manage lifecycle instead of the servlet container. By default the FilterToBeanProxy will never call this class' init(FilterConfig) and destroy() methods, meaning each of the filters defined against FilterInvocationDefinitionSource will not be called. If you do need your filters to be initialized and destroyed, please set the lifecycle initialization parameter against the FilterToBeanProxy to specify servlet container lifecycle management.

If a filter name of TOKEN_NONE is used, this allows specification of a filter pattern which should never cause any filters to fire.



]]>
一 Acegi安全pȝ介绍 (1)http://www.aygfsteel.com/huamengxing/archive/2007/09/12/144482.html华梦?/dc:creator>华梦?/author>Wed, 12 Sep 2007 06:42:00 GMThttp://www.aygfsteel.com/huamengxing/archive/2007/09/12/144482.htmlhttp://www.aygfsteel.com/huamengxing/comments/144482.htmlhttp://www.aygfsteel.com/huamengxing/archive/2007/09/12/144482.html#Feedback0http://www.aygfsteel.com/huamengxing/comments/commentRss/144482.htmlhttp://www.aygfsteel.com/huamengxing/services/trackbacks/144482.html一 Acegi安全pȝ介绍 

    Author: cac 差沙

    Acegi是Spring Framework 下最成熟的安全系l,它提供了强大灉|的企业安全服务Q如完善的认证和授权机制QHttp资源讉K控制QMethod 调用讉K控制QAccess Control List (ACL) Z对象实例的访问控ӞYale Central Authentication Service (CAS) 耉单点登陆QX509 认证Q当前所有流行容器的认证适配器,Channel Security频道安全理{功能?/p>

1.1 |站资源

官方|站      http://acegisecurity.sourceforge.net
论坛            http://forum.springframework.org/forumdisplay.php?f=33
Jira              http://opensource.atlassian.com/projects/spring/browse/SEC

1.2 多方面的安全控制_度

  1. URL 资源讉K控制
     http://apps:8080/index.htm -> for public
     http://apps:8080/user.htm -> for authorized user
  2. Ҏ调用讉K控制
    public void getData() -> all user
    public void modifyData() -> supervisor only
  3. 对象实例保护
    order.getValue() < $100 -> all user
    order.getValue() > $100 -> supervisor only

1.3 非入侵式安全架构

  1. ZServlet Filter和Spring aop,  使商业逻辑和安全逻辑分开Q结构更清晰
  2. 使用Spring 来代理对象,能方便地保护Ҏ调用

1.4 其它安全架构

    Acegi只是安全框架之一Q其实还存在其它优秀的安全框架可供选择Q?/p>



]]>
Acegihttp://www.aygfsteel.com/huamengxing/archive/2007/09/12/144479.html华梦?/dc:creator>华梦?/author>Wed, 12 Sep 2007 06:39:00 GMThttp://www.aygfsteel.com/huamengxing/archive/2007/09/12/144479.htmlhttp://www.aygfsteel.com/huamengxing/comments/144479.htmlhttp://www.aygfsteel.com/huamengxing/archive/2007/09/12/144479.html#Feedback0http://www.aygfsteel.com/huamengxing/comments/commentRss/144479.htmlhttp://www.aygfsteel.com/huamengxing/services/trackbacks/144479.htmlhttp://wiki.springside.org.cn/display/springside/Acegi+Reference


http://www.springside.org.cn/docs/reference/Acegi2.htm

http://www.springside.org.cn/docs/reference/Acegi3.htm


http://www.springside.org.cn/docs/reference/Acegi4.htm
http://www.springside.org.cn/docs/reference/Acegi5.htm
http://www.springside.org.cn/docs/reference/Acegi6.htm


]]>
MainTestForJdbcTemplatehttp://www.aygfsteel.com/huamengxing/archive/2007/09/11/144191.html华梦?/dc:creator>华梦?/author>Tue, 11 Sep 2007 03:22:00 GMThttp://www.aygfsteel.com/huamengxing/archive/2007/09/11/144191.htmlhttp://www.aygfsteel.com/huamengxing/comments/144191.htmlhttp://www.aygfsteel.com/huamengxing/archive/2007/09/11/144191.html#Feedback0http://www.aygfsteel.com/huamengxing/comments/commentRss/144191.htmlhttp://www.aygfsteel.com/huamengxing/services/trackbacks/144191.htmlpackage org.springframework.samples;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.ListableBeanFactory;
import org.springframework.beans.factory.generic.GenericBeanFactoryAccessor;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.ColumnMapRowMapper;
import org.springframework.jdbc.core.ConnectionCallback;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.PreparedStatementCallback;
import org.springframework.jdbc.core.PreparedStatementCreator;
import org.springframework.jdbc.core.PreparedStatementSetter;
import org.springframework.jdbc.core.ResultSetExtractor;
import org.springframework.jdbc.core.RowCallbackHandler;
import org.springframework.jdbc.core.RowCountCallbackHandler;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.core.StatementCallback;
import org.springframework.jdbc.support.JdbcUtils;
import org.springframework.samples.petclinic.Person;

/**
 *
 * @author worldheart
 *
 */
public class MainTestForJdbcTemplate {

 private static final Log log = LogFactory.getLog(MainTestForJdbcTemplate.class);
 
 public static void main(String[] args) {  
  ListableBeanFactory cbf = new ClassPathXmlApplicationContext("ac1.xml");  
  GenericBeanFactoryAccessor gbfa = new GenericBeanFactoryAccessor(cbf);
  
  JdbcTemplate jt = gbfa.getBean("jdbcTemplate");
  
  jt.execute(new ConnectionCallback(){
   public Object doInConnection(Connection con) throws SQLException, DataAccessException {
    System.out.println(con.getMetaData().getDriverName());
    return null;
   }
  });
  
  List nameList = (List)jt.execute(new StatementCallback(){
   public Object doInStatement(Statement stmt) throws SQLException, DataAccessException {
    System.out.println(stmt.getConnection().getMetaData().getDatabaseProductVersion());
    List<String> nameList = new ArrayList<String>();
    ResultSet rs = null;
    try{
     rs = stmt.executeQuery("select name from types");
     while(rs.next()){
      nameList.add(rs.getString("name"));
     }
    }finally{
     JdbcUtils.closeResultSet(rs);
    }
    return nameList;
   }
  });
  System.out.println(nameList);
  
  List perList = (List)jt.query("select * from vets",
    new ResultSetExtractor(){
      public Object extractData(ResultSet rs) throws SQLException, DataAccessException {
       List<Person> personList = new ArrayList<Person>();
       while(rs.next()){
        Person per = new Person();
        per.setId(rs.getInt("id"));
        per.setFirstName(rs.getString("first_name"));
        per.setLastName(rs.getString("last_name"));
        personList.add(per);
       }
       return personList;
    }
  });
  for(Iterator iterator = perList.iterator(); iterator.hasNext();){
   Person person = (Person)iterator.next();
   System.out.println(person.getId() + "," + person.getFirstName() + "," + person.getLastName());
  }

  final List<Person> pSonList = new ArrayList<Person>();
  jt.query("select * from vets", new RowCallbackHandler(){
   public void processRow(ResultSet rs) throws SQLException {
    Person per = new Person();
    per.setId(rs.getInt("id"));
    per.setFirstName(rs.getString("first_name"));
    per.setLastName(rs.getString("last_name"));
    pSonList.add(per);
   }
  });
  for(Person pSon: pSonList){
   System.out.println(pSon.getId() + "," + pSon.getFirstName() + "," + pSon.getLastName());
  }
  
  RowCountCallbackHandler rcch = new RowCountCallbackHandler();
  jt.query("select * from vets", rcch);
  for(String colName: rcch.getColumnNames())
   System.out.println(colName);
  for(int colType: rcch.getColumnTypes())
   System.out.println(colType);
  System.out.println(rcch.getColumnCount());
  System.out.println(rcch.getRowCount());
  
  List vetsList = (List)jt.query("select * from vets",
    new RowMapper(){
     public Object mapRow(ResultSet rs, int rowNum) throws SQLException {
      Person pers = new Person();
      pers.setId(rs.getInt("id"));
      pers.setFirstName(rs.getString("first_name"));
      pers.setLastName(rs.getString("last_name"));
      return pers;
     }
  });
  System.out.println(vetsList);
  
  ColumnMapRowMapper cmrw = new ColumnMapRowMapper();
  List vList = (List)jt.query("select * from vets", cmrw);
  System.out.println(vList);
  
  System.out.println(jt.queryForInt("select count(*) from vets where id = ?",
    new Object[]{3}));
      
  jt.execute("update owners set address = 'GuangZhou' where telephone = ?",
    new PreparedStatementCallback(){
     public Object doInPreparedStatement(PreparedStatement ps) throws SQLException, DataAccessException {
      ps.setString(1, "16068008");
      ps.addBatch();
      ps.setString(1, "6085555487");
      ps.addBatch();
      return ps.executeBatch();
     }
  });
  
  System.out.println(jt.query("select address from owners where first_name = ? and last_name = ?",
    new PreparedStatementSetter(){
     public void setValues(PreparedStatement ps) throws SQLException {
      ps.setString(1, "Jeff");
      ps.setString(2, "Black");
     }
    },
    new RowMapper(){
     public Object mapRow(ResultSet rs, int rowNum) throws SQLException {      
      return rs.getString("address");
     }
    }));
  
  System.out.println(jt.execute(
    new PreparedStatementCreator(){
     public PreparedStatement createPreparedStatement(Connection con) throws SQLException {
      return con.prepareStatement("select address from owners");
     }
    },
    new PreparedStatementCallback(){
     public Object doInPreparedStatement(PreparedStatement ps) throws SQLException, DataAccessException {
      List<String> list = new ArrayList<String>();
      ResultSet rs = null;
      try{
       rs = ps.executeQuery();
       while(rs.next()){
        list.add(rs.getString("address"));
       }
      }finally{
       JdbcUtils.closeResultSet(rs);
      }
      return list;
     }
    }));
  
 }

}



]]>
BindStatus APIhttp://www.aygfsteel.com/huamengxing/archive/2007/09/11/144186.html华梦?/dc:creator>华梦?/author>Tue, 11 Sep 2007 03:00:00 GMThttp://www.aygfsteel.com/huamengxing/archive/2007/09/11/144186.htmlhttp://www.aygfsteel.com/huamengxing/comments/144186.htmlhttp://www.aygfsteel.com/huamengxing/archive/2007/09/11/144186.html#Feedback0http://www.aygfsteel.com/huamengxing/comments/commentRss/144186.htmlhttp://www.aygfsteel.com/huamengxing/services/trackbacks/144186.html
public class BindStatus
extends Object
		

Simple adapter to expose the bind status of a field or object. Set as a variable both by the JSP bind tag and Velocity/FreeMarker macros.

  单的适配器获取域或者是对象l定状态, 同样适用于模ѝ?br />Obviously, object status representations (i.e. errors at the object level rather than the field level) do not have an expression and a value but only error codes and messages. For simplicity's sake and to be able to use the same tags and macros, the same status class is used for both scenarios.
昄Q对象的状态表C(例如错误是对象的而不是域U别的)没有表达式或者|而是只有错误码和消息

Method Summary
 int doEndTag ()
           
 void doFinally ()
           
protected  int doStartTagInternal ()
          Called by doStartTag to perform the actual work.
 PropertyEditor getEditor ()
          Retrieve the PropertyEditor for the property that this tag is currently bound to.
 Errors getErrors ()
          Retrieve the Errors instance that this tag is currently bound to.
 String getPath ()
          Return the path that this tag applies to.
 String getProperty ()
          Retrieve the property that this tag is currently bound to, or null if bound to an object rather than a specific property.
 boolean isIgnoreNestedPath ()
          Return whether to ignore a nested path, if any.
 void setIgnoreNestedPath (boolean ignoreNestedPath)
          Set whether to ignore a nested path, if any.
 void setPath (String path)
          Set the path that this tag should apply.


]]>
RowCountCallbackHandler 回调http://www.aygfsteel.com/huamengxing/archive/2007/09/10/143935.html华梦?/dc:creator>华梦?/author>Mon, 10 Sep 2007 03:35:00 GMThttp://www.aygfsteel.com/huamengxing/archive/2007/09/10/143935.htmlhttp://www.aygfsteel.com/huamengxing/comments/143935.htmlhttp://www.aygfsteel.com/huamengxing/archive/2007/09/10/143935.html#Feedback0http://www.aygfsteel.com/huamengxing/comments/commentRss/143935.htmlhttp://www.aygfsteel.com/huamengxing/services/trackbacks/143935.html  jt.query("select * from vets", rcch);
  for(String colName: rcch.getColumnNames())
   System.out.println(colName);
  for(int colType: rcch.getColumnTypes())
   System.out.println(colType);
  System.out.println(rcch.getColumnCount());
  System.out.println(rcch.getRowCount());

]]>
所谓回?/title><link>http://www.aygfsteel.com/huamengxing/archive/2007/09/10/143926.html</link><dc:creator>华梦?/dc:creator><author>华梦?/author><pubDate>Mon, 10 Sep 2007 03:16:00 GMT</pubDate><guid>http://www.aygfsteel.com/huamengxing/archive/2007/09/10/143926.html</guid><wfw:comment>http://www.aygfsteel.com/huamengxing/comments/143926.html</wfw:comment><comments>http://www.aygfsteel.com/huamengxing/archive/2007/09/10/143926.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.aygfsteel.com/huamengxing/comments/commentRss/143926.html</wfw:commentRss><trackback:ping>http://www.aygfsteel.com/huamengxing/services/trackbacks/143926.html</trackback:ping><description><![CDATA[ <p>所谓的回调是在执行statement Aq程中,用到了statement BQ那么先保存以前的执行信息,此时statement B 可以使用Statement A 的内容,然后再执?Statement BQ?Statement B执行完毕后,q回的结果,可以被Statement A 利用Q然后l执行statementA。这是传说中回?/p> <img src ="http://www.aygfsteel.com/huamengxing/aggbug/143926.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.aygfsteel.com/huamengxing/" target="_blank">华梦?/a> 2007-09-10 11:16 <a href="http://www.aygfsteel.com/huamengxing/archive/2007/09/10/143926.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title> PreparedStatementCallback PreparedStatement回调接口定义?/title><link>http://www.aygfsteel.com/huamengxing/archive/2007/09/10/143921.html</link><dc:creator>华梦?/dc:creator><author>华梦?/author><pubDate>Mon, 10 Sep 2007 03:10:00 GMT</pubDate><guid>http://www.aygfsteel.com/huamengxing/archive/2007/09/10/143921.html</guid><wfw:comment>http://www.aygfsteel.com/huamengxing/comments/143921.html</wfw:comment><comments>http://www.aygfsteel.com/huamengxing/archive/2007/09/10/143921.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.aygfsteel.com/huamengxing/comments/commentRss/143921.html</wfw:commentRss><trackback:ping>http://www.aygfsteel.com/huamengxing/services/trackbacks/143921.html</trackback:ping><description><![CDATA[jt.execute("update owners set address = 'GuangZhou' where telephone = ?", <br />    new PreparedStatementCallback(){<br />     public Object doInPreparedStatement(PreparedStatement ps) throws SQLException, DataAccessException {<br />      ps.setString(1, "16068008");<br />      ps.addBatch();<br />      ps.setString(1, "6085555487");<br />      ps.addBatch();<br />      return ps.executeBatch();<br />     }<br />  });<br /><ol><li><p><i><font color="#339900">/**</font></i></p><br /></li><li><i><font color="#339900"> * Copyright: Copyright (c) 2005-2005</font></i><br /></li><li><i><font color="#339900"> * Company: JavaResearch(http://www.javaresearch.org)</font></i><br /></li><li><i><font color="#339900"> */</font></i><br /></li><li><b><font color="#0000ff">package</font></b> org.javaresearch.jerch;<br /></li><li><br /></li><li><b><font color="#0000ff">import</font></b> java.sql.<b><a ><font color="#ff0000"><u>PreparedStatement</u></font></a></b>;<br /></li><li><b><font color="#0000ff">import</font></b> java.sql.<b><a ><font color="#ff0000"><u>SQLException</u></font></a></b>;<br /></li><li><br /></li><li><i><font color="#339900">/**</font></i><br /></li><li><i><font color="#339900"> * PreparedStatement回调接口定义?/font></i><br /></li><li><i><font color="#339900"> * 最后更新日?2005q??8?/font></i><br /></li><li><i><font color="#339900"> * @author cherami</font></i><br /></li><li><i><font color="#339900"> */</font></i><br /></li><li><b><font color="#0000ff">public</font></b><b><font color="#0000ff">interface</font></b><a ><font color="#ff0000"><u>PreparedStatementCallback</u></font></a> {<br /></li><li>  <i><font color="#339900">/**</font></i><br /></li><li><i><font color="#339900">   * 定义PreparedStatement中的执行内容?</font></i><br /></li><li><i><font color="#339900">   * 执行前PreparedStatement的参数等内容都已l设|好了?/font></i><br /></li><li><i><font color="#339900">   * @param stmt PreparedStatement</font></i><br /></li><li><i><font color="#339900">   * @return 执行的结?/font></i><br /></li><li><i><font color="#339900">   * @throws SQLException</font></i><br /></li><li><i><font color="#339900">   */</font></i><br /></li><li>  <b><font color="#0000ff">public</font></b><b><a ><font color="#ff0000"><u>Object</u></font></a></b> doInPreparedStatement(<b><a ><font color="#ff0000"><u>PreparedStatement</u></font></a></b> stmt) <b><font color="#0000ff">throws</font></b><b><a ><font color="#ff0000"><u>SQLException</u></font></a></b>;<br /></li><li>}</li></ol><img src ="http://www.aygfsteel.com/huamengxing/aggbug/143921.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.aygfsteel.com/huamengxing/" target="_blank">华梦?/a> 2007-09-10 11:10 <a href="http://www.aygfsteel.com/huamengxing/archive/2007/09/10/143921.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>取ResultSet中的全部数据的接口定义?/title><link>http://www.aygfsteel.com/huamengxing/archive/2007/09/10/143916.html</link><dc:creator>华梦?/dc:creator><author>华梦?/author><pubDate>Mon, 10 Sep 2007 03:05:00 GMT</pubDate><guid>http://www.aygfsteel.com/huamengxing/archive/2007/09/10/143916.html</guid><wfw:comment>http://www.aygfsteel.com/huamengxing/comments/143916.html</wfw:comment><comments>http://www.aygfsteel.com/huamengxing/archive/2007/09/10/143916.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.aygfsteel.com/huamengxing/comments/commentRss/143916.html</wfw:commentRss><trackback:ping>http://www.aygfsteel.com/huamengxing/services/trackbacks/143916.html</trackback:ping><description><![CDATA[ <ol> <li> <i> <font color="#339900">/**</font> </i> <br /> </li> <li> <i> <font color="#339900"> * Copyright: Copyright (c) 2005-2005</font> </i> <br /> </li> <li> <i> <font color="#339900"> * Company: JavaResearch(http://www.javaresearch.org)</font> </i> <br /> </li> <li> <i> <font color="#339900"> */</font> </i> <br /> </li> <li> <b> <font color="#0000ff">package</font> </b> org.javaresearch.jerch;<br /></li> <li> <br /> </li> <li> <b> <font color="#0000ff">import</font> </b> java.sql.<b><a ><font color="#ff0000"><u>ResultSet</u></font></a></b>;<br /></li> <li> <b> <font color="#0000ff">import</font> </b> java.sql.<b><a ><font color="#ff0000"><u>SQLException</u></font></a></b>;<br /></li> <li> <br /> </li> <li> <i> <font color="#339900">/**</font> </i> <br /> </li> <li> <i> <font color="#339900"> * 提取ResultSet中的全部数据的接口定义?/font> </i> <br /> </li> <li> <i> <font color="#339900"> * 最后更新日?2005q??8?/font> </i> <br /> </li> <li> <i> <font color="#339900"> * @author cherami</font> </i> <br /> </li> <li> <i> <font color="#339900"> */</font> </i> <br /> </li> <li> <b> <font color="#0000ff">public</font> </b> <b> <font color="#0000ff">interface</font> </b> <a > <font color="#ff0000"> <u>ResultSetExtractor</u> </font> </a> {<br /></li> <li>  <i><font color="#339900">/**</font></i><br /></li> <li> <i> <font color="#339900">   * 提取ResultSet中的全部数据?/font> </i> <br /> </li> <li> <i> <font color="#339900">   * @param rs ResultSet</font> </i> <br /> </li> <li> <i> <font color="#339900">   * @return ResultSet中的全部数据</font> </i> <br /> </li> <li> <i> <font color="#339900">   * @throws SQLException</font> </i> <br /> </li> <li> <i> <font color="#339900">   */</font> </i> <br /> </li> <li>  <b><font color="#0000ff">public</font></b><b><a ><font color="#ff0000"><u>Object</u></font></a></b> extractSet(<b><a ><font color="#ff0000"><u>ResultSet</u></font></a></b> rs) <b><font color="#0000ff">throws</font></b><b><a ><font color="#ff0000"><u>SQLException</u></font></a></b>;<br /></li> <li>}<br /></li> </ol> <p>List perList = (List)jt.query("select * from vets", <br />    new ResultSetExtractor(){<br />      public Object extractData(ResultSet rs) throws SQLException, DataAccessException {<br />       List<Person> personList = new ArrayList<Person>();<br />       while(rs.next()){<br />        Person per = new Person();<br />        per.setId(rs.getInt("id"));<br />        per.setFirstName(rs.getString("first_name"));<br />        per.setLastName(rs.getString("last_name"));<br />        personList.add(per);<br />       }<br />       return personList;<br />    }<br />  });</p> <img src ="http://www.aygfsteel.com/huamengxing/aggbug/143916.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.aygfsteel.com/huamengxing/" target="_blank">华梦?/a> 2007-09-10 11:05 <a href="http://www.aygfsteel.com/huamengxing/archive/2007/09/10/143916.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>interface Mappable http://www.aygfsteel.com/huamengxing/archive/2007/09/10/143915.html华梦?/dc:creator>华梦?/author>Mon, 10 Sep 2007 03:00:00 GMThttp://www.aygfsteel.com/huamengxing/archive/2007/09/10/143915.htmlhttp://www.aygfsteel.com/huamengxing/comments/143915.htmlhttp://www.aygfsteel.com/huamengxing/archive/2007/09/10/143915.html#Feedback0http://www.aygfsteel.com/huamengxing/comments/commentRss/143915.htmlhttp://www.aygfsteel.com/huamengxing/services/trackbacks/143915.html
  • public interface Mappable {


  •   /**
  •    * 得到字段对应的填充方法的Ҏ名?/font>
  •    * @param fieldName 数据库表的字D名
  •    * @return q行填充的方法名?/font>
  •    */
  •   publicString getMapMethod(String fieldName);
  •   /**
  •    * 得到字段对应的填充方法的参数cd?/font>
  •    * 数据库返回的值的cd和Java中的可能不是匚w的,或者JavaBean中自己定义ؓ原始cd了,因此需要指定?/font>
  •    * @param fieldName 数据库表的字D名
  •    * @param dbType 数据库返回的cd帔R定义
  •    * @return q行填充的方法的参数的类?/font>
  •    */
  •   publicClass getMethodParameterType(String fieldName,int dbType);
  • }


  • ]]>
    PreparedStatementSetter 讄PreparedStatement所需的全部参数?/title><link>http://www.aygfsteel.com/huamengxing/archive/2007/09/10/143912.html</link><dc:creator>华梦?/dc:creator><author>华梦?/author><pubDate>Mon, 10 Sep 2007 02:54:00 GMT</pubDate><guid>http://www.aygfsteel.com/huamengxing/archive/2007/09/10/143912.html</guid><wfw:comment>http://www.aygfsteel.com/huamengxing/comments/143912.html</wfw:comment><comments>http://www.aygfsteel.com/huamengxing/archive/2007/09/10/143912.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.aygfsteel.com/huamengxing/comments/commentRss/143912.html</wfw:commentRss><trackback:ping>http://www.aygfsteel.com/huamengxing/services/trackbacks/143912.html</trackback:ping><description><![CDATA[ <ol> <li> <p> <i> <font color="#339900">/**</font> </i> </p> <br /> </li> <li> <i> <font color="#339900">* Copyright: Copyright (c) 2005-2005</font> </i> <br /> </li> <li> <i> <font color="#339900">* Company: JavaResearch(http://www.javaresearch.org)</font> </i> <br /> </li> <li> <i> <font color="#339900">*/</font> </i> <br /> </li> <li> <b> <font color="#0000ff">package</font> </b>org.javaresearch.jerch;<br /></li> <li> <br /> </li> <li> <b> <font color="#0000ff">import</font> </b>java.sql.<b><a ><font color="#ff0000"><u>PreparedStatement</u></font></a></b>;<br /></li> <li> <b> <font color="#0000ff">import</font> </b>java.sql.<b><a ><font color="#ff0000"><u>SQLException</u></font></a></b>;<br /></li> <li> <br /> </li> <li> <i> <font color="#339900">/**</font> </i> <br /> </li> <li> <i> <font color="#339900">* 讄PreparedStatement的参数的接口定义?/font> </i> <br /> </li> <li> <i> <font color="#339900">* 最后更新日?2005q??8?/font> </i> <br /> </li> <li> <i> <font color="#339900">* @author cherami</font> </i> <br /> </li> <li> <i> <font color="#339900">*/</font> </i> <br /> </li> <li> <b> <font color="#0000ff">public</font> </b> <b> <font color="#0000ff">interface</font> </b> <a > <font color="#ff0000"> <u>PreparedStatementSetter</u> </font> </a>{<br /></li> <li>  <i><font color="#339900">/**</font></i><br /></li> <li> <i> <font color="#339900">   * 讄PreparedStatement所需的全部参数?/font> </i> <br /> </li> <li> <i> <font color="#339900">   * @param ps PreparedStatement</font> </i> <br /> </li> <li> <i> <font color="#339900">   * @throws SQLException</font> </i> <br /> </li> <li> <i> <font color="#339900">   */</font> </i> <br /> </li> <li>  <b><font color="#0000ff">public</font></b><b><font color="#0000ff">void</font></b> setValues(<b><a ><font color="#ff0000"><u>PreparedStatement</u></font></a></b> ps)  <b><font color="#0000ff">throws</font></b><b><a ><font color="#ff0000"><u>SQLException</u></font></a></b>;<br /></li> <li>} </li> <li> </li> <li>jt.query("select address from owners where first_name = ? and last_name = ?",<br />    new PreparedStatementSetter(){<br />     public void setValues(PreparedStatement ps) throws SQLException {<br />      ps.setString(1, "Jeff");<br />      ps.setString(2, "Black");<br />     }<br />    }, <br />    new RowMapper(){<br />     public Object mapRow(ResultSet rs, int rowNum) throws SQLException {      <br />      return rs.getString("address");<br />     }<br />    }));<br /></li> </ol> <img src ="http://www.aygfsteel.com/huamengxing/aggbug/143912.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.aygfsteel.com/huamengxing/" target="_blank">华梦?/a> 2007-09-10 10:54 <a href="http://www.aygfsteel.com/huamengxing/archive/2007/09/10/143912.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>PreparedStatementCreatorhttp://www.aygfsteel.com/huamengxing/archive/2007/09/10/143900.html华梦?/dc:creator>华梦?/author>Mon, 10 Sep 2007 02:38:00 GMThttp://www.aygfsteel.com/huamengxing/archive/2007/09/10/143900.htmlhttp://www.aygfsteel.com/huamengxing/comments/143900.htmlhttp://www.aygfsteel.com/huamengxing/archive/2007/09/10/143900.html#Feedback0http://www.aygfsteel.com/huamengxing/comments/commentRss/143900.htmlhttp://www.aygfsteel.com/huamengxing/services/trackbacks/143900.html
  • /**


  • * Copyright: Copyright (c) 2005-2005
  • * Company: JavaResearch(http://www.javaresearch.org)
  • */
  • package org.javaresearch.jerch;

  • import java.sql.Connection;
  • import java.sql.PreparedStatement;
  • import java.sql.SQLException;

  • /**
  • * 创徏PreparedStatement的接口定义?/font>
  • * 最后更新日?2005q??8?/font>
  • * @author cherami
  • */
  • public interface PreparedStatementCreator {
  •   /**
  •    * 从数据库q接中创Z个PreparedStatement?/font>
  •    * @param con 数据库连?/font>
  •    * @return 创徏好的PreparedStatement
  •    * @throws SQLException
  •    */
  •   publicPreparedStatement createPreparedStatement(Connection con) throwsSQLException;
  • }
  • 使用实例Q?br />
  • jt  jdbctemplateQ?br />  System.out.println(jt.execute(
        new PreparedStatementCreator(){
         public PreparedStatement createPreparedStatement(Connection con) throws SQLException {
          return con.prepareStatement("select address from owners");
         }
        },
        new PreparedStatementCallback(){
         public Object doInPreparedStatement(PreparedStatement ps) throws SQLException, DataAccessException {
          List<String> list = new ArrayList<String>();
          ResultSet rs = null;
          try{
           rs = ps.executeQuery();
           while(rs.next()){
            list.add(rs.getString("address"));
           }
          }finally{
           JdbcUtils.closeResultSet(rs);
          }
          return list;
         }
        }));


  • ]]>
    Spring JDBC Templatehttp://www.aygfsteel.com/huamengxing/archive/2007/09/06/143234.html华梦?/dc:creator>华梦?/author>Thu, 06 Sep 2007 09:50:00 GMThttp://www.aygfsteel.com/huamengxing/archive/2007/09/06/143234.htmlhttp://www.aygfsteel.com/huamengxing/comments/143234.htmlhttp://www.aygfsteel.com/huamengxing/archive/2007/09/06/143234.html#Feedback0http://www.aygfsteel.com/huamengxing/comments/commentRss/143234.htmlhttp://www.aygfsteel.com/huamengxing/services/trackbacks/143234.html  <property name="driverClassName" value="org.hsqldb.jdbcDriver" />
      <property name="username" value="sa" />
      <property name="password" value="" />
      <property name="url" value="jdbc:hsqldb:hsql://localhost:9001" />
     </bean>
     
     <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
      <property name="dataSource" ref="dataSource"></property>
     </bean>

    ListableBeanFactory cbf = new ClassPathXmlApplicationContext("ac1.xml");
      JdbcTemplate jt = (JdbcTemplate) cbf.getBean("jdbcTemplate");  
      List rsList = jt.queryForList("select * from pets");
      //////////////////// 首先取得list
      for(Iterator iterator = rsList.iterator();iterator.hasNext() ;){
    //////////////////////////////所有的listq代出来
       System.out.println(iterator.next());
      }  
    1. List rows = jdbcTemplate.queryForList("SELECT * FROM USER");   
    2. Iterator it = rows.iterator();   
    3. while(it.hasNext()) {   
    4.     Map userMap = (Map) it.next();   
    5.     System.out.print(userMap.get("user_id") + "\t");   
    6.     System.out.print(userMap.get("name") + "\t");   
    7.     System.out.print(userMap.get("sex") + "\t");   
    8.     System.out.println(userMap.get("age") + "\t");   
    9. }


    ]]>
    MessageSourceResolvablehttp://www.aygfsteel.com/huamengxing/archive/2007/09/05/142934.html华梦?/dc:creator>华梦?/author>Wed, 05 Sep 2007 08:43:00 GMThttp://www.aygfsteel.com/huamengxing/archive/2007/09/05/142934.htmlhttp://www.aygfsteel.com/huamengxing/comments/142934.htmlhttp://www.aygfsteel.com/huamengxing/archive/2007/09/05/142934.html#Feedback0http://www.aygfsteel.com/huamengxing/comments/commentRss/142934.htmlhttp://www.aygfsteel.com/huamengxing/services/trackbacks/142934.html  MessageSourceResolvable msr = new DefaultMessageSourceResolvable(new String[]{"welcome"},
        new Object[]{"默认MessageSourceResolvable实现"});
      System.out.println(aac.getMessage(msr, Locale.CHINA));

     



    ]]>
    Spring提供了MethodNameResolver的三U方式:http://www.aygfsteel.com/huamengxing/archive/2007/08/30/141396.html华梦?/dc:creator>华梦?/author>Thu, 30 Aug 2007 07:21:00 GMThttp://www.aygfsteel.com/huamengxing/archive/2007/08/30/141396.htmlhttp://www.aygfsteel.com/huamengxing/comments/141396.htmlhttp://www.aygfsteel.com/huamengxing/archive/2007/08/30/141396.html#Feedback0http://www.aygfsteel.com/huamengxing/comments/commentRss/141396.htmlhttp://www.aygfsteel.com/huamengxing/services/trackbacks/141396.html
    1 : ParameterMethodNameResolver,q个可以Ҏh的参数来定一个需要调用的Ҏ?br />例如Q?a >http://www.sf.net/index.view?testParam=testItQ这个请求会调用名称为testIt的处理方法?br />
    2 : InternalPathMethodNameResolverQ这个可以根据请求的路径名称来调用相应的Ҏ?br />例如Q?a >http://www.sf.net/testing.view,q个h会调用testingҎ?br />
    3 : PropertiesMethodNameResolverQ这个可以根据一个URLs 映射列表来调用相应的Ҏ?br />例如Q如果定义了/index/welcome.html=doItQ那么当h?index/welcome.htmlӞ会调用doItҎ。在定义URLs时可以用通配W?**/welcom?.html

    ]]>
    Spring 控制?/title><link>http://www.aygfsteel.com/huamengxing/archive/2007/08/30/141312.html</link><dc:creator>华梦?/dc:creator><author>华梦?/author><pubDate>Thu, 30 Aug 2007 04:32:00 GMT</pubDate><guid>http://www.aygfsteel.com/huamengxing/archive/2007/08/30/141312.html</guid><wfw:comment>http://www.aygfsteel.com/huamengxing/comments/141312.html</wfw:comment><comments>http://www.aygfsteel.com/huamengxing/archive/2007/08/30/141312.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.aygfsteel.com/huamengxing/comments/commentRss/141312.html</wfw:commentRss><trackback:ping>http://www.aygfsteel.com/huamengxing/services/trackbacks/141312.html</trackback:ping><description><![CDATA[ <div id="wmqeeuq" class="section" lang="zh-cn"> <div id="wmqeeuq" class="titlepage"> <div> <div> <h2 class="title" style="CLEAR: both"> <a name="mvc-controller"> </a>13.3. 控制?/h2> </div> </div> </div> <p>控制器的概念是MVC设计模式的一部分(切地说Q是MVC中的<span id="wmqeeuq" class="emphasis"><em>C</em></span>Q。应用程序的行ؓ通常被定义ؓ服务接口Q而控制器使得用户可以讉K应用所提供的服务。控制器解析用户输入Qƈ其转换成合理的模型数据Q从而可以进一步由视图展示l用戗Spring以一U抽象的方式实现了控制器概念Q这样得不同类型的控制器可以被创徏。Spring本n包含表单控制器、命令控制器、向导型控制器等多种多样的控制器?/p> <p>Spring控制器架构的基础?code class="interfacename">org.springframework.mvc.Controller</code>接口Q其代码如下Q?/p> <pre class="programlisting">public interface Controller { /** * Process the request and return a ModelAndView object which the DispatcherServlet * will render. */ ModelAndView handleRequest( HttpServletRequest request, HttpServletResponse response) throws Exception; }</pre> <p>你可以发?code class="interfacename">Controller</code>接口仅仅声明了一个方法,它负责处理请求ƈq回合适的模型和视图。Spring MVC实现的基是q三个概念:Mdel、ViewQ?code class="classname">ModelAndView</code>Q以?<code class="interfacename">Controller</code>。虽?code class="interfacename">Controller</code>接口是完全抽象的Q但Spring也提供了许多你可能会用到的控制器。Controller接口仅仅定义了每个控制器都必L供的基本功能Q处理请求ƈq回一个模型和一个视图?/p> <div id="wmqeeuq" class="section" lang="zh-cn"> <div id="wmqeeuq" class="titlepage"> <div> <div> <h3 class="title"> <a name="mvc-controller-abstractcontroller"> </a>13.3.1. <code class="classname">AbstractController</code> ?<code class="classname">WebContentGenerator</code></h3> </div> </div> </div> <p>Z提供一套基设施Q所有的Spring控制器都l承?<code class="classname">AbstractController</code> Q?code class="classname">AbstractController</code> 提供了诸如缓存支持和mimetype讄q样的功能?/p> <div id="wmqeeuq" class="table"> <a name="mvc-controller-abstract-features"> </a> <p class="title"> <b>表?3.3. <code class="classname">AbstractController</code>提供的功?/b> </p> <div id="wmqeeuq" class="table-contents"> <table summary="AbstractController提供的功? border="1"> <colgroup> <col /> <col /> </colgroup> <thead> <tr> <th>功能</th> <th>描述</th> </tr> </thead> <tbody> <tr> <td> <code class="literal">supportedMethods</code> </td> <td>指定q个控制器应该接受什么样的请求方法。通常它被讄成同时支持GET和POSTQ但是你可以选择你想支持的方法。如果控制器不支持请求发送的ҎQ客L会得到通知Q通常是抛Z?code class="classname">ServletException</code>Q?</td> </tr> <tr> <td> <code class="literal">requiresSession</code> </td> <td>指定q个控制器是否需要HTTP session才能正常工作。如果控制器在没有session的情况下接收到请求,客户端会因ؓ抛出<code class="classname">ServletException</code>而得到通知?</td> </tr> <tr> <td> <code class="literal">synchronizeSession</code> </td> <td>指定controller是否同步用户的HTTP session?</td> </tr> <tr> <td> <code class="literal">cacheSeconds</code> </td> <td>指定controller通知客户端对数据内容~存的秒敎ͼ一般ؓ大于零的整数。默认gؓ<span id="wmqeeuq" class="emphasis"><em>-1</em></span>Q即不缓存?</td> </tr> <tr> <td> <code class="literal">useExpiresHeader</code> </td> <td>指定Controller在响应请求时是否兼容HTTP 1.0 <span id="wmqeeuq" class="emphasis"><em>Expires</em></span> header。缺省gؓ<code class="literal">true</code>?</td> </tr> <tr> <td> <code class="literal">useCacheHeader</code> </td> <td>指定Controller在相应请求时是否兼容HTTP 1.1 <span id="wmqeeuq" class="emphasis"><em>Cache-Control</em></span> header。默认gؓ<code class="literal">true</code>?</td> </tr> </tbody> </table> </div> </div> <br class="table-break" /> <p>当从<code class="classname">AbstractController</code>l承Ӟ需要实?code class="literal">handleRequestInternal(HttpServletRequest, HttpServletResponse)</code>抽象ҎQ该Ҏ用来实现自q逻辑Qƈq回一?code class="classname">ModelAndView</code>对象。下面这个简单的例子演示了如何从<code class="classname">AbstractController</code>l承以及如何在applicationContext.xml中进行配|?/p> <pre class="programlisting">package samples; public class SampleController extends AbstractController { public ModelAndView handleRequestInternal( HttpServletRequest request, HttpServletResponse response) throws Exception { ModelAndView mav = new ModelAndView("hello"); mav.addObject("message", "Hello World!"); return mav; } }</pre> <pre class="programlisting"><bean id="sampleController" class="samples.SampleController"> <property name="cacheSeconds" value="120"/> </bean></pre> <p>该controllerq回的ModelAndView使用了硬~码的视囑֐Q尽这样做不好Q,q知客户端将响应数据~存2分钟。除了通过以上方式创徏和配|controller之外Q还需要配|handler mapping(请参?a title="13.4. 处理器映(handler mappingQ? href="mk:@MSITStore:C:\Documents%20and%20Settings\ljl\桌面\spring2.0-reference_final_zh_cn.chm::/ch13s04.html">W?3.4 ?“处理器映射Qhandler mappingQ?/a>)Q这栯controller可以工作了?/p> </div> <div id="wmqeeuq" class="section" lang="zh-cn"> <div id="wmqeeuq" class="titlepage"> <div> <div> <h3 class="title"> <a name="mvc-controller-othersimplecontrollers"> </a>13.3.2. 其它的简单控制器</h3> </div> </div> </div> <p>管可以l承<code class="classname">AbstractController</code>来实现自q控制器,不过Spring提供的众多控制器减轻了我们开发简单MVC应用时的负担?code class="classname">ParameterizableViewController</code>基本上和上面例子中的一P不同的是Q你可以在applicationContext.xml配置中指定返回视囑֐从而避免了在Java代码中的编码?/p> <p> <code class="classname">UrlFilenameViewController</code>会检查URLQ获取文件请求的文g名,q把它作囑֐加以使用。。例如,<code class="literal">http://www.springframework.org/index.html</code>对应的视图文件名?code class="literal">index</code>?/p> </div> <div id="wmqeeuq" class="section" lang="zh-cn"> <div id="wmqeeuq" class="titlepage"> <div> <div> <h3 class="title"> <a name="mvc-controller-multiaction"> </a>13.3.3. <code class="classname">MultiActionController</code></h3> </div> </div> </div> <p> <code class="classname">MultiActionController</code>多个行?action)合ƈ在一个控制器里,q样可以把相兛_能组合在一赗?code class="classname">MultiActionController</code>位于<code class="literal">org.springframework.web.mvc.multiaction</code>包中Q它通过请求映到正确的方法名来调用方法。当在一个控制器存在大量公共的行为,但是有多个调用入口时Q?code class="classname">MultiActionController</code>q别方ѝ?/p> <div id="wmqeeuq" class="table"> <a name="mvc-controller-multiaction-features"> </a> <p class="title"> <b>表?3.4. <code class="classname">MultiActionController</code>提供的功?/b> </p> <div id="wmqeeuq" class="table-contents"> <table summary="MultiActionController提供的功? border="1"> <colgroup> <col /> <col /> </colgroup> <thead> <tr> <th>功能</th> <th>描述</th> </tr> </thead> <tbody> <tr> <td> <code class="literal">delegate</code> </td> <td> <code class="classname">MultiActionController</code>有两U用方式。第一U是你?code class="classname">MultiActionController</code>Qƈ在子cM指定?code class="interfacename">MethodNameResolver</code>解析的方法(q种情况下不需要这个delegate参数Q。第二种是你定义一个代理对象,由它提供<code class="interfacename">MethodNameResolver</code>解析出来的方法(q种情况下,你必M用这个配|参数定义代理对象)?</td> </tr> <tr> <td> <code class="literal">methodNameResolver</code> </td> <td> <code class="classname">MultiActionController</code>需要一U策略,使其可以通过解析h信息来获得要调用的方法。这个解析策略由<code class="interfacename">MethodNameResolver</code>q个接口定义的。这个参数允怽实现<code class="interfacename">MethodNameResolver</code>接口Q然后在控制器中使用你的{略?</td> </tr> </tbody> </table> </div> </div> <br class="table-break" /> <p> <code class="classname">MultiActionController</code>所支持的方法需要符合下列格式:</p> <pre class="programlisting"> <em class="lineannotation"> <span id="wmqeeuq" class="lineannotation">// anyMeaningfulName can be replaced by any methodname</span> </em> public [ModelAndView | Map | void] anyMeaningfulName(HttpServletRequest, HttpServletResponse [, Exception | AnyObject]);</pre> <p>注意Q在?span class="emphasis"><em>不允?/em></span>Ҏ重蝲Q因?code class="classname">MultiActionController</code>无法分L出重载(overloadingQ了的方法。此外,你可以定?span class="emphasis"><em>exception handler</em></span>来处理方法中抛出的异常?</p> <p> <code class="classname">Exception</code> 参数是可选的Q它可以?span class="emphasis"><em>M</em></span>异常Q只要它?code class="classname">java.lang.Exception</code>?code class="classname">java.lang.RuntimeException</code>的子cR?code class="classname">AnyObject</code>参数也是可选的Q它可以?span class="emphasis"><em>M</em></span>对象。HTTP Request中的参数会存在这个对象中Q以便用?/p> <p>下面几个例子C?code class="classname">MultiActionController</code>正确的方法定义?/p> <p>标准格式(?code class="interfacename">Controller</code>接口定义的一??/p> <pre class="programlisting">public ModelAndView doRequest(HttpServletRequest, HttpServletResponse)</pre> <p>下面q个Ҏ支持<code class="classname">Login</code>参数Q?q个参数中包含从h中抽取出来的信息?/p> <pre class="programlisting">public ModelAndView doLogin(HttpServletRequest, HttpServletResponse, Login)</pre> <p>下面q个Ҏ可以处理<code class="classname">Exception</code>?/p> <pre class="programlisting">public ModelAndView processException(HttpServletRequest, HttpServletResponse, IllegalArgumentException)</pre> <p>下面q个Ҏ不返回Q何数倹{?(请参考后面的章节 <a title="13.11. 惯例优先原则(convention over configuration)" href="mk:@MSITStore:C:\Documents%20and%20Settings\ljl\桌面\spring2.0-reference_final_zh_cn.chm::/ch13s11.html">W?3.11 ?“惯例优先原?convention over configuration)?/a>)</p> <pre class="programlisting">public void goHome(HttpServletRequest, HttpServletResponse)</pre> <p>This signature has a <code class="interfacename">Map</code> return type (see the section entitled <a title="13.11. 惯例优先原则(convention over configuration)" href="mk:@MSITStore:C:\Documents%20and%20Settings\ljl\桌面\spring2.0-reference_final_zh_cn.chm::/ch13s11.html">W?3.11 ?“惯例优先原?convention over configuration)?/a> below).</p> <p>下面q个Ҏq回一?code class="interfacename">Map</code>?(请参考后面的章节<a title="13.11. 惯例优先原则(convention over configuration)" href="mk:@MSITStore:C:\Documents%20and%20Settings\ljl\桌面\spring2.0-reference_final_zh_cn.chm::/ch13s11.html">W?3.11 ?“惯例优先原?convention over configuration)?/a>)</p> <pre class="programlisting">public Map doRequest(HttpServletRequest, HttpServletResponse)</pre> <p> <code class="interfacename">MethodNameResolver</code>负责从请求中解析出需要调用的Ҏ名称。下面是Spring中内|的三个<code class="interfacename">MethodNameResolver</code> 实现?/p> <div id="wmqeeuq" class="itemizedlist"> <ul type="disc" compact=""> <li> <p> <code class="classname">ParameterMethodNameResolver</code> - 解析h参数Qƈ它作ؓҎ名?对应<code class="literal">http://www.sf.net/index.view?testParam=testIt</code>的请求,会调?<code class="literal">testIt(HttpServletRequest,HttpServletResponse)</code>Ҏ)。?code class="literal">paramName</code>配置参数Q可以设定要查的参数?/p> </li> <li> <p> <code class="classname">InternalPathMethodNameResolver</code> -从\径中获取文g名作为方法名 (<code class="literal">http://www.sf.net/testing.view</code>的请求会调用<code class="literal">testing(HttpServletRequest,HttpServletResponse)</code>Ҏ?/p> </li> <li> <p> <code class="classname">PropertiesMethodNameResolver</code> - 使用用户自定义的属性对象,请求的URL映射到方法名。当属性中包含<code class="literal">/index/welcome.html=doIt</code>Ӟ发到<code class="literal">/index/welcome.html</code> 的请求会调用<code class="literal">doIt(HttpServletRequest, HttpServletResponse)</code>q个Ҏ?q个Ҏ名解析器可以?code class="literal">PathMatcher</code>一起工作,比如上边那个URL写成<code class="literal">/**/welcom?.html</code>也是可以的?/p> </li> </ul> </div> <p>我们来看一l例子。首先是一个?code class="classname">ParameterMethodNameResolver</code>和代理(delegateQ属性的例子Q它接受包含参数?method"的请求,调用Ҏ<code class="literal">retrieveIndex</code>Q?</p> <pre class="programlisting"><bean id="paramResolver" class="org....mvc.multiaction.ParameterMethodNameResolver"> <property name="paramName" value="method"/> </bean> <bean id="paramMultiController" class="org....mvc.multiaction.MultiActionController"> <property name="methodNameResolver" ref="paramResolver"/> <property name="delegate" ref="sampleDelegate"/> </bean> <bean id="sampleDelegate" class="samples.SampleDelegate"/> <em class="lineannotation"><span id="wmqeeuq" class="lineannotation">## together with</span></em> public class SampleDelegate { public ModelAndView retrieveIndex(HttpServletRequest req, HttpServletResponse resp) { return new ModelAndView("index", "date", new Long(System.currentTimeMillis())); } }</pre> <p>当用上面的代理对象Ӟ我们也可以?code class="classname">PropertiesMethodNameRseolver</code>来匹配一lURLQ将它们映射到我们定义的Ҏ上:</p> <pre class="programlisting"><bean id="propsResolver" class="org....mvc.multiaction.PropertiesMethodNameResolver"> <property name="mappings"> <value> /index/welcome.html=retrieveIndex /**/notwelcome.html=retrieveIndex /*/user?.html=retrieveIndex </value> </property> </bean> <bean id="paramMultiController" class="org....mvc.multiaction.MultiActionController"> <property name="methodNameResolver" ref="propsResolver"/> <property name="delegate" ref="sampleDelegate"/> </bean></pre> </div> <div id="wmqeeuq" class="section" lang="zh-cn"> <div id="wmqeeuq" class="titlepage"> <div> <div> <h3 class="title"> <a name="mvc-controller-command"> </a>13.3.4. 命o控制?/h3> </div> </div> </div> <p>Spring?span class="emphasis"><em>CommandController</em></span>是Spring MVC的重要部分。命令控制器提供了一U和数据对象交互的方式,q动态地来?code class="interfacename">HttpServletRequest</code>的参数绑定到你指定的数据对象上。它的功能和Struts中的<code class="classname">ActionForm</code>有点像,不过在Spring中,你不需要实CQ何接口来实现数据l定。首先,让我们看一下有哪些可以使用的命令控制器Q?/p> <div id="wmqeeuq" class="itemizedlist"> <ul type="disc" compact=""> <li> <p> <code class="classname">AbstractCommandController</code> Q-你可以用该抽象命o控制器来创徏自己的命令控制器Q它能够请求参数绑定到你指定的命o对象。这个类q不提供M表单功能Q但是它提供验证功能Qƈ且让你在子类中去实现如何处理p求参C生的命o对象?</p> </li> <li> <p> <code class="classname">AbstractFormController</code>Q-一个支持表单提交的抽象控制器类。用这个控制器Q你可以定义表单Qƈ使用从控制器获取的数据对象构单。当用户输入表单内容Q?code class="classname">AbstractFormController</code>用戯入的内容l定到命令对象,验证表单内容Qƈ该对象交给控制器,完成相应的操作。它支持的功能有防止重复提交、表单验证以及一般的表单处理程。子c需要实现自qҎ来指定采用哪个视图来昄输入表单Q哪个视图显C单正提交后的结果。如果你需要表单,但不惛_应用上下文中指定昄l用L视图Q就使用q个控制器?</p> </li> <li> <p> <code class="classname">SimpleFormController</code> Q-q是一个form cotnrollerQ当需要根据命令对象来创徏相应的form的时候,该类可以提供更多的支持。你可以为其指定一个命令对象,昄表单的视囑֐Q当表单提交成功后显C给用户的视囑֐{等?</p> </li> <li> <p> <code class="classname">AbstractWizardFormController</code> Q-q是一个抽象类Q承这个类需要实?code class="literal">validatePage()</code>?code class="literal">processFinish()</code> ?code class="literal">processCancel()</code> Ҏ?/p> <p>你有可能也需要写一个构造器Q它臛_需要调?code class="literal">setPages()</code>?code class="literal">setCommandName()</code>Ҏ。setPages()的参数是一个String数组Q这个数l包含了l成向导的视囑֐。setCommandName()的参数是一个StringQ该参数用来在视图中调用你的命令对象?/p> <p>?code class="classname">AbstractFormController</code>的实C? 你需要用命令对象(其实是一个JavaBeanQ?q个bean中包含了表单的信息)。你有两个选择Q在构造函C调用<code class="literal">setCommandClass()</code>ҎQ参数是命o对象的类名)Q或者实?code class="literal">formBackingObject()</code>Ҏ?/p> <p> <code class="classname">AbstractWizardFormController</code> 有几个你可以复写QoverrideQ的Ҏ。最有用的一个是<code class="literal">referenceData(..)</code>。这个方法允怽把模型数据以<code class="interfacename">Map</code>的格式传递给视图Q?code class="literal">getTargetPage()</code> 允许你动态地更改向导的页面顺序,或者直接蟩q某些页面;<code class="literal">onBindAndValidate()</code> 允许你复写内|的l定和验证流E?/p> <p>最后,我们有必要提一?code class="literal">setAllowDirtyBack()</code> ?code class="literal">setAllowDirtyForward()</code>两个Ҏ?你可以在<code class="literal">getTargetPage()</code>中调用这两个ҎQ这两个Ҏ决定在当前面验证p|Ӟ是否允许向导前移或后退?/p> <p> <code class="classname">AbstractWizardFormController</code>的更详细内容请参考JavaDoc。在Spring附带的例子jPetStore中,有一个关于向导实现的例子Q?<code class="classname">org.springframework.samples.jpetstore.web.spring.OrderFormController</code>?/p> </li> </ul> </div> </div> </div> <img src ="http://www.aygfsteel.com/huamengxing/aggbug/141312.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.aygfsteel.com/huamengxing/" target="_blank">华梦?/a> 2007-08-30 12:32 <a href="http://www.aygfsteel.com/huamengxing/archive/2007/08/30/141312.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>视图解析?/title><link>http://www.aygfsteel.com/huamengxing/archive/2007/08/30/141185.html</link><dc:creator>华梦?/dc:creator><author>华梦?/author><pubDate>Thu, 30 Aug 2007 02:01:00 GMT</pubDate><guid>http://www.aygfsteel.com/huamengxing/archive/2007/08/30/141185.html</guid><wfw:comment>http://www.aygfsteel.com/huamengxing/comments/141185.html</wfw:comment><comments>http://www.aygfsteel.com/huamengxing/archive/2007/08/30/141185.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.aygfsteel.com/huamengxing/comments/commentRss/141185.html</wfw:commentRss><trackback:ping>http://www.aygfsteel.com/huamengxing/services/trackbacks/141185.html</trackback:ping><description><![CDATA[视图解析? <div></div><p>p和Spring集成的其他表现层技术一P对于JSP面你需要一个视图解析器来解析。最常用的JSP视图解析器是<tt class="literal">InternalResourceViewResolver</tt>?tt class="literal">ResourceBundleViewResolver</tt>。它们被定义在WebApplicationContext里:</p><pre class="programlisting"># The ResourceBundleViewResolver: <bean id="viewResolver" class="org.springframework.web.servlet.view.ResourceBundleViewResolver"> <property name="basename"><value>views</value></property> </bean> # And a sample properties file is uses (views.properties in WEB-INF/classes): welcome.class=org.springframework.web.servlet.view.JstlView welcome.url=/WEB-INF/jsp/welcome.jsp productList.class=org.springframework.web.servlet.view.JstlView productList.url=/WEB-INF/jsp/productlist.jsp</pre><p>你可以看到ResourceBundleViewResolver需要一个属性文件来把视囑֐U映到 1)cd 2) URL?通过ResolverBundleViewResolverQ你可以用一个解析器来解析两U类型的视图?/p><pre class="programlisting"><bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="viewClass"><value>org.springframework.web.servlet.view.JstlView</value></property> <property name="prefix"><value>/WEB-INF/jsp/</value></property> <property name="suffix"><value>.jsp</value></property> </bean></pre><p>InternalResourceBundleViewResolver可以配置成用JSP面。作为好的实现方式,强烈推荐你将JSP文g攑֜WEB-INF下的一个目录中Q这样客L׃会直接访问到它们?/p><img src ="http://www.aygfsteel.com/huamengxing/aggbug/141185.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.aygfsteel.com/huamengxing/" target="_blank">华梦?/a> 2007-08-30 10:01 <a href="http://www.aygfsteel.com/huamengxing/archive/2007/08/30/141185.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>spring2.0 Bean xsdhttp://www.aygfsteel.com/huamengxing/archive/2007/08/30/141164.html华梦?/dc:creator>华梦?/author>Thu, 30 Aug 2007 01:24:00 GMThttp://www.aygfsteel.com/huamengxing/archive/2007/08/30/141164.htmlhttp://www.aygfsteel.com/huamengxing/comments/141164.htmlhttp://www.aygfsteel.com/huamengxing/archive/2007/08/30/141164.html#Feedback0http://www.aygfsteel.com/huamengxing/comments/commentRss/141164.htmlhttp://www.aygfsteel.com/huamengxing/services/trackbacks/141164.html阅读全文

    ]]>
    Spring2.0 可扩展XML配置初探 http://www.aygfsteel.com/huamengxing/archive/2007/08/29/140925.html华梦?/dc:creator>华梦?/author>Wed, 29 Aug 2007 07:08:00 GMThttp://www.aygfsteel.com/huamengxing/archive/2007/08/29/140925.htmlhttp://www.aygfsteel.com/huamengxing/comments/140925.htmlhttp://www.aygfsteel.com/huamengxing/archive/2007/08/29/140925.html#Feedback0http://www.aygfsteel.com/huamengxing/comments/commentRss/140925.htmlhttp://www.aygfsteel.com/huamengxing/services/trackbacks/140925.html 2007-07-18 Spring的事务通知   
    关键? Spring       

    Spring 2.0版本支持扩展XML配置Q着实兴奋了一下,在我看来QSpring作ؓ目前最行的框Ӟ不能扩展用户自定义的配置Q实在是Spring的一个很不爽的地方,<bean />的方式用h比较通用Qv码到目前为止W合大部分h的用习惯,q且能完成Spring所有的配置操作,但是对于W三方的提供商或则会l常扩展Spring功能的开发者来?使用<bean />q样的配|方式或怸是他们最惌的,他们需要ɾlg的配|更加直观、易阅读、易扩展……试想用下面的配置方式

    代码
    1. <mytag:datasource id= "datasource"     
    2.        databaseType= "oracle"     
    3.        ip= "192.168.1.110"     
    4.        port= "1521"     
    5.        databaseName= "myDB"   
    6.        userName= "admin"   
    7.        password= "password"   
    8.        />   
    9.   
    10. <mytag:ehCache id= "ehcache"     
    11.        cache= "true"     
    12.        maxElements= "100000"     
    13.        timeToIdleSeconds= "120"     
    14.        timeToLiveSeconds= "120"   
    15.        overflowToDisk= "true"   
    16.        />   

    上面的代码中配置了两个组Ӟdatasource和cachelgQ相比普通的bean&propertiy方式,很显Ӟq种配置方式更直观,更易读,更重要的是,如果作ؓlg发布Q用者也可以很方便快L开始用,而不需要关心组件的M实现l节?

    扩展XML配置大致需要一下几个步?
    1、创Z个需要扩展的lg
    2、定义一个xsd文g描述lg内容
    3、创Z个文Ӟ实现BeanDefinitionParser接口Q用来解析xsd文g中的定义和组件定?
    4、创Z个Handler文gQ扩展自NamespaceHandlerSupportQ目的是组件注册到Spring容器
    5、编写spring.handlers和spring.schemas文g

    提供一个简单的例子Q来说明如何L展一个Spring配置Q需求如下:使用自定义标{֮义一个简单的beanQ这个bean有一个或多个属性,标签定义完成后,可以在其他项目中用自定义标签来定义该bean?/p>

    首先Q创Z个需要扩展的lgQ在q里只是一个简单的beanQ而且q个bean只有一个属性age.
    One.java

    代码
    1. package com.mysite.tag;   
    2.   
    3. public class One {   
    4.     private String age;   
    5.        
    6.     public One(){   
    7.            
    8.     }   
    9.   
    10.     public String getAge() {   
    11.         return age;   
    12.     }   
    13.   
    14.     public void setAge(String age) {   
    15.         this.age = age;   
    16.     }   
    17. }   

    然后Q徏立一个xsd文gQ来描述q个bean
    mytag.xsd

    代码
    1. <?xml version="1.0" encoding="UTF-8"?>  
    2. <xsd:schema xmlns="http://www.mysite.org/schema/mytag"  
    3.         xmlns:xsd="http://www.w3.org/2001/XMLSchema"  
    4.         xmlns:beans="http://www.springframework.org/schema/beans"  
    5.         targetNamespace="http://www.mysite.org/schema/mytag"  
    6.         elementFormDefault="qualified"  
    7.         attributeFormDefault="unqualified">  
    8.        
    9.     <xsd:import namespace="http://www.springframework.org/schema/beans"/>  
    10.        
    11.     <xsd:element name="one">  
    12.         <xsd:complexType>  
    13.             <xsd:complexContent>  
    14.                 <xsd:extension base="beans:identifiedType">  
    15.                     <xsd:attribute name="age" type="xsd:string" default="99999"/>  
    16.                 </xsd:extension>  
    17.             </xsd:complexContent>  
    18.         </xsd:complexType>  
    19.     </xsd:element>  
    20. </xsd:schema>  

    在上面的xsd文g描述了一个新的targetNamespaceQƈ在这个空间中定义了一个name为one的element,one有一个age属性,cd为stringQ默认gؓ99999.xsd文g是xml DTD的替代者,使用XML Schema语言q行~写Q这里对xsd schema不做太多解释Q有兴趣可以参考相关的资料?

    创徏一个Java文gQ该文g实现了BeanDefinitionParser接口Q用来解析xsd文g中的定义q注册到lg中?
    MyBeanDefinitionParser.java

    代码
    1. package com.mysite.tag;   
    2.   
    3. import org.springframework.beans.factory.config.BeanDefinition;   
    4. import org.springframework.beans.factory.config.BeanDefinitionHolder;   
    5. import org.springframework.beans.factory.support.BeanDefinitionReaderUtils;   
    6. import org.springframework.beans.factory.support.RootBeanDefinition;   
    7. import org.springframework.beans.factory.xml.BeanDefinitionParser;   
    8. import org.springframework.beans.factory.xml.ParserContext;   
    9. import org.w3c.dom.Element;   
    10.   
    11. public class MyBeanDefinitionParser implements BeanDefinitionParser{   
    12.     public BeanDefinition parse(Element arg0, ParserContext arg1) {   
    13.         RootBeanDefinition def = new RootBeanDefinition();   
    14.                 //讄Bean Class   
    15.         def.setBeanClass(One.class);   
    16.            
    17.                 //注册ID属?  
    18.         String id = arg0.getAttribute("id");   
    19.         BeanDefinitionHolder idHolder= new BeanDefinitionHolder(def,id);   
    20.         BeanDefinitionReaderUtils.registerBeanDefinition(idHolder, arg1.getRegistry());   
    21.            
    22.                 //注册age属?  
    23.         String age = arg0.getAttribute("age");   
    24.         BeanDefinitionHolder ageHolder= new BeanDefinitionHolder(def,age);   
    25.         BeanDefinitionReaderUtils.registerBeanDefinition(ageHolder, arg1.getRegistry());   
    26.         def.getPropertyValues().addPropertyValue("age", age);   
    27.            
    28.         return def;   
    29.     }   
    30. }   
    31.   

    上面的代码仅仅实C一个方法public BeanDefinition parse(Element arg0, ParserContext arg1)Q设|相关的Bean ClassQ解析了对应的xsd文gQƈ解析的内容注册C下文中,同时q回一个BeanDefinition对象QBeanDefinition是Spring的bean定义Q提供了bean部分的操作方法,如isSingleton()、isLazyInit(){??

    注意Qid属性是一个默认的属性,可以不在xsd文g中描qͼ但是需要注册它Q否则将无法通过getBeanҎ获取标签定义的beanQ也无法被其他bean引用?
    另外Q下面代码是lbean的属性赋|q个是不可缺的Q否则在使用标签定义时将无法获取bean属性的倹{?

    代码
    1. def.getPropertyValues().addPropertyValue("age", age);   

    然后为组件编写一个Handler文gQ扩展自NamespaceHandlerSupportQ它的作用是组件注册到Spring容器
    MyNameSpaceHandler.java

    代码
    1. package com.mysite.tag;   
    2.   
    3. import org.springframework.beans.factory.xml.NamespaceHandlerSupport;   
    4.   
    5. public class MyNameSpaceHandler extends NamespaceHandlerSupport {   
    6.   
    7.     public void init() {   
    8.          registerBeanDefinitionParser("one",new MyBeanDefinitionParser());   
    9.     }   
    10. }   

    实际执行的过E只有一句代码,注册了一个名字ؓone的扩展配|,包含了MyBeanDefinitionParser所parser相关xsd的内宏V?

    Cq里Q一个Spring扩展标签已经完成Q但是我们目前还没办法用它QSpring没那么聪明,它无法知道我们在什么地方定义了哪些扩展标签Q这些标{ְ被谁解析Q这个过E要我们通过一些文件来告知Spring知道Q它们就是spring.handlers和spring.schemasQ它们放在META-INF目录中,Spring.jar的META-INF目录中也有同名的文gQ它们的文g内容基本上是怼的,而Spring在执行过E中Q如果发现其他jar文g的META-INF文g夹中包含有这两个文gQSpring会合ƈ它们?
    spring.schemas

    代码
    1. http\://www.mysite.org/schema/mytag.xsd=com/mysite/tag/mytag.xsd   

    spring.handlers

    代码
    1. http\://www.mysite.org/schema/mytag=com.mysite.tag.MyNameSpaceHandler   

    spring.schemas告诉Spring配置文g知道Q如果在配置中引用http://www.mysite.org/schema/mytag.xsdQ它应该M么地Ҏ相应的xsd文g?
    而spring.handlers文g告诉SpringQ应该用哪个Handler注册扩展标签?/p>

    现在为止Q一个完整的xml扩展标签全部完成Q做一个小应用试一下?
    整个项目打包成jar文gQ别忘记META-INF内的两个文gQ,然后新徏一个项目,引用刚才打包的jar文gQƈ引用Spring相关文g?
    需要注?自定义xml扩展配置只有xsd方式的引用才可以使用.
    application.xml

    代码
    1. <?xml version="1.0" encoding="UTF-8"?>  
    2. <beans xmlns="http://www.springframework.org/schema/beans"  
    3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
    4.     xmlns:tag="http://www.mysite.org/schema/mytag"  
    5.     xsi:schemaLocation="http://www.springframework.org/schema/beans   
    6.     http://www.springframework.org/schema/beans/spring-beans-2.0.xsd   
    7.     http://www.mysite.org/schema/mytag   
    8.     http://www.mysite.org/schema/mytag.xsd">  
    9.   
    10.     <tag:one id="oneBean" age="99"/>  
    11. </beans>  

    在xml文g引用上可以看?配置了一个名字ؓtag的名U空?目标为http://www.mysite.org/schema/mytag命名I间,q个目标名称I间必须是存在于目的引用中?mytag.xsd中所定义?.
    代码
    1. <tag:one id="oneBean" age="99"/>   

    上面定义了一个id为oneBean的组Ӟ使用了“one”扩展标{,也就是我们在handler中所注册的,lgage属性的gؓ99?

    Main.java

    代码
    1. package com.test;   
    2.   
    3. import org.springframework.context.ApplicationContext;   
    4. import org.springframework.context.support.ClassPathXmlApplicationContext;   
    5.   
    6. import com.mysite.tag.One;   
    7.   
    8. public class Main {   
    9.   
    10.     public static void main(String[] args) {   
    11.         ApplicationContext ctx = new ClassPathXmlApplicationContext("application.xml");   
    12.         One tag = (One) ctx.getBean("oneBean");   
    13.         System.out.println("oneBean age is "+tag.getAge());   
    14.     }   
    15. }   

    q行试E序Q结果如?
    代码
    1. oneBean age is 99  

    Spring的xml扩展是一个非常有用的Ҏ,在Spring2.0的版本中也提供了一些新的标{用,?lt;aop>,<tx>{?但就目前来讲受大家关注程度ƈ不高Q我惛_部分使用Spring的开发h员都在用Spring开发企业应用,而Spring所提供?lt;bean />定义的方式对于开发h员来说已l能够满需要,但是如果看的更远一些,在Spring以后的发展过E中Qxml扩展应该会成为spring的核心特性之一Q或许到时大家会习惯q样的方式来~写Spring配置文g?

    代码
    1. <XXCompany:XXXModule id="">   
    2. ...   
    3. ...   

    最后更斎ͼ2007-07-18 16:03
    15:42  |   怹链接  |   览 (481)  |   评论 (2)  |    收藏  |   q入论坛  |  
    评论    ?2 ? 发表评论
    baallee     2007-07-18 15:53

    代码
    1. <? xml   version = "1.0"   encoding = "UTF-8" ?>     
    2. < beans   xmlns = "http://www.springframework.org/schema/beans"        
    3. xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance"        
    4. xmlns:p = "http://www.springframework.org/schema/p"        
    5. xsi:schemaLocation ="http://www.springframework.org/schema/beans           
    6. http://www.springframework.org/schema/beans/spring-beans.xsd" >      
    7. < bean   class = "com.interface21.spring2.ioc.Person"         
    8. p:name = "Tony"        
    9. p:age = "53"         
    10. p:house-ref = "number10"   />       
    11. < bean   class = "com.interface21.spring2.ioc.House"         
    12. id = "number10"        
    13. p:name = "10 Downing Street" />      
    14. </ beans >   

    对于单的应用可以用p标签好了?
    自定意扩展应该用在更多的高应用?/p>



    ]]>
    spring中dispatcherservlet的运行机?/title><link>http://www.aygfsteel.com/huamengxing/archive/2007/08/29/140843.html</link><dc:creator>华梦?/dc:creator><author>华梦?/author><pubDate>Wed, 29 Aug 2007 04:25:00 GMT</pubDate><guid>http://www.aygfsteel.com/huamengxing/archive/2007/08/29/140843.html</guid><wfw:comment>http://www.aygfsteel.com/huamengxing/comments/140843.html</wfw:comment><comments>http://www.aygfsteel.com/huamengxing/archive/2007/08/29/140843.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.aygfsteel.com/huamengxing/comments/commentRss/140843.html</wfw:commentRss><trackback:ping>http://www.aygfsteel.com/huamengxing/services/trackbacks/140843.html</trackback:ping><description><![CDATA[spring中dispatcherservlet的运行机?br /><br />dispatcherservlet是spring的web框架(以下Uspringweb)中的核心servlet."spring的web框架——象其它web框架一样——是一个请求驱动的web框架Q其设计围绕一个能请求分发到控制器的servletQ它也提供其它功能帮助web应用开发?----《spring framework 开发参考手册(中文版)》而在springweb框架中这个servlet是org.springframework.web.servlet.dispatcherservlet。这个servlet的承关pd下图所C:springweb首先传l的httpservlet抽象cd装成了beanQframeworkservlet抽象Zweb框架中的servlets的一些基本行为,比如对application context的访问;dispatcherservlet的主要工作就是将一个request分发C个合适的处理器上Qƈ处理返回的modelandviewl制出来q回l客L?br /><br />dispatcherservlet作ؓ一个servlet那他一定有两个主要的方法:init()和doservice()?br /><br />一 init()初始化。dispatcherservletl承体系中init()Ҏ的实C于httpservletbean中,httpservletbean首先调用initbeanwrapper()Q初始化beanwrapperQ然后调用抽象方法initservletbean()Q这个方法的实现位于他的子类frameworkservlet中;framewordservlet中的initservletbean()Ҏ调用initwebapplicationcontext()Q初始化webapplicationcontextQ然后同栯用他的抽象方法initframeworkservlet()Q而这个抽象方法的实现位于最l的dispatcherservlet中;dispatcherservlet中的initframeworkservet()依ơ初始化multiparQ用作文件上传)解析器、本地化信息解析器、主题解析器处理器映等{内宏V所以dispatcherservlet的初始化序为init();initbeanwrapper();<br /><br />initservletbeaninitwebapplicationcontext()<br /><br />initframework();initmultipartresolver();initlocaleresolver();initthemeresolver();inithandlermappings();inithandleradapters();inithandlerexceptionresolvers();initviewresolvers();<br /><br />?doservice()处理h。dispatcherservlet中无论是通过post方式q是get方式提交的requestQ最l都会交由doservice()处理。doservice()中的处理逻辑大致分以下六个步骤:1.if(request是multipart,x件上? 则将request解析q包装成multiparthttpservletrequest2.mappedhandler = gethandler(request)Ҏrequest得到相应的处理器3.调用注册的所有拦截器的prehandleҎ4.调用处理器  ?handleradapter ha = new gethandleradapter(mappedhandler.gethandler());    modelandview mv = ha.handle(req, res, mappedhandler.gethandler())//q里使用了adapter模式5.调用注册的所有拦截器的posthandleҎ6.l制mv<br /><br />也许spring不像struts、hibernate那样是用最为广泛的Q但他是全面的、轻量的、够灵zȝ、容易替换、容易扩展的。springweb是springframework中的一个部分,而dispatcherservlet又是springweb中的一部分,要弄懂spring以及他背后的设计思想Q对我等菜鸟而言Q还是有很长一D\要走的。但愿在新的一条\出现之前Q我们已l走遍了q条路。\漫O其修q兮Q吾上下而求索。l研Iing...to be continue...<br /><br /><img src ="http://www.aygfsteel.com/huamengxing/aggbug/140843.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.aygfsteel.com/huamengxing/" target="_blank">华梦?/a> 2007-08-29 12:25 <a href="http://www.aygfsteel.com/huamengxing/archive/2007/08/29/140843.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Spring-mvc 的处理流E?http://www.aygfsteel.com/huamengxing/archive/2007/08/29/140803.html华梦?/dc:creator>华梦?/author>Wed, 29 Aug 2007 02:23:00 GMThttp://www.aygfsteel.com/huamengxing/archive/2007/08/29/140803.htmlhttp://www.aygfsteel.com/huamengxing/comments/140803.htmlhttp://www.aygfsteel.com/huamengxing/archive/2007/08/29/140803.html#Feedback0http://www.aygfsteel.com/huamengxing/comments/commentRss/140803.htmlhttp://www.aygfsteel.com/huamengxing/services/trackbacks/140803.html

    关键? Spring   mvc ioc     

    h的分?/p>

    h首先到达DispatcherServletQ应用服务器会根据Web应用中web.xml文g定义的url映射相应的h分发到DispatcherServlet?/p>

    h的处?/p>

    DispatcherServlet会查扄应的HandlerMapping接口的实现类Q调用其中的ҎQHandlerExecutionChain getHandler(HttpServletRequest request) throws ExceptionQ该Ҏ会返回一个HandlerExecutionChain。返回的HandlerExecutionChain中包含了零个或者是多个Interceptor和一个处理请求的Handler。DispatcherServlet会调用Interceptor中的preHandle() Ҏ。然后处理HandlerQ这个Handler相当于Struts中ActionQ在SpringMVC中默认的实现是Controller接口Q是具体处理h的代码所ȝ的地斏V事实上HandlerExecutionChain中的getHandler()q回的是一个Objectcd。DispatcherServlet不会直接调用getHandlerQ)q回对象中的ҎQDispatcherServlet会查扄应的HandlerAdapterQ然后具体通过HandlerAdapter来调用getHandler()q回的handler对象中的Ҏ。就是说我们可以实现自己的HandlerAdapter然后通过IoC注入到DispatcherServlet中,从而可以实C套自定义的控制器。随后DispatcherServlet会调用Interceptor中的postHandle()Ҏ?

    视图的处?/p>

    DispatcherServlet会期望Handerq回一个ModelAndViewQDispatcherServlet会根据所q回的ModelAndView对象所包含的信息进行视囄渲染。v具体出来程如下Q?/p>

    首先DispatcherServlet会根据LocaleResolver来识别请求中的LocaleQ开发h员可以自己实现LocaleResolver接口Q然后通过IoC注入到DispatcherServlet中,然后DispatcherServlet会判断ModelAndView中是否已l包含了接口View的具体实玎ͼ如果包含了,则直接调用View中的Ҏrender(Map model, HttpServletRequest request, HttpServletResponse response)。如果不包含Q则说明该ModelAndView只是包含了View的名U引用,DispatcherServlet会调用ViewResolver中的resolveViewName(String viewName, Locale locale)来解析其真正的视图。该Ҏ会返回一个View的具体实现?/p>

    视图的渲?/p>

    Spring支持多种视图技术,其中比较常用的包括有Jstl视图QVeloctiy视图QFreeMarker视图{。对Jstl视图的渲染Spring是通过JstlViewq个cd体实现的。事实上其最l的渲染是交l容器来做的QSpring只是通过RequestDispatcher实现了服务器内部h的Forward。而对于模板视图,如Veloctiy和FreeMarker{,Spring会初始化其相应的模板引擎Q由模板引擎生成最l的Html面然后在合q到Response的输出流中?/p>

    异常的处?/p>

    如果在Hander中处理请求是抛出异常QDispatcherServlet会查找HandlerExceptionResolver接口的具体实玎ͼ该接口定义了一个方法:

    ModelAndView resolveException(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex)Q实现类需要实现该Ҏ以便对异常进行处理,最后该Ҏ需要返回一个ModelAndView?/p>

    SpringMVC的一些ȝ
    灉|的InterceptorQ通过Interceptor我们可以在一个请求处理前和请求处理完成之后做相应的操作,通过Interceptor机制Q我们可以做authentication, logging, and filtering{?
    良好的表单支持,在SpringMVC的Controllerl承体系l构中,其具体的子类对表单(FormQ提供了良好的支持。能够很好的支持单个表单的显C、修攏V提交操作。同时也提供了表单的分步提交?
    可定制的数据l定QData BindingQ?
    多视图技术的支持QSpringMVC同时支持Jstl, Velocity {多中视图技术,但是q同时也会引Z个问题,因ؓ各种视图技术都有自q一套方法来处理国际化,例如Jstl和Velocity处理国际化的方式很不相同。因此在多个视图技术ƈ存的应用中,国际化也是一个需要注意的问题?
    其HandlerQ控制器Q作为Bean定义在Spring容器中,因此能n受容器带来的服务?
    HandlerQ控制器Q具有良好的可测试性?



    ]]>HttpServletBeanhttp://www.aygfsteel.com/huamengxing/archive/2007/08/29/140794.html华梦?/dc:creator>华梦?/author>Wed, 29 Aug 2007 02:13:00 GMThttp://www.aygfsteel.com/huamengxing/archive/2007/08/29/140794.htmlhttp://www.aygfsteel.com/huamengxing/comments/140794.htmlhttp://www.aygfsteel.com/huamengxing/archive/2007/08/29/140794.html#Feedback0http://www.aygfsteel.com/huamengxing/comments/commentRss/140794.htmlhttp://www.aygfsteel.com/huamengxing/services/trackbacks/140794.html
    public abstract class HttpServletBean
    extends HttpServlet
    		

    Simple extension of HttpServlet which treats its config parameters (init-param entries within the servlet tag in web.xml) as bean properties.

    HttpServlet的简单扩展用来处?Qinit-paramQin the web.xml
    A handy superclass for any type of servlet. Type conversion of config parameters is automatic, with the corresponding setter method getting invoked with the converted value. It is also possible for subclasses to specify required properties. Parameters without matching bean property setter will simply be ignored.

    This servlet leaves request handling to subclasses, inheriting the default behavior of HttpServlet (doGet, doPost, etc).

    This generic servlet base class has no dependency on the Spring ApplicationContext concept. Simple servlets usually don't load their own context but rather access service beans from the Spring root application context, accessible via the filter's ServletContext (see WebApplicationContextUtils).

    The FrameworkServlet class is a more specific servlet base class which loads its own application context. FrameworkServlet serves as direct base class of Spring's full-fledged DispatcherServlet.

    public abstract class FrameworkServlet
    extends HttpServletBean
    implements ApplicationListener
    		

    Base servlet for Spring's web framework. Provides integration with a Spring application context, in a JavaBean-based overall solution.

    spring web Framework的基 servlet  Q提供在以javabean为基的整体解x案已完成与spring应用上下文的集成
    This class offers the following functionality:
    1.理一个servlet一个网l应用上下文实例Q这个servlet的配|由servlet命名I间里的bean来决?br />2.Ҏh处理发布事gQ是否请求成功的被处理了

    • Manages a WebApplicationContext instance per servlet. The servlet's configuration is determined by beans in the servlet's namespace.
    • Publishes events on request processing, whether or not a request is successfully handled.

    Subclasses must implement doService(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse) to handle requests. Because this extends HttpServletBean rather than HttpServlet directly, bean properties are automatically mapped onto it. Subclasses can override initFrameworkServlet() for custom initialization.

    因ؓ它承自httpservletBean 所以bean的属性已l被自动的装配了Q子cd以通过覆盖initFrameworkServlet来定制初始化bean

    Detects a "contextClass" parameter at the servlet init-param level, falling back to the default context class, XmlWebApplicationContext, if not found. Note that, with the default FrameworkServlet, a custom context class needs to implement the ConfigurableWebApplicationContext SPI.

    Passes a "contextConfigLocation" servlet init-param to the context instance, parsing it into potentially multiple file paths which can be separated by any number of commas and spaces, like "test-servlet.xml, myServlet.xml". If not explicitly specified, the context implementation is supposed to build a default location from the namespace of the servlet.

    Note: In case of multiple config locations, later bean definitions will override ones defined in earlier loaded files, at least when using Spring's default ApplicationContext implementation. This can be leveraged to deliberately override certain bean definitions via an extra XML file.

    The default namespace is "'servlet-name'-servlet", e.g. "test-servlet" for a servlet-name "test" (leading to a "/WEB-INF/test-servlet.xml" default location with XmlWebApplicationContext). The namespace can also be set explicitly via the "namespace" servlet init-param.



    ]]>
    定制初始化跟销毁bean的方?/title><link>http://www.aygfsteel.com/huamengxing/archive/2007/08/28/140488.html</link><dc:creator>华梦?/dc:creator><author>华梦?/author><pubDate>Tue, 28 Aug 2007 06:28:00 GMT</pubDate><guid>http://www.aygfsteel.com/huamengxing/archive/2007/08/28/140488.html</guid><wfw:comment>http://www.aygfsteel.com/huamengxing/comments/140488.html</wfw:comment><comments>http://www.aygfsteel.com/huamengxing/archive/2007/08/28/140488.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.aygfsteel.com/huamengxing/comments/commentRss/140488.html</wfw:commentRss><trackback:ping>http://www.aygfsteel.com/huamengxing/services/trackbacks/140488.html</trackback:ping><description><![CDATA[ <pre class="programlisting"> <pre class="programlisting"><bean id="exampleInitBean" class="examples.ExampleBean" init-method="cleanup"/></pre> <br /><bean id="exampleInitBean" class="examples.ExampleBean" destroy-method="cleanup"/></pre> <img src ="http://www.aygfsteel.com/huamengxing/aggbug/140488.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.aygfsteel.com/huamengxing/" target="_blank">华梦?/a> 2007-08-28 14:28 <a href="http://www.aygfsteel.com/huamengxing/archive/2007/08/28/140488.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Spring BeanFactory API 详解http://www.aygfsteel.com/huamengxing/archive/2007/08/28/140469.html华梦?/dc:creator>华梦?/author>Tue, 28 Aug 2007 06:09:00 GMThttp://www.aygfsteel.com/huamengxing/archive/2007/08/28/140469.htmlhttp://www.aygfsteel.com/huamengxing/comments/140469.htmlhttp://www.aygfsteel.com/huamengxing/archive/2007/08/28/140469.html#Feedback0http://www.aygfsteel.com/huamengxing/comments/commentRss/140469.htmlhttp://www.aygfsteel.com/huamengxing/services/trackbacks/140469.htmlBeanFactory的实现应该尽可能支持标准的bean生命周期Q以下是所以的Ҏ的顺序的依次列表
    1. BeanNameAware's   setBeanName
    2. BeanClassLoaderAware's  setBeanClassLoader
    3. BeanFactoryAware's   setBeanFactory
    4. ResourceLoaderAware's setResourceLoader (only applicable when running in an application context)
    5. ApplicationEventPublisherAware's setApplicationEventPublisher (only applicable when running in an application context)
    6. MessageSourceAware's setMessageSource (only applicable when running in an application context)
    7. ApplicationContextAware's setApplicationContext (only applicable when running in an application context)
    8. ServletContextAware's setServletContext (only applicable when running in a web application context)
    9. postProcessBeforeInitialization methods of BeanPostProcessors
    10. InitializingBean's afterPropertiesSet
    11. a custom init-method definition
    12. postProcessAfterInitialization methods of BeanPostProcessors
     当关闭一个bean的时?br />On shutdown of a bean factory, the following lifecycle methods apply:
    1. DisposableBean's destroy
    2. a custom destroy-method definition

    Method Summary
     booleancontainsBean(String name)
              Does this bean factory contain a bean with the given name?
     String[]getAliases(String name)
              Return the aliases for the given bean name, if any.
     ObjectgetBean(String name)
              Return an instance, which may be shared or independent, of the specified bean.
     ObjectgetBean(String name, Class requiredType)
              Return an instance, which may be shared or independent, of the specified bean.
     ClassgetType(String name)
              Determine the type of the bean with the given name.
     booleanisPrototype(String name)
              Is this bean a prototype?
     booleanisSingleton(String name)
              Is this bean a shared singleton?
     booleanisTypeMatch(String name, Class targetType)
              Check whether the bean with the given name matches the specified type.
     

    ]]>
    Lifecycle apihttp://www.aygfsteel.com/huamengxing/archive/2007/08/28/140460.html华梦?/dc:creator>华梦?/author>Tue, 28 Aug 2007 05:49:00 GMThttp://www.aygfsteel.com/huamengxing/archive/2007/08/28/140460.htmlhttp://www.aygfsteel.com/huamengxing/comments/140460.htmlhttp://www.aygfsteel.com/huamengxing/archive/2007/08/28/140460.html#Feedback0http://www.aygfsteel.com/huamengxing/comments/commentRss/140460.htmlhttp://www.aygfsteel.com/huamengxing/services/trackbacks/140460.html
    public interface Lifecycle
    		

    Interface defining methods for start/stop lifecycle control. The typical use case for this is to control asynchronous processing.

    Can be implemented by both components (typically a Spring bean defined in a Spring BeanFactory) and containers (typically a Spring ApplicationContext). Containers will propagate start/stop signals to all components that apply.

    Can be used for direct invocations or for management operations via JMX. In the latter case, the MBeanExporter will typically be defined with an InterfaceBasedMBeanInfoAssembler, restricting the visibility of activity-controlled components to the Lifecycle interface.

    Since:
    2.0
    Author:
    Juergen Hoeller
    See Also:
    ConfigurableApplicationContext , AbstractMessageListenerContainer, SchedulerFactoryBean

    Method Summary
     boolean isRunning ()
              Check whether this component is currently running.
     void start ()
              Start this component.
     void stop ()
              Stop this component.
     

    ]]>
    Spring中Bean的生命周?http://www.aygfsteel.com/huamengxing/archive/2007/08/28/140415.html华梦?/dc:creator>华梦?/author>Tue, 28 Aug 2007 04:56:00 GMThttp://www.aygfsteel.com/huamengxing/archive/2007/08/28/140415.htmlhttp://www.aygfsteel.com/huamengxing/comments/140415.htmlhttp://www.aygfsteel.com/huamengxing/archive/2007/08/28/140415.html#Feedback0http://www.aygfsteel.com/huamengxing/comments/commentRss/140415.htmlhttp://www.aygfsteel.com/huamengxing/services/trackbacks/140415.html Spring中Bean的生命周?/b>
        在传l的Java应用中,Bean的生命周期非常简单。Java的关键词new用来实例化BeanQ或总是非序列化的Q。这样就够用了。相反,Bean的生命周期在Spring容器中更加细致。理解Spring Bean的生命周期非帔R要,因ؓ你或许要利用Spring提供的机会来订制Bean的创E?br />

    1. 容器LBean的定义信息ƈ且将其实例化?br />2.受用依赖注入QSpring按照Bean定义信息配置Bean的所有属性?br />3.如果Bean实现了BeanNameAware接口Q工厂调用Bean的setBeanName()Ҏ传递Bean的ID?br />4.如果Bean实现了BeanFactoryAware接口Q工厂调用setBeanFactory()Ҏ传入工厂自n?br />5.如果BeanPostProcessor和Bean兌Q那么它们的postProcessBeforeInitialzation()Ҏ被调用?br />6.如果Bean指定了init-methodҎQ它被调用?br />7.最后,如果有BeanPsotProcessor和Bean兌Q那么它们的postProcessAfterInitialization()Ҏ被调用?br />    到这个时候,Bean已经可以被应用系l用了Qƈ且将被保留在Bean Factory中知道它不再需要。有两种Ҏ可以把它从Bean Factory中删除掉?br />1.如果Bean实现了DisposableBean接口Qdestory()Ҏ被调用?br />2.如果指定了订制的销毁方法,p用这个方法?br />    Bean在Spring应用上下文的生命周期与在Bean工厂中的生命周期只有一点不同,唯一不同的是Q如果Bean实现了ApplicationContextAwre接口QsetApplicationContext()Ҏ被调用?br />



    ]]>
    ClassPathXmlApplicationContext 获得javabeanhttp://www.aygfsteel.com/huamengxing/archive/2007/08/28/140324.html华梦?/dc:creator>华梦?/author>Tue, 28 Aug 2007 01:58:00 GMThttp://www.aygfsteel.com/huamengxing/archive/2007/08/28/140324.htmlhttp://www.aygfsteel.com/huamengxing/comments/140324.htmlhttp://www.aygfsteel.com/huamengxing/archive/2007/08/28/140324.html#Feedback0http://www.aygfsteel.com/huamengxing/comments/commentRss/140324.htmlhttp://www.aygfsteel.com/huamengxing/services/trackbacks/140324.html

    ]]>
    վ֩ģ壺 ʲ| | | Ҫ| | ij| ȷɽ| | ʡ| غ| ֹ| | | | ԭ| Դ| | ɽ| | Ƽ| ϳ| | | | ϵ| | ̫ԭ| | Դ| ν| | | | ɽ| | ˮ| | | | | |