1、Spring 框架的 AOP
java
1.Spring 中基於 AOP 的 @AspectJ mysql
package com.liandy.model;
import java.util.*;
import org.aspectj.lang.annotation.Before;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Configuration;
@Configuration
public class Student {
private String name;
private int age;
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public void printThrowException(){
System.out.println("Exception raised");
throw new IllegalArgumentException();
}
}
package com.liandy.model;
import org.aspectj.lang.annotation.*;
@Aspect
public class Logging {
@Pointcut("execution(* com.liandy.model.*.*(..))")
private void selectAll(){}
/**
* This is the method which I would like to execute
* before a selected method execution.
*/
@Before("selectAll()")
public void beforeAdvice(){
System.out.println("Going to setup student profile.");
}
/**
* This is the method which I would like to execute
* after a selected method execution.
*/
@After("selectAll()")
public void afterAdvice(){
System.out.println("Student profile has been setup.");
}
/**
* This is the method which I would like to execute
* when any method returns.
*/
@AfterReturning(pointcut = "selectAll()", returning="retVal")
public void afterReturningAdvice(Object retVal){
System.out.println("Returning:" + retVal.toString() );
}
/**
* This is the method which I would like to execute
* if there is an exception raised.
*/
@AfterThrowing(pointcut = "selectAll()", throwing = "ex")
public void AfterThrowingAdvice(IllegalArgumentException ex){
System.out.println("There has been an exception- " + ex.toString());
}
}
<?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-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd"> <aop:aspectj-autoproxy/> <!-- bean definitions go here --> <bean id="student" class="com.liandy.model.Student"> <property name="name" value="jake"></property> <property name="age" value="12"></property> </bean> <bean id="logging" class="com.liandy.model.Logging"/> </beans>
package com.liandy.main; import org.springframework.beans.factory.BeanFactory; import org.springframework.beans.factory.xml.XmlBeanFactory; import org.springframework.context.ApplicationContext; import org.springframework.context.ConfigurableApplicationContext; import org.springframework.context.annotation.AnnotationConfigApplicationContext; import org.springframework.context.support.AbstractApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import org.springframework.core.io.ClassPathResource; import com.liandy.model.Cat; import com.liandy.model.CustomEventPublisher; import com.liandy.model.Foo; import com.liandy.model.FooConfig; import com.liandy.model.HelloWorld; import com.liandy.model.Student; import com.liandy.model.TestBean; import com.liandy.model.TestConfiguration; import com.liandy.service.UserService; public class App { public static void main(String[] args) throws InterruptedException { ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml"); Student student = (Student) context.getBean("student"); student.getName(); student.getAge(); student.printThrowException(); } }
2、JDBC 框架概述
spring
1.Spring JDBC的示例 sql
package com.liandy.model; public class Customer { private int id; private String name; public Customer(String name) { super(); this.name = name; } public Customer(int id, String name) { // TODO Auto-generated constructor stub this.id=id; this.name=name; } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public void print() { System.out.print("id:"+this.id+"|name:"+this.name); } }
package service; import com.liandy.model.Customer; public interface CustomerDAO { public void insert(Customer customer); public Customer findByCustomerId(int custId); public void insertByJdbcTemplate(Customer customer); }
package service.imp; import java.sql.*; import javax.sql.DataSource; import org.springframework.jdbc.core.JdbcTemplate; import com.liandy.model.Customer; import service.CustomerDAO; public class JdbcCustomerDAO implements CustomerDAO { private DataSource dataSource; private JdbcTemplate jdbcTemplate; public void setDataSource(DataSource dataSource) { this.dataSource = dataSource; } public void insertByJdbcTemplate(Customer customer){ String sql = "INSERT INTO CUSTOMER " + "(id, NAME) VALUES (?, ?)"; jdbcTemplate = new JdbcTemplate(dataSource); jdbcTemplate.update(sql, new Object[] { customer.getId(), customer.getName() }); } public void insert(Customer customer){ String sql = "INSERT INTO CUSTOMER " + "(id,name) VALUES (?, ?)"; Connection conn = null; try { conn = dataSource.getConnection(); PreparedStatement ps = conn.prepareStatement(sql); ps.setInt(1, customer.getId()); ps.setString(2, customer.getName()); ps.executeUpdate(); ps.close(); } catch (SQLException e) { throw new RuntimeException(e); } finally { if (conn != null) { try { conn.close(); } catch (SQLException e) {} } } } public Customer findByCustomerId(int custId){ String sql = "SELECT * FROM CUSTOMER WHERE id = ?"; Connection conn = null; try { conn = dataSource.getConnection(); PreparedStatement ps = conn.prepareStatement(sql); ps.setInt(1, custId); Customer customer = null; ResultSet rs = ps.executeQuery(); if (rs.next()) { customer = new Customer( rs.getInt("id"), rs.getString("name") ); } rs.close(); ps.close(); return customer; } catch (SQLException e) { throw new RuntimeException(e); } finally { if (conn != null) { try { conn.close(); } catch (SQLException e) {} } } } }
package com.liandy.main; import org.springframework.beans.factory.BeanFactory; import org.springframework.beans.factory.xml.XmlBeanFactory; import org.springframework.context.ApplicationContext; import org.springframework.context.ConfigurableApplicationContext; import org.springframework.context.annotation.AnnotationConfigApplicationContext; import org.springframework.context.support.AbstractApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import org.springframework.core.io.ClassPathResource; import service.CustomerDAO; import com.liandy.model.Cat; import com.liandy.model.CustomEventPublisher; import com.liandy.model.Customer; import com.liandy.model.Foo; import com.liandy.model.FooConfig; import com.liandy.model.HelloWorld; import com.liandy.model.Student; import com.liandy.model.TestBean; import com.liandy.model.TestConfiguration; import com.liandy.service.UserService; public class App { public static void main(String[] args) throws InterruptedException { ApplicationContext context = new ClassPathXmlApplicationContext( "ApplicationContext.xml"); CustomerDAO dao=(CustomerDAO) context.getBean("customerDAO"); dao.insert(new Customer("aa")); } }
<?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-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd"> <aop:aspectj-autoproxy/> <!-- bean definitions go here --> <import resource="springDatasource.xml" /> <import resource="springCustomer.xml" /> </beans>
<beans xmlns="http://www.springframework.org/schema/beans" 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-2.5.xsd"> <bean id="customerDAO" class="service.imp.JdbcCustomerDAO"> <property name="dataSource" ref="dataSource" /> </bean> </beans>
<beans xmlns="http://www.springframework.org/schema/beans" 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-2.5.xsd"> <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <property name="driverClassName" value="com.mysql.jdbc.Driver" /> <property name="url" value="jdbc:mysql://localhost:3306/mydb?useUnicode=true" /> <property name="username" value="root" /> <property name="password" value="" /> </bean> </beans>
2.事務管理數據庫
• 原子性:事務應該看成一個單獨單元的操做,這意味着整個序列操做要麼是成功,要麼是失敗的。
• 一致性:這表示數據庫的引用完整性的一致性,表中惟一的主鍵等。
• 隔離性:可能同時處理不少有相同的數據集的事務,每一個事務應該與其餘事務隔離,以防止數據損壞。
• 持久性:一個事務一旦完成所有操做後,這個事務的結果必須是永久性的,不能因系統故障而從數據庫中刪
除 app