applicationContext.xml <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd"> <beans> <!-- 建立数据源bean --> <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <property name="driverClassName"> <value>com.mysql.jdbc.Driver</value> </property> <property name="url"> <value>jdbc:mysql://localhost:3306/ssh?useUnicode=true&characterEncoding=UTF-8</value> </property> <property name="username"> <value>root</value> </property> <property name="password"> <value>root</value> </property> </bean> <!-- 建立LoginActionForm bean --> <bean id="loginForm" class="com.spring.chapterNine.LoginActionForm"> <property name="userName"> <value>test</value> </property> <property name="password"> <value>pass</value> </property> </bean> <!-- 建立CustomerLoginActionForm bean 类之间的依赖关系被定义出来 --> <bean id="customer" class="com.spring.chapterNine.CustomerLoginAction" singleton="true"> <property name="ds"> <ref local="dataSource"/> </property> <property name="tablename"> <value>customer</value> </property> <property name="loginActionForm"> <ref local="loginForm"/> </property> <property name="checklogin"> <ref bean="checklogin"/> </property> </bean> <!-- 建立before类型通知bean --> <bean id="loginBeforeAdvice" class="LoginBeforeAdvice"></bean> <!-- 建立代理bean --> <bean id="proxy" class="org.springframework.aop.framework.ProxyFactoryBean"> <!-- 注入代理对象实现的接口 --> <property name="proxyInterfaces"> <value>com.spring.LoginAction</value> </property> <!-- 注入目标对象,也就是被代理的对象 --> <property name="target"> <ref local="customerLogin"/> </property> <!-- 注入通知列表 --> <property name="interceptorNames"> <value>loginBeforeAdvice</value> </property> </bean> <!-- Advisor的配置 --> <bean id="loginBeforeAdvice" class="com.spring.LoginBeforeAdvice"></bean> <!-- 定义Advisor --> <bean id="loginRegPointcutAdvisor" class="org.springframework.aop.support.RegexpMethodPointcutAdvisor"> <!-- 用正则表达式来表示切点中方法名的范围,用来和目标对象方法名匹配,如果匹配则执行通知 --> <property name="pattern"> <value>.*execute.*</value> </property> </bean> <bean id="loginPointcutAdvisor" class="org.springframework.aop.support.NameMatchMethodPointcutAdvisor"> <!-- 设定切点中方法名,用来和目标对象方法名匹配。如果匹配则执行通知value值可使用通配符 --> <property name="mappedName"> <value>execute</value> </property> <!-- 引入通知 --> <property name="advice"> <ref bean="loginBeforeAdvice"/> </property> </bean> <!-- 定义代理 --> <bean id="proxy" class="org.springframework.aop.framework.ProxyFactoryBean"> <property name="proxyInterfaces"> <value>com.spring.LoginAction</value> </property> <!-- 下面引入Advisor,来根据切点执行通知 --> <property name="interceptorNames"> <value>loginPointcutAdvisor</value> </property> <property name="target"> <ref local="customerLogin"/> </property> </bean> <bean id="loginIntroductionAdvisor" class="org.springframework.aop.support.DefaultIntroductionAdvisor"> <!-- 注入通知 --> <constructor-arg> <ref bean="loginIntroductionAdvice"/> </constructor-arg> </bean> </beans> 初始化spring有两种方法 1.servlet <context-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/applicationContext.xml</param-value> </context-param> <servlet> <servlet-name>context</servlet-name> <servlet-class>org.springframework.web.context.ContextLoaderServlet</servlet-class> <load-on-startup>1</load-on-startup> </servlet> 2.listener <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <context-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/applicationContext.xml</param-value> </context-param> 使用spring的配置 1. public static void main(String[] args){ ApplicationContext ctx= new FileSystemXmlApplicationContext("WEB-INF//applicationContext.xml"); LoginAction userLogin= null; userLogin=(LoginAction)ctx.getBean("customerLogin"); try { userLogin.execute(); } catch (SQLException e) { System.out.println("sorry,accessing database is failed"); } 2. ApplicationContext ctx = WebApplicationContextUtils.getRequiredWebApplicationContext(ServletActionContext.getServletContext());