musiclover

2009年10月13日 #

用Struts2实现在线银行付款

     摘要:                                  &n...  阅读全文

posted @ 2009-10-13 18:27 Jxi 阅读(1178) | 评论 (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 阅读(130) | 评论 (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 阅读(144) | 评论 (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 阅读(123) | 评论 (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 阅读(120) | 评论 (0)编辑 收藏

仅列出标题  

My Links

Blog Stats

常用链接

留言簿

随笔分类

随笔档案

搜索

最新评论

阅读排行榜

评论排行榜