<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开头的方法加强