<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:p="http://www.springframework.org/schema/p"
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
https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop
https://www.springframework.org/schema/aop/spring-aop.xsd">
beans>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-aopartifactId>
<version>5.1.8.RELEASEversion>
dependency>
<dependency>
<groupId>aopalliancegroupId>
<artifactId>aopallianceartifactId>
<version>1.0version>
dependency>
<dependency>
<groupId>org.aspectjgroupId>
<artifactId>aspectjweaverartifactId>
<version>1.9.4version>
dependency>
<bean id="HelloSpring" class="com.pojo.HelloSpring">
<property name="who">
<value>Springvalue>
property>
bean>
注解定义bean
<bean id="user" class="com.spring.demo.Person" p:name="zhang" p:age="">
bean>
<bean id="userService" class="com.spring.service.impl.userServiceImpl" p:dao-ref="userDao">
bean>
使用
<bean id="user" class="com.spring.demo.Person">
<property name="name" >
<value>value>
property>
bean>
aop是一种编程思想,面向切面(Aspect Oriented Programming)
<dependency>
<groupId>org.aspectjgroupId>
<artifactId>aspectjweaverartifactId>
<version>1.9.4version>
dependency>
<dependency>
<groupId>aopalliancegroupId>
<artifactId>aopallianceartifactId>
<version>1.0version>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-aopartifactId>
<version>5.1.9.RELEASEversion>
dependency>
public class Mysout {
public void around(ProceedingJoinPoint pj){
System.out.println("进入前置");
Object str=null;
try {
str=pj.proceed();
System.out.println("进入后置");
}catch (Throwable e){
System.out.println("进入异常");
}finally {
System.out.println("进入最终");
}
}
}
<bean id="mysout" class="com.utils.Mysout">bean>
<aop:config>
<aop:pointcut id="pointCut" expression="execution(* com..*.UserAction.UserFind())"/>
<aop:aspect ref="mysout">
<aop:before method="print" pointcut-ref="pointCut">aop:before>
<aop:after-throwing method="error" pointcut-ref="pointCut" throwing="ex">aop:after-throwing>
<aop:around method="around" pointcut-ref="pointCut" >aop:around>
aop:aspect>
aop:config>
@Component
import org.springframework.stereotype.Component;
@Repository:用于标注DAO类
import org.springframework.stereotype.Repository;
@Service:用于标注业务类
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Controller;
@Service("orderBiz")
public class OrderBizImpl implements OrderBiz {
@Resource(name = "orderMapper")
private OrderMapper orderMapper;
@Resource(name = "orderDetailMapper")
private OrderDetailMapper orderDetailMapper;
@Override
public List<Order> getOrderList() {
return orderMapper.getOrderList();
}
@Override
@Transactional
public Integer add(Order order, OrderDetail orderDetail) {
int rst= orderMapper.addOrder(order);
orderDetail.setOrderId(order.getId());
rst=orderDetailMapper.addOrderDetail(orderDetail);
return rst;
}
public OrderMapper getOrderMapper() {
return orderMapper;
}
public void setOrderMapper(OrderMapper orderMapper) {
this.orderMapper = orderMapper;
}
public OrderDetailMapper getOrderDetailMapper() {
return orderDetailMapper;
}
public void setOrderDetailMapper(OrderDetailMapper orderDetailMapper) {
this.orderDetailMapper = orderDetailMapper;
}
}
@Controller("lbwnb")
public class OrderControllerImpl implements OrderController{
@Resource(name = "orderBiz")
private OrderBiz orderBiz;
@Override
public List<Order> getOrderList() {
return orderBiz.getOrderList();
}
@Override
public Integer add(Order order, OrderDetail orderDetail) {
return orderBiz.add(order,orderDetail);
}
public OrderBiz getOrderBiz() {
return orderBiz;
}
public void setOrderBiz(OrderBiz orderBiz) {
this.orderBiz = orderBiz;
}
}
<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"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:p="http://www.springframework.org/schema/p"
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/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd">
<context:component-scan base-package="controller,biz">context:component-scan>
beans>
<tx:annotation-driven transaction-manager="txManager">tx:annotation-driven>
<tx:advice id="txAdvice" transaction-manager="txManager">
<tx:attributes>
<tx:method name="add" propagation="SUPPORTS"/>
tx:attributes>
tx:advice>
<aop:config>
<aop:pointcut id="pointcut" expression="execution(* biz..*.*(..))"/>
<aop:advisor advice-ref="txAdvice" pointcut-ref="pointcut">aop:advisor>
aop:config>
注解使用事务
@Transactional