musiclover

          2009年10月13日 #

          用Struts2實現在線銀行付款

               摘要:                                  &n...  閱讀全文

          posted @ 2009-10-13 18:27 Jxi 閱讀(1201) | 評論 (2)編輯 收藏

          2009年9月30日 #

          SSH基礎搭建

           

                                     Struts2+Hibernate3.0+Spring的基本搭建(非常基礎的)

          具體操作步驟截圖見附件(源代碼不包含lib包):S2SH截圖
          除了操作的配置代碼:
          WEB-INF下的web.xml的配置:

           1 <?xml version="1.0" encoding="UTF-8"?>
           2 <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
           3     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           4     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
           5     http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
           6     <!-- 配置Struts 2框架的核心Filter -->
           7     <filter>
           8         <filter-name>struts</filter-name>
           9         <filter-class>
          10             org.apache.struts2.dispatcher.FilterDispatcher
          11         </filter-class>
          12     </filter>
          13 
          14     <!-- 配置Filter攔截的URL -->
          15     <filter-mapping>
          16         <filter-name>struts</filter-name>
          17         <url-pattern>/*</url-pattern>
          18     </filter-mapping>
          19     <!-- 配置Spring -->
          20 <context-param>
          21  <param-name>contextConfigLocation</param-name>
          22  <param-value>classpath:applicationContext.xml</param-value>
          23 </context-param>
          24 
          25 <listener>
          26    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
          27 </listener>
          28 
          29     <welcome-file-list>
          30         <welcome-file>index.jsp</welcome-file>
          31     </welcome-file-list>
          32 </web-app>
          33 

          applicationContext.xml:
           1 <?xml version="1.0" encoding="UTF-8"?>
           2 <beans
           3     xmlns="http://www.springframework.org/schema/beans"
           4     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           5     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
           6 <!-- Spring 風格的Hibernate的配置 -->
           7 <!-- 使用外部屬性文件/WEB-INF/jdbc.properties -->
           8 <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> 
           9      <property name="location" value="/WEB-INF/jdbc.properties"></property>
          10 </bean>
          11     <!-- 配置數據源 -->
          12 <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
          13  destroy-method="close" >
          14           <property name="driverClassName" value="${jdbc.driverClassName}"></property>
          15           <property name="url" value="${jdbc.url}"></property>
          16           <property name="username" value="${jdbc.username}"></property>
          17           <property name="password" value="${jdbc.password}"></property>     
          18 </bean>
          19 
          20 
          21 <!-- 創建SessionFactory實例 -->
          22 <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
          23 <property name="dataSource" ref="dataSource"></property><!-- 數據源 -->
          24 
          25 <property name="mappingResources">
          26    <list>
          27     
          28    </list>
          29 
          30 </property>
          31 
          32 
          33 <!-- 配置數據庫方言 -->
          34 <property name="hibernateProperties">
          35      <props>
          36           <prop key="hibernate.dialect">
          37             ${hibernate.dialect}
          38           </prop>
          39           <prop key="hibernate.show_sql">true</prop>
          40           <prop key="hibernate.generate_satistices">true</prop>
          41      </props>
          42 </property>
          43 </bean>
          44 
          45 
          46  <!-- 使用Hibernate3事務管理配置 -->
          47  <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
          48     <property name="sessionFactory" ref="sessionFactory"></property>
          49  </bean>
          50 
          51 
          52 <!-- 配置HibernateTemplate -->
          53 <bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate">
          54    <property name="sessionFactory" ref="sessionFactory"></property>
          55 </bean>
          56 
          57 
          58 </beans>

          struts.xml:
           1 <?xml version="1.0" encoding="UTF-8" ?>
           2 <!DOCTYPE struts PUBLIC
           3     "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
           4     "http://struts.apache.org/dtds/struts-2.0.dtd">
           5 <struts>
           6   <!-- 包含外部struts的配置 -->
           7  <!--  <include file="filename"></include>  -->
           8  
           9  <!-- 配置UTF-8 -->
          10 <constant name="struts.i18n.encoding" value="utf-8"></constant>
          11  <package name="struts2" extends="struts-default">
          12  
          13  <!-- 寫Action的配置  -->
          14   
          15  
          16  </package>
          17   
          18 </struts>
          hibernate.cfg.xml:什么都做,全交給Spring管理
           1 <?xml version='1.0' encoding='UTF-8'?>
           2 <!DOCTYPE hibernate-configuration PUBLIC
           3           "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
           4           "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
           5 
           6 <!-- Generated by MyEclipse Hibernate Tools.                   -->
           7 <hibernate-configuration>
           8 
           9     <session-factory>
          10     
          11     </session-factory>
          12 
          13 </hibernate-configuration>
          WEB-INF下的數據庫配置文件 jdbc.properties
          1 hibernate.dialect= org.hibernate.dialect.MySQLDialect
          2 jdbc.driverClassName=com.mysql.jdbc.Driver
          3 jdbc.url=jdbc\:mysql\://localhost\:3306/bookbooks
          4 jdbc.username=root
          5 jdbc.password=123456
          6 



          posted @ 2009-09-30 15:29 Jxi 閱讀(144) | 評論 (0)編輯 收藏

          2009年9月14日 #

          Spring @AspectJ

          準備工作:需要  spring/lib/asm 下的3個包:asm.jar  asm-commons.jar asm-util.jar
          spring/lib/aspectj  下的2個包 aspectjrt.jar  和aspectjweaver.jar
          使用@AspectJ的小例子:

          public interface Company {
            
          public void descript();
            
          public void ss();
          }

          public class stockCompany implements Company{

              
          public void descript() {
                  System.out.print(
          "This is a stock market");
              }

              
          //另外添加一個內嵌descript()的函數 調用后不會攔截里面的descript函數
              public void ss(){
                  System.out.println(
          "在另外一個函數調用的");
                  descript();
              }

          }

          import org.aspectj.lang.annotation.Aspect;
          import org.aspectj.lang.annotation.Before;


          @Aspect
          public class PreDescriptAspect {
          @Before(
          "execution(* descript(..))")
           
          public void beforeDescript(){
               System.out.print(
          "Here Is My Company Description!");
           }

          }

          import org.springframework.aop.aspectj.annotation.AspectJProxyFactory;

          public class stockCompanyTest {
           
          public static void main(String[] args){
           Company target
          =new stockCompany();
           AspectJProxyFactory factory
          =new AspectJProxyFactory();
           factory.setTarget(target);
           factory.addAspect(PreDescriptAspect.
          class);
           Company proxy
          =factory.getProxy();
           proxy.descript();
           proxy.ss();
           }

          }



          posted @ 2009-09-14 20:01 Jxi 閱讀(157) | 評論 (0)編輯 收藏

          2009年8月23日 #

          Spring AOP 學習筆記

          AOP  Aspect Oriented Programing  面向方面編程
          AOP 術語
          連接點:(Joinpoint)    程序執行的特定位置(類開始初始化前、類初始化后、調用前后、異常拋出后)
          切點:   (PointCut)     程序類中客觀存在的事物(類中的方法)
          增強 :  (Advice)        置入到目標類Joinpint的一段代碼
          目標對象:(Target)   引介(Introduction)  織入(Weaving)  代理(Proxy) 切面(Aspect):切點和增強的組成 
           
          基礎知識: Spring Aop :使用動態代理技術在運行期間織入增強代碼
          ProxyFactory 代理工廠采用JDK代理或CGLib代理技術
          JDK 動態代理:
          1.定義UserService接口函數:
          public interface UserService {
          public void addUser(int id);
           }

          2.UserServiceImpl
          public class UserServiceImpl implements UserService{
           
          public void addUser(int id){
               System.out.println(
          "添加一個用戶中 :"+id);
           }

          }

          3.ProxyHandler
          import java.lang.reflect.InvocationHandler;
          import java.lang.reflect.Method;


          public class ProxyHandler implements InvocationHandler{
             
          private Object target;
             
          public  ProxyHandler(Object target){
                 
          this.target=target;
             }

             
          public Object invoke(Object proxy, Method method, Object[] args)
                  
          throws Throwable {
                 System.out.println(
          "添加用戶前");
                 Object obj
          =method.invoke(target,args);
                 System.out.println(
          "添加用戶后");
                 
          return obj;
          }

          }

          TestProxyHandler
          import java.lang.reflect.Proxy;

          public class ProxyHandlerTest {
            
          public static void main(String args[]){
                UserService target
          =new UserServiceImpl();
                ProxyHandler handler
          =new ProxyHandler(target);
                UserService proxy
          =(UserService)Proxy.newProxyInstance(
                  target.getClass().getClassLoader(),
                  target.getClass().getInterfaces(),
                  handler);
                proxy.addUser(
          100);
            }

          }

          CGLib 代理:
          AddUserService接口:
          package CGlib.Text.Service;

          public interface AddUserService {
           
          public void addUser(int id);
          }

          AddUserServiceImpl 實現類:
          package CGlib.Text.Service;

          public class AddUserServiceImpl implements AddUserService {

              
          public void addUser(int id) {
                  System.out.println(
          "添加用戶ID中:"+id);
              }
          }
          CglibProxy 代理類:
          package CGlib.Text.Service;



          import java.lang.reflect.Method;

          import net.sf.cglib.proxy.Enhancer;
          import net.sf.cglib.proxy.MethodInterceptor;
          import net.sf.cglib.proxy.MethodProxy;

          public class CglibProxy implements MethodInterceptor{
              
          private Enhancer enhancer=new Enhancer();
              @SuppressWarnings(
          "unchecked")
              
          public Object getProxy(Class clazz){
                  enhancer.setSuperclass(clazz);
                  enhancer.setCallback(
          this);
                  
          return enhancer.create();        
              }


              
          public Object intercept(Object obj, Method method, Object[] args,
                      MethodProxy proxy) 
          throws Throwable {
                  System.out.println(
          "添加用戶之前");
                  System.out.print(method.getName());
                  System.out.print(obj.getClass().getName()
          +"外加");
                  System.out.print(args.getClass().getName());
                  Object result
          =proxy.invokeSuper(obj, args);
                  System.out.println(
          "外加的參數"+args.toString());
                  System.out.println(
          "添加用戶之后");
                  
          // TODO Auto-generated method stub
                  return result;
              }

              

          }


          CglibProxyTest:測試類
          package CGlib.Text.Service;

          public class CglibProxyTest {
              
          public static void main(String[] args){
                  CglibProxy proxy
          =new CglibProxy();
                  AddUserServiceImpl userService
          =
                      (AddUserServiceImpl)proxy.getProxy(AddUserServiceImpl.
          class);
                userService.addUser(
          100);        
              }


          }


          對JDK動態代理和Cglib動態代理的理解:
          JDK   代理時性能高,但生成代理對象時運行性能較低
          Cglib 代理時性能低,但生成代理對象時運行性能較高
          所以:單例Singleton 時采用CGLib代理。

          posted @ 2009-08-23 22:52 Jxi 閱讀(137) | 評論 (0)編輯 收藏

          2009年7月25日 #

          Spring

           

          Spring

          Important 

          Spring is an application framework. Unlike single-tier frameworks such as Struts or Hibernate, Spring aims to help structure whole applications in a consistent, productive manner, pulling together best-of-breed single-tier frameworks to create a coherent architecture.

          Problems with the Traditional Approach to J2EE

          傳統J2EE方法存在的問題

          • J2EE applications tend to contain excessive amounts of "plumbing" code.
          • Many J2EE applications use a distributed object model where this is inappropriate.
          • The EJB component model is unduly complex.不當
          • EJB is overused.過度使用
          • Many "J2EE design patterns" are not, in fact, design patterns, but workarounds for technology limitations.技術跟不上
          • J2EE applications are hard to unit test.測試難
          • Certain J2EE technologies have simply failed.

          Enter Spring

          接觸Spring

          • Inversion of Control container:倒置控制
          • Aspect-Oriented Programming (AOP) framework:
          • Data access abstraction:
          • JDBC simplification:
          • Transaction management:
          • MVC web framework:
          • Simplification for working with JNDI, JTA, and other J2EE APIs:
          • Lightweight remoting:
          • JMS support:
          • JMX support:
          • Support for a comprehensive testing strategy for application developers:

          The key Spring values can be summarized as follows:

          ·         Spring is a non-invasive framework.非侵入

          ·         Spring provides a consistent programming model, usable in any environment.

          ·         Spring aims to promote code reuse.復用

          ·         Spring aims to facilitate Object Oriented design in J2EE applications.

          ·         Spring aims to facilitate good programming practice, such as programming to interfaces, rather than classes.面向接口

          ·         Spring promotes pluggability.性能

          ·         Spring facilitates the extraction of configuration values from Java code into XML or properties files.

          ·         Spring is designed so that applications using it are as easy as possible to test.

          ·         Spring is consistent.

          ·         Spring promotes architectural choice.

          ·         Spring does not reinvent the wheel.

          posted @ 2009-07-25 14:57 Jxi 閱讀(152) | 評論 (0)編輯 收藏

          僅列出標題  

          My Links

          Blog Stats

          常用鏈接

          留言簿

          隨筆分類

          隨筆檔案

          搜索

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 灌云县| 钟祥市| 鹰潭市| 蒲江县| 类乌齐县| 西吉县| 盐边县| 涿鹿县| 德格县| 伊春市| 越西县| 台山市| 房山区| 墨脱县| 平安县| 抚松县| 徐汇区| 泗洪县| 郸城县| 沛县| 故城县| 巴林左旗| 河曲县| 镇雄县| 永顺县| 扶余县| 宜州市| 孟津县| 景谷| 易门县| 宁阳县| 安康市| 晋江市| 谢通门县| 濮阳县| 保靖县| 新蔡县| 田阳县| 黄冈市| 江达县| 河北省|