锘??xml version="1.0" encoding="utf-8" standalone="yes"?>
IUserService:
package com.swjs.aop.serivce;
import java.util.List;
/**
* @author jason
*
*/
public interface IUserService {
List getUserList();
}
琚唬鐞嗙被UserService錛?br />
package com.swjs.aop.serivce;
import java.util.List;
/**
* @author jason
*
*/
public class UserService implements IUserService {
public List getUserList() {
System.out.println("get Userlist");
return null;
}
}
浠g悊綾伙細
package com.swjs.aop.serivce;
import java.util.List;
/**
* @author jason
*
*/
public class SecureProxy implements IUserService{
private IUserService userService;
public SecureProxy(IUserService userService) {
super();
this.userService = userService;
}
public List getUserList()
{
System.out.println("韜喚媯鏌?/span>");
userService.getUserList();
System.out.println("浜嬪姟鎻愪氦");
return null;
}
}
嫻嬭瘯綾伙細
package com.swjs.aop.serivce;
/**
* @author jason
*
*/
public class StaticProxyTest {
/**
* @param args
*/
public static void main(String[] args) {
IUserService userService = new UserService();
SecureProxy proxy = new SecureProxy(userService);
proxy.getUserList();
}
}
鏄劇ず緇撴灉錛?br />
韜喚媯鏌?br />
get Userlist
浜嬪姟鎻愪氦
浜岋細鍔ㄦ佷唬鐞?br />
鎺ュ彛
public interface IUserService {
public void getUserList(int start, int limit);
}
涓氬姟鏂規硶錛?br />
public class UserService implements IUserService {
public void getUserList(int start, int limit) {
System.out.println("start: " + start + " limit: " + limit );
System.out.println("get user List");
}
}
澶勭悊鍣細
public class SecureHandler implements InvocationHandler {
private IUserService service;
public SecureHandler(IUserService service) {
super();
this.service = service;
}
public Object invoke(Object proxy, Method method, Object[] arg)
throws Throwable {
System.out.println("trac begin");
System.out.println(arg.length);
for(int i = 0; i < arg.length; i++)
{
System.out.println(arg[i]);
}
method.invoke(service, arg);
System.out.println("trac end");
return null;
}
}
嫻嬭瘯綾伙細
public class DynamicProxyTest {
public static void main(String[] args)
{
IUserService service = new UserService();
SecureHandler handler = new SecureHandler(service);
IUserService serv = (IUserService)Proxy.newProxyInstance(service.getClass().getClassLoader(),
service.getClass().getInterfaces(), handler);
serv.getUserList(0,10);
}
}
鏄劇ず緇撴灉錛?br />
trac begin
2
0
10
start: 0 limit: 10
get user List
trac end
]]>
hibernate.dialect=org.hibernate.dialect.MySQLDialect
2hibernate.driverClassName=com.mysql.jdbc.Driver
3hibernate.url=jdbc:mysql://127.0.0.1:3306/test
4hibernate.username=root
5hibernate.password=sa
6hibernate.showSQL=true
7hibernate.maxActive=50
8hibernate.maxIdle=30
9hibernate.maxWait=1000
2銆乤pplicationContext.xml
<?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.0.xsd">
6
7 <!-- 璇誨叆灞炴ф枃浠?nbsp;-->
8 <bean id="propertyConfig" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
9 <property name="locations">
10 <list>
11 <value>classpath:hibernate.properties</value>
12 </list>
13 </property>
14 </bean>
15
16 <!-- 閰嶇疆鏁版嵁婧愶紝鍙互鍏朵粬鏂瑰紡 -->
17 <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
18 <property name="driverClassName" value="${hibernate.driverClassName}" />
19 <property name="url" value="${hibernate.url}" />
20 <property name="username" value="${hibernate.username}" />
21 <property name="password" value="${hibernate.password}" />
22 <property name="maxActive" value="${hibernate.maxActive}" />
23 <property name="maxIdle" value="${hibernate.maxIdle}" />
24 <property name="maxWait" value="${hibernate.maxWait}" />
25 </bean>
26
27 <!-- 閰嶇疆Hibernate鐨凷ession宸ュ巶錛屾敞鍏ユ暟鎹簮銆佹槧灝勬枃浠?nbsp;-->
28 <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
29 <property name="dataSource">
30 <ref local="dataSource"/>
31 </property>
32 <property name="mappingResources">
33 <list>
34 <value>com/sailor/test/dao/Employee.hbm.xml</value>
35 </list>
36 </property>
37 <property name="hibernateProperties">
38 <props>
39 <prop key="hibernate.dialect">${hibernate.dialect}</prop>
40 <prop key="hibernate.show_sql">${hibernate.showSQL}</prop>
41 </props>
42 </property>
43 </bean>
44
45
46 <!-- 瀹氫箟浜嬪姟綆$悊鍣紝浣跨敤閫傜敤浜嶩ibernte鐨勪簨鍔$鐞嗗櫒-->
47 <bean id="transactionManager"
48 class="org.springframework.orm.hibernate3.HibernateTransactionManager">
49 <!-- HibernateTransactionManager bean闇瑕佷緷璧栨敞鍏ヤ竴涓猄essionFactory bean鐨勫紩鐢?/span>-->
50 <property name="sessionFactory">
51 <ref local="sessionFactory" />
52 </property>
53 </bean>
54
55 <!-- 閰嶇疆浜嬪姟鎷︽埅鍣?/span>-->
56 <bean id="transactionInterceptor"
57 class="org.springframework.transaction.interceptor.TransactionInterceptor">
58 <!-- 浜嬪姟鎷︽埅鍣╞ean闇瑕佷緷璧栨敞鍏ヤ竴涓簨鍔$鐞嗗櫒 -->
59 <property name="transactionManager" ref="transactionManager" />
60 <property name="transactionAttributes">
61 <!-- 涓嬮潰瀹氫箟浜嬪姟浼犳挱灞炴?/span>-->
62 <props>
63 <!-- 鎵鏈変互save寮澶寸殑鏂規硶錛岄噰鐢╮equired鐨勪簨鍔$瓥鐣?/span>-->
64 <prop key="save*">PROPAGATION_REQUIRED</prop>
65 <!-- 鎵鏈変互mod寮澶寸殑鏂規硶錛岄噰鐢╮equired鐨勪簨鍔$瓥鐣?/span>-->
66 <prop key="mod*">PROPAGATION_REQUIRED</prop>
67 <!-- 鎵鏈変互del寮澶寸殑鏂規硶錛岄噰鐢╮equired鐨勪簨鍔$瓥鐣?/span>-->
68 <prop key="del*">PROPAGATION_REQUIRED</prop>
69 <!-- 鍏朵粬鏂規硶錛宺eadOnly -->
70 <prop key="*">readOnly</prop>
71 </props>
72 </property>
73 </bean>
74
75 <!-- 瀹氫箟BeanNameAutoProxyCreator,璇ean鏄釜bean鍚庡鐞嗗櫒錛屾棤闇琚紩鐢紝鍥犳娌℃湁id灞炴?br />
76
榪欎釜bean鍚庡鐞嗗櫒錛屾牴鎹簨鍔℃嫤鎴櫒涓虹洰鏍嘼ean鑷姩鍒涘緩浜嬪姟浠g悊 -->
77 <bean
78 class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">
79 <!--鎸囧畾瀵規弧瓚沖摢浜沚ean name鐨刡ean鑷姩鐢熸垚涓氬姟浠g悊 -->
80 <property name="beanNames">
81 <!-- 涓嬮潰鏄墍鏈夐渶瑕佽嚜鍔ㄥ垱寤轟簨鍔′唬鐞嗙殑bean-->
82 <list>
83 <value>employeeService</value>
84 </list>
85 <!-- 姝ゅ鍙鍔犲叾浠栭渶瑕佽嚜鍔ㄥ垱寤轟簨鍔′唬鐞嗙殑bean-->
86 </property>
87 <!-- 涓嬮潰瀹氫箟BeanNameAutoProxyCreator鎵闇鐨勪簨鍔℃嫤鎴櫒-->
88 <property name="interceptorNames">
89 <list>
90 <value>transactionInterceptor</value>
91 <!-- 姝ゅ鍙鍔犲叾浠栨柊鐨処nterceptor -->
92 </list>
93 </property>
94 </bean>
95
96 <!-- dao灞?nbsp;-->
97 <bean id="employeeDAO" class="com.sailor.test.dao.EmployeeDAO">
98 <property name="sessionFactory" ref="sessionFactory"></property>
99 </bean>
100
101 <!-- service灞?nbsp;-->
102 <bean id="employeeService" class="com.sailor.test.service.impl.EmployeeServiceImpl">
103 <property name="employeeDAO" ref="employeeDAO" />
104 </bean>
105
106 </beans>
]]>
]]>
]]>
web.xml 錛?br />
閫氳繃錛?
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener-->
鎴栵細
<servlet>
<servlet-name>SpringContextServlet</servlet-name>
<servlet-class>org.springframework.web.context.ContextLoaderServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
Web 瀹瑰櫒浼氳嚜鍔ㄥ姞杞?/WEB-INF/applicationContext.xml 鍒濆鍖?ApplicationContex t瀹炰緥錛?br />
涔熷彲浠ラ氳繃
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext-*.xml</param-value>
</context-param>
浣?Web 瀹瑰櫒鍔犺澆鎸囧畾鍚嶇О璺緞鐨?Spring 閰嶇疆鏂囦歡銆?/p>
2銆侀氳繃 struts-config.xml 鏉ヤ嬌 Web 瀹瑰櫒鍔犺澆 Spring
struts-config.xml
閫氳繃
<plug-in className="org.springframework.web.struts.ContextLoaderPlugIn">
<set-property property="contextConfigLocation"
value="/WEB-INF/applicationContext.xml,
/WEB-INF/action-servlet.xml"/>
</plug-in>
鏉ュ姞杞?Spring 閰嶇疆鏂囦歡銆?/p>
鏂囩珷鍑哄錛歨ttp://www.diybl.com/course/3_program/java/javashl/200855/112963.html