
AOP綾?/span>package cc.apl330.aspect;
public class UserAspect {
public void before() {
System.out.println("method before!");
}
}

鏁版嵁搴撴搷浣?/span>package cc.apl330.dao;
import cc.apl330.model.User;
public interface IDAO {
public void save(User user) ;
}

鏁版嵁搴撴搷浣?/span>package cc.apl330.dao;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import javax.sql.DataSource;
import cc.apl330.model.User;
public class UserDAO implements IDAO {
private DataSource dataSource ;
public void save(User user) {
int id = user.getId();
String name = user.getName() ;
String sql = "INSERT INTO USER(_id,_name) VALUE(?,?)" ;
Connection conn = null ;
PreparedStatement ps = null ;
try {
conn = dataSource.getConnection() ;
ps = conn.prepareStatement(sql) ;
ps.setInt(1, id) ;
ps.setString(2, name) ;
ps.executeUpdate() ;
} catch (SQLException e) {
e.printStackTrace();
}finally{
try {
ps.close() ;
conn.close() ;
} catch (SQLException e) {
e.printStackTrace();
}
}
}
public DataSource getDataSource() {
return dataSource;
}
public void setDataSource(DataSource dataSource) {
this.dataSource = dataSource;
}
}

涓氬姟灞?span class="cnblogs_code_Collapse" id="Code_Closed_Text_259591">package cc.apl330.service;
import cc.apl330.dao.IDAO;
import cc.apl330.dao.UserDAO;
import cc.apl330.model.User;
public class UserService {
private UserDAO userdao = null ;
public void add(User user) {
userdao.save(user) ;
}
public UserDAO getUserdao() {
return userdao;
}
public void setUserdao(UserDAO userdao) {
this.userdao = userdao;
}
}

嫻嬭瘯綾?/span>package cc.apl330.service;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import cc.apl330.model.User;
public class UserServiceTest {
@Test
public void save() {
ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml") ;
UserService userService = (UserService) ctx.getBean("userService");
User user = new User() ;
user.setName("apl330") ;
userService.add(user);
}
}

sping閰嶇疆鏂囦歡<?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">
<bean id="dao" class="cc.apl330.dao.UserDAO" >
<property name="dataSource" ref="dataSource"></property>
</bean>
<bean id="user" class="cc.apl330.model.User" scope="prototype"></bean>
<bean id="userService" class="cc.apl330.service.UserService">
<property name="userdao" ref="dao"></property>
</bean>
<bean id="addbefore" class="cc.apl330.aspect.UserAspect"></bean>
<aop:config>
<aop:pointcut
expression="execution(public * cc.apl330.service.*.add(..))"
id="userpoincut"/>
<aop:aspect id="useraspect" ref="addbefore" >
<aop:before method="before" pointcut-ref="userpoincut"/>
</aop:aspect>
</aop:config>
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
<property name="url" value="jdbc:mysql://localhost:3306/spring"></property>
<property name="username" value="root"></property>
<property name="password" value="359848184"></property>
</bean>
</beans>