1.配置文件: web配置: <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath*:applicationContext.xml</param-value>//在类文件下查找配置文件:applicationContext.xml </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <bean id="check" class="quanquanfly.Check" init-method="init" destroy-method="destroy"/> //初始化spring时自动运行 quanquanfly.Check中的init方法 2.注入: <bean id="test" class="quanquanfly.Test" /> <bean id= "testimpl" class="quanquanfly.TestImpl"> <property name="test" ref="test"/> </bean> junit测试: @Test public void test() { ApplicationContext act = new ClassPathXmlApplicationContext("applicationContext.xml"); TestImpl test = (TestImpl)act.getBean("testimpl"); test.saveimpl(); } 3.使用注解进行注入: <context:annotation-config /> @Resource(name = "test") public Test test;// @Resource先根据名字test到配置文件中查找,找不到在通过类型Test进行查找注入 @Resource public Test test; @Autowired public Test test1;// @Autowired默认按类型进行查找注入 @Autowired @Qualifier("test") public Test test2;// 更改默认按类型为按test名字进行查找注入 4.自动扫描class管理bean: <context:component-scan base-package="quanquanfly"/> 扫描的类应该含有@Repository@Controller@Service等标记 @Service("testimpl")//设置bean名字为testimpl,默认为类名第一字母小写testImpl public class TestImpl { //@Resource(name="test") public Test test;//@Resource先根据名字test到配置文件中查找,找不到在通过类型Test进行查找注入 //@Autowired public Test test1;//@Autowired默认按类型进行查找注入 //@Autowired @Qualifier("test") public Test test2;//更改默认按类型为按test名字进行查找注入 @Resource public Test test; public void saveimpl() { test.save(); } } 4.aop: <?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:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd"> <aop:aspectj-autoproxy />//开启aop注释 <context:annotation-config /> <!-- <context:component-scan base-package="quanquanfly" /> --> <bean id="check" class="quanquanfly.Check" init-method="init" destroy-method="destroy" /> <bean id="test" class="quanquanfly.Test" /> <bean id="testimpl" class="quanquanfly.TestImpl"> </bean> <bean id="myInterceptor" class="quanquanfly.Check1"/> </beans> @Aspect@Repository public class Check1 { @Pointcut("execution(* quanquanfly.TestImpl.*(..))")//声明切入点 public void anyMethod(){} @Before("anyMethod()")//定义前置通知 public void before() { System.out.println("前置通知"); } @AfterReturning("anyMethod()")//定义后置通知 public void afterreturn() { System.out.println("后置通知"); } @After("anyMethod()")//定义后置通知 public void after() { System.out.println("最终通知"); } @AfterThrowing("anyMethod()")//出现例外通知的时候,不出现后置通知,其他通知都可以执行 public void throwing() { System.out.println("例外通知"); } @Around("anyMethod()") public Object doBasicProfiling(ProceedingJoinPoint pjp) throws Throwable { System.out.println("进入方法"); Object result = pjp.proceed(); System.out.println("退出方法"); return result; } }