??????????
package com.bjsxt.service;
import javax.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;
import com.bjsxt.dao.UserDAO;
import com.bjsxt.model.User;
@Component("userService")
public class UserService {
private UserDAO userDAO;
public void init() {
System.out.println("init");
}
public void add(User user) {
userDAO.save(user);
}
public UserDAO getUserDAO() {
return userDAO;
}
@Resource(name="u")
public void setUserDAO( UserDAO userDAO) {
this.userDAO = userDAO;
}
public void destroy() {
System.out.println("destroy");
}
}
????????aop
package com.bjsxt.aop;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;
@Aspect
@Component
public class LogInterceptor {
@Pointcut("execution(public * com.bjsxt.service..*.add(..))")
public void myMethod(){};
/*@Before("execution(public void com.bjsxt.dao.impl.UserDAOImpl.save(com.bjsxt.model.User))")*/
@Before("myMethod()")
public void before() {
System.out.println("method staet");
}
@After("myMethod()")
public void after() {
System.out.println("method after");
}
@AfterReturning("execution(public * com.bjsxt.dao..*.*(..))")
public void AfterReturning() {
System.out.println("method AfterReturning");
}
@AfterThrowing("execution(public * com.bjsxt.dao..*.*(..))")
public void AfterThrowing() {
System.out.println("method AfterThrowing");
}
}
???????????
<?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-3.1.xsd
"><!-- ?????2?? -->
<context:annotation-config />
<context:component-scan base-package="com.bjsxt"/>  <!-- ?????? -->
<aop:aspectj-autoproxy/>  <!-- ??????? -->
</beans>
??????????
package com.bjsxt.service;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.bjsxt.model.User;
//Dependency Injection
//Inverse of Control
public class UserServiceTest {
@Test
public void testAdd() throws Exception {
ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
UserService service = (UserService)ctx.getBean("userService");
System.out.println(service.getClass());
service.add(new User());
System.out.println("###");
ctx.destroy();
}
}
?????????
????class com.bjsxt.service.UserService$$EnhancerByCGLIB$$7b201784
????method staet
????user save11d!
????method AfterReturning
????method after
????###
???????
????@Aspect???????????????????
????@Componet????????????????? Spring ??????????????????????????????????????? Spring ??????
????@Befoe???????????(Advice)
????execution…:???????
??????????xml????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-3.1.xsd
"><!-- ?????2?? -->
<context:annotation-config />
<context:component-scan base-package="com.bjsxt"/>
<bean id="logInterceptor" class="com.bjsxt.aop.LogInterceptor"></bean>
<aop:config>
<aop:pointcut expression="execution(public * com.bjsxt.service..*.add(..))"
id="servicePointcut"/>
<aop:aspect id="logAspect" ref="logInterceptor">
<aop:before method="before"  pointcut-ref="servicePointcut" />
</aop:aspect>
</aop:config>
</beans>
?????????<beans>??Spring??????????beans???漸????????????
????xmlns??
??????????xml??????????????spring??beans???????http://www.springframework.org/schema/beans??
?????????????????????????beans?????????????????????????<>??????????<bean>????
????xmlns:xsi??
??????xml????????淶?????URL???????????w3?????淶?????????xsi:schemaLocation????λ???е?????????
????xmlns:aop??
????????????????????????????????Щ????淶????????????AOP????
????xmlns:tx:
????Spring?????????????????????
???????XML????????????????????????????淶??
?????????????xml?????beans????????????????????????????????????????aop????????к?????????????????????????aop:xxx???????????????aop:config??
?????????????????xmlns???????aop????????????淶???????xml?п??????дconfig?????????