package tech.pdai.springframework.entity;
/**
* @author pdai
*/
public class User {
/**
* user's name.
*/
private String name;
/**
* user's age.
*/
private int age;
/**
* init.
*
* @param name name
* @param age age
*/
public User(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
DAO 获取 POJO, UserDaoServiceImpl (mock 数据)
package tech.pdai.springframework.dao;
import java.util.Collections;
import java.util.List;
import tech.pdai.springframework.entity.User;
/**
* @author pdai
*/
public class UserDaoImpl {
/**
* init.
*/
public UserDaoImpl() {
}
/**
* mocked to find user list.
*
* @return user list
*/
public List<User> findUserList() {
return Collections.singletonList(new User("pdai", 18));
}
}
并增加daos.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="www.springframework.org/schema/beans"
xmlns:xsi="www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="www.springframework.org/schema/beans
www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="userDao" class="tech.pdai.springframework.dao.UserDaoImpl">
<!-- additional collaborators and configuration for this bean go here -->
</bean>
<!-- more bean definitions for data access objects go here -->
</beans>
业务层 UserServiceImpl(调用DAO层)
package tech.pdai.springframework.service;
import java.util.List;
import tech.pdai.springframework.dao.UserDaoImpl;
import tech.pdai.springframework.entity.User;
/**
* @author pdai
*/
public class UserServiceImpl {
/**
* user dao impl.
*/
private UserDaoImpl userDao;
/**
* init.
*/
public UserServiceImpl() {
}
/**
* find user list.
*
* @return user list
*/
public List<User> findUserList() {
return this.userDao.findUserList();
}
/**
* set dao.
*
* @param userDao user dao
*/
public void setUserDao(UserDaoImpl userDao) {
this.userDao = userDao;
}
}
并增加services.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="www.springframework.org/schema/beans"
xmlns:xsi="www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="www.springframework.org/schema/beans
www.springframework.org/schema/beans/spring-beans.xsd">
<!-- services -->
<bean id="userService" class="tech.pdai.springframework.service.UserServiceImpl">
<property name="userDao" ref="userDao"/>
<!-- additional collaborators and configuration for this bean go here -->
</bean>
<!-- more bean definitions for services go here -->
</beans>
拦截所有service中的方法,并输出记录
package tech.pdai.springframework.aspect;
import java.lang.reflect.Method;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
/**
* @author pdai
*/
@Aspect
public class LogAspect {
/**
* aspect for every methods under service package.
*/
@Around("execution(* tech.pdai.springframework.service.*.*(..))")
public Object businessService(ProceedingJoinPoint pjp) throws Throwable {
// get attribute through annotation
Method method = ((MethodSignature) pjp.getSignature()).getMethod();
System.out.println("execute method: " + method.getName());
// continue to process
return pjp.proceed();
}
}
并增加aspects.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="www.springframework.org/schema/beans"
xmlns:xsi="www.w3.org/2001/XMLSchema-instance"
xmlns:aop="www.springframework.org/schema/aop"
xmlns:context="www.springframework.org/schema/context"
xsi:schemaLocation="www.springframework.org/schema/beans
www.springframework.org/schema/beans/spring-beans.xsd
www.springframework.org/schema/aop
www.springframework.org/schema/aop/spring-aop.xsd
www.springframework.org/schema/context
www.springframework.org/schema/context/spring-context.xsd
">
<context:component-scan base-package="tech.pdai.springframework" />
<aop:aspectj-autoproxy/>
<bean id="logAspect" class="tech.pdai.springframework.aspect.LogAspect">
<!-- configure properties of aspect here as normal -->
</bean>
<!-- more bean definitions for data access objects go here -->
</beans>
组装App
package tech.pdai.springframework;
import java.util.List;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import tech.pdai.springframework.entity.User;
import tech.pdai.springframework.service.UserServiceImpl;
/**
* @author pdai
*/
public class App {
/**
* main interfaces.
*
* @param args args
*/
public static void main(String[] args) {
// create and configure beans
ApplicationContext context =
new ClassPathXmlApplicationContext("aspects.xml", "daos.xml", "services.xml");
// retrieve configured instance
UserServiceImpl service = context.getBean("userService", UserServiceImpl.class);
// use configured instance
List<User> userList = service.findUserList();
// print info from beans
userList.forEach(a -> System.out.println(a.getName() + "," + a.getAge()));
}
}
/**
* aspect for every methods under service package.
*/
@Around("execution(* tech.pdai.springframework.service.*.*(..))")
public Object businessService(ProceedingJoinPoint pjp) throws Throwable {
// get attribute through annotation
Method method = ((MethodSignature) pjp.getSignature()).getMethod();
System.out.println("execute method: " + method.getName());
// continue to process
return pjp.proceed();
}
更进一步,你便能理解为何会有如下的知识点了:
Spring 框架通过定义切面, 通过拦截切点实现了不同业务模块的解耦,这个就叫面向切面编程 - Aspect Oriented Programming (AOP)
/**
* @author pdai
*/
@Repository
public class UserDaoImpl {
/**
* mocked to find user list.
*
* @return user list
*/
public List<User> findUserList() {
return Collections.singletonList(new User("pdai", 18));
}
}
/**
* @author pdai
*/
@Service
public class UserServiceImpl {
/**
* user dao impl.
*/
@Autowired
private UserDaoImpl userDao;
/**
* find user list.
*
* @return user list
*/
public List<User> findUserList() {
return userDao.findUserList();
}
}
在App中扫描tech.pdai.springframework包
package tech.pdai.springframework;
import java.util.List;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import tech.pdai.springframework.entity.User;
import tech.pdai.springframework.service.UserServiceImpl;
/**
* @author pdai
*/
public class App {
/**
* main interfaces.
*
* @param args args
*/
public static void main(String[] args) {
// create and configure beans
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(
"tech.pdai.springframework");
// retrieve configured instance
UserServiceImpl service = context.getBean(UserServiceImpl.class);
// use configured instance
List<User> userList = service.findUserList();
// print info from beans
userList.forEach(a -> System.out.println(a.getName() + "," + a.getAge()));
}
}
package tech.pdai.springframework.entity;
/**
* @author pdai
*/
public class User {
/**
* user's name.
*/
private String name;
/**
* user's age.
*/
private int age;
/**
* init.
*
* @param name name
* @param age age
*/
public User(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
DAO 获取 POJO, UserDaoServiceImpl (mock 数据)
package tech.pdai.springframework.dao;
import java.util.Collections;
import java.util.List;
import tech.pdai.springframework.entity.User;
/**
* @author pdai
*/
public class UserDaoImpl {
/**
* init.
*/
public UserDaoImpl() {
}
/**
* mocked to find user list.
*
* @return user list
*/
public List<User> findUserList() {
return Collections.singletonList(new User("pdai", 18));
}
}
并增加daos.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="www.springframework.org/schema/beans"
xmlns:xsi="www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="www.springframework.org/schema/beans
www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="userDao" class="tech.pdai.springframework.dao.UserDaoImpl">
<!-- additional collaborators and configuration for this bean go here -->
</bean>
<!-- more bean definitions for data access objects go here -->
</beans>
业务层 UserServiceImpl(调用DAO层)
package tech.pdai.springframework.service;
import java.util.List;
import tech.pdai.springframework.dao.UserDaoImpl;
import tech.pdai.springframework.entity.User;
/**
* @author pdai
*/
public class UserServiceImpl {
/**
* user dao impl.
*/
private UserDaoImpl userDao;
/**
* init.
*/
public UserServiceImpl() {
}
/**
* find user list.
*
* @return user list
*/
public List<User> findUserList() {
return this.userDao.findUserList();
}
/**
* set dao.
*
* @param userDao user dao
*/
public void setUserDao(UserDaoImpl userDao) {
this.userDao = userDao;
}
}
并增加services.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="www.springframework.org/schema/beans"
xmlns:xsi="www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="www.springframework.org/schema/beans
www.springframework.org/schema/beans/spring-beans.xsd">
<!-- services -->
<bean id="userService" class="tech.pdai.springframework.service.UserServiceImpl">
<property name="userDao" ref="userDao"/>
<!-- additional collaborators and configuration for this bean go here -->
</bean>
<!-- more bean definitions for services go here -->
</beans>
拦截所有service中的方法,并输出记录
package tech.pdai.springframework.aspect;
import java.lang.reflect.Method;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
/**
* @author pdai
*/
@Aspect
public class LogAspect {
/**
* aspect for every methods under service package.
*/
@Around("execution(* tech.pdai.springframework.service.*.*(..))")
public Object businessService(ProceedingJoinPoint pjp) throws Throwable {
// get attribute through annotation
Method method = ((MethodSignature) pjp.getSignature()).getMethod();
System.out.println("execute method: " + method.getName());
// continue to process
return pjp.proceed();
}
}
并增加aspects.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="www.springframework.org/schema/beans"
xmlns:xsi="www.w3.org/2001/XMLSchema-instance"
xmlns:aop="www.springframework.org/schema/aop"
xmlns:context="www.springframework.org/schema/context"
xsi:schemaLocation="www.springframework.org/schema/beans
www.springframework.org/schema/beans/spring-beans.xsd
www.springframework.org/schema/aop
www.springframework.org/schema/aop/spring-aop.xsd
www.springframework.org/schema/context
www.springframework.org/schema/context/spring-context.xsd
">
<context:component-scan base-package="tech.pdai.springframework" />
<aop:aspectj-autoproxy/>
<bean id="logAspect" class="tech.pdai.springframework.aspect.LogAspect">
<!-- configure properties of aspect here as normal -->
</bean>
<!-- more bean definitions for data access objects go here -->
</beans>
组装App
package tech.pdai.springframework;
import java.util.List;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import tech.pdai.springframework.entity.User;
import tech.pdai.springframework.service.UserServiceImpl;
/**
* @author pdai
*/
public class App {
/**
* main interfaces.
*
* @param args args
*/
public static void main(String[] args) {
// create and configure beans
ApplicationContext context =
new ClassPathXmlApplicationContext("aspects.xml", "daos.xml", "services.xml");
// retrieve configured instance
UserServiceImpl service = context.getBean("userService", UserServiceImpl.class);
// use configured instance
List<User> userList = service.findUserList();
// print info from beans
userList.forEach(a -> System.out.println(a.getName() + "," + a.getAge()));
}
}
/**
* aspect for every methods under service package.
*/
@Around("execution(* tech.pdai.springframework.service.*.*(..))")
public Object businessService(ProceedingJoinPoint pjp) throws Throwable {
// get attribute through annotation
Method method = ((MethodSignature) pjp.getSignature()).getMethod();
System.out.println("execute method: " + method.getName());
// continue to process
return pjp.proceed();
}
更进一步,你便能理解为何会有如下的知识点了:
Spring 框架通过定义切面, 通过拦截切点实现了不同业务模块的解耦,这个就叫面向切面编程 - Aspect Oriented Programming (AOP)
/**
* @author pdai
*/
@Repository
public class UserDaoImpl {
/**
* mocked to find user list.
*
* @return user list
*/
public List<User> findUserList() {
return Collections.singletonList(new User("pdai", 18));
}
}
/**
* @author pdai
*/
@Service
public class UserServiceImpl {
/**
* user dao impl.
*/
@Autowired
private UserDaoImpl userDao;
/**
* find user list.
*
* @return user list
*/
public List<User> findUserList() {
return userDao.findUserList();
}
}
在App中扫描tech.pdai.springframework包
package tech.pdai.springframework;
import java.util.List;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import tech.pdai.springframework.entity.User;
import tech.pdai.springframework.service.UserServiceImpl;
/**
* @author pdai
*/
public class App {
/**
* main interfaces.
*
* @param args args
*/
public static void main(String[] args) {
// create and configure beans
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(
"tech.pdai.springframework");
// retrieve configured instance
UserServiceImpl service = context.getBean(UserServiceImpl.class);
// use configured instance
List<User> userList = service.findUserList();
// print info from beans
userList.forEach(a -> System.out.println(a.getName() + "," + a.getAge()));
}
}