<bean id="userSevice" class="com.ljw.service.UserService">
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:util="http://www.springframework.org/schema/util" xmlns:jpa="http://www.springframework.org/schema/data/jpa" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.1.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-4.1.xsd http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.1.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.1.xsd http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.1.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.1.xsd"> </beans>
spring_context.xml
配置文件中配置相關的設置spring_context.xml
配置文件中配置相關的設置spring_context.xml
配置文件中配置相關的設置測試:
java
註解詳情請查看文章:註解 @Annotationweb
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <!-- bean definitions here --> </beans>
@Scope(value="singleton")
單實例@Scope(value="prototype")
多實例用註解建立 dao 對象和 service 對象
spring
再service類中建立dao類型的屬性,並用註解注入對象,有兩種方式
第一種:@Autowired
編程
第二種:@Resource
spring-mvc
導入基本jar包和AOP相關的jar包
服務器
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"> <!-- bean definitions here --> </beans>
package com.ljw.spring.annotation; import org.aspectj.lang.ProceedingJoinPoint; import org.springframework.stereotype.Component; @Component(value="userAdvice") public class UserAdvice { /** * @description 前置通知 */ public void userBefore() { System.out.println("前置通知........"); } /** * @description 後置通知 */ public void userAfter() { System.out.println("後置通知........"); } /** * @description 環繞通知 * @param proceedingJoinPoint * @throws Throwable */ public void userAround(ProceedingJoinPoint proceedingJoinPoint) throws Throwable { // 方法以前 System.out.println("方法以前.........."); // 執行加強的方法 proceedingJoinPoint.proceed(); // 方法以後 System.out.println("方法以後.........."); } }
實現思想:把加載配置文件和建立對象過程,在服務器啓動的時候完成mvc
ServletContext servletContext = config.getServletContext();
ServletContext servletContext = this.getServletContext();
execution(<訪問修飾符>?<返回類型><方法全名>(<參數>)<異常>)
execution(* com.ljw.spring.aop.User.add(..))
對User類的add方法加強execution(* com.ljw.spring.aop.User.*(..))
對User類的全部方法加強execution(* *.*(..))
對全部方法加強execution(* save*(..))
對全部save開頭的方法加強