1.AspectJ
@Aspect public class MyAspect { //前置增强 @Before(value = "execution(* *..Spring17AspectJ.*.*(..))") public void myBefore(){ System.out.println("===我是前置增强内容======"); } //后置增强 @AfterReturning(value = "execution(* *..Spring17AspectJ.*.*(..))") public void myAferReturing(){ System.out.println("===我是after内容======"); } //环绕增强 @Around(value = "execution(* *..Spring17AspectJ.*.*(..))") public Object myAround(ProceedingJoinPoint proceed) throws Throwable { System.out.println("===我是环绕前内容======"); Object result = proceed.proceed(); System.out.println("===我是环绕后内容======"); if (result!=null){ String str=(String)result; return str.toUpperCase(); }else{ return null; } } //异常增强 @Pointcut("execution(* *..Spring17AspectJ.*.*(..))") private void pointcut(){}; @AfterThrowing public void throwing(){ System.out.println("出错了"); } //最终增强 @Pointcut("execution(* *..Spring17AspectJ.*.insert(..))") private void insert(){}; @Pointcut("execution(* *..Spring17AspectJ.*.delete(..))") private void delete(){}; @Pointcut("execution(* *..Spring17AspectJ.*.update(..))") private void update(){}; @After("execution(* *..Spring17AspectJ.*.*(..))") public void after(){ System.out.println("我是最终增强"); } }
public class SomeService implements ISomeService { //核心业务 public void doSome(){ System.out.println("我们都要找到"); } public String add() { System.out.println("==============add============"); return "add"; } }
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 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 "> <!--目标对象--> <bean id="someService" class="cn.happy.Spring17AspectJ.SomeService"></bean> <!--增强通知--> <bean class="cn.happy.Spring17AspectJ.MyAspect"></bean> <aop:aspectj-autoproxy></aop:aspectj-autoproxy> </beans>
@Test //AspectJ 注解 public void test17(){ ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext17.xml"); ISomeService service = (ISomeService) ctx.getBean("someService"); service.doSome(); String result = service.add(); System.out.println(result); }
2.JDBC Template
public class Book { private Integer bookid; private String bookname; private Integer bookprice; public Integer getBookid() { return bookid; } public void setBookid(Integer bookid) { this.bookid = bookid; } public String getBookname() { return bookname; } public void setBookname(String bookname) { this.bookname = bookname; } public Integer getBookprice() { return bookprice; } public void setBookprice(Integer bookprice) { this.bookprice = bookprice; } }
public class BookDaoImpl implements BookDao{ private JdbcTemplate jdbcTemplate; public List<Book> findAll() { String sql="select * from book"; List<Book> list=this.getJdbcTemplate().query(sql, new RowMapper<Book>() { public Book mapRow(ResultSet rs, int i) throws SQLException { Book book=new Book(); book.setBookid(rs.getInt("bookid")); book.setBookname(rs.getString("bookname")); book.setBookprice(rs.getInt("bookprice")); return book; } }); return list; } public void setJdbcTemplate(JdbcTemplate jdbcTemplate) { this.jdbcTemplate = jdbcTemplate; } public JdbcTemplate getJdbcTemplate() { return jdbcTemplate; } }
public class BookServiceImpl implements BookService{ private BookDao dao; public List<Book> findAll() { return dao.findAll(); } public BookDao getDao() { return dao; } public void setDao(BookDao dao) { this.dao = dao; } }
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 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 "> <!--00.识别jdbc.properties文件--> <context:property-placeholder location="jdbc.properties"></context:property-placeholder> <!--c3p0--> <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> <property name="driverClass" value="${jdbc.driverClassName}"></property> <property name="jdbcUrl" value="${jdbc.url}"></property> <property name="user" value="${jdbc.user}"></property> <property name="password" value="${jdbc.password}"></property> </bean> <!--<!–dbcp–>--> <!--<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">--> <!--<property name="driverClassName" value="${jdbc.driverClassName}"></property>--> <!--<property name="url" value="${jdbc.url}"></property>--> <!--<property name="username" value="${jdbc.user}"></property>--> <!--<property name="password" value="${jdbc.password}"></property>--> <!--</bean>--> <!--<!–alibaba–>--> <!--<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">--> <!--<property name="driverClassName" value="${jdbc.driverClassName}"></property>--> <!--<property name="url" value="${jdbc.url}"></property>--> <!--<property name="username" value="${jdbc.user}"></property>--> <!--<property name="password" value="${jdbc.password}"></property>--> <!--</bean>--> <!--01.建立数据源 ${} Spring 内置的一个数据源 DriverManager--> <!--<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">--> <!--<property name="driverClassName" value="${jdbc.driverClassName}"></property>--> <!--<property name="url" value="${jdbc.url}"></property>--> <!--<property name="username" value="${jdbc.user}"></property>--> <!--<property name="password" value="${jdbc.password}"></property>--> <!--</bean>--> <!--02.jdbcTemplate 配置--> <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"> <property name="dataSource" ref="dataSource"></property> </bean> <!--03.dao配置--> <bean id="bookDao" class="cn.happy.Spring19jdbcTemplate.dao.impl.BookDaoImpl"> <property name="jdbcTemplate" ref="jdbcTemplate"></property> </bean> <!--04.service bookService--> <bean id="bookService" class="cn.happy.Spring19jdbcTemplate.service.impl.BookServiceImpl"> <property name="dao" ref="bookDao"></property> </bean> </beans>
@Test //jdbc template public void test18(){ ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext19.xml"); BookService service = (BookService) ctx.getBean("bookService"); List<Book> list = service.findAll(); for (Book item:list) { System.out.println(item.getBookname()); } }