构造方法 注入
<bean id="simpleC" class="spring21.SimpleBean">
<constructor-arg index="0">
<value>李四</value>
</constructor-arg>
<constructor-arg index="1">
<value>123</value>
</constructor-arg>
</bean>
从spring容器中取对象
ApplicationContext context = null ;
context = new ClassPathXmlApplicationContext("applicationContext.xml") ;
SimpleBean simple = (SimpleBean)context.getBean("simple") ;
定义AOP spring1.x
applicationContext.xml
<beans>
<bean id="before" class="spring33.ProxySubjectBeforeAdvice">
</bean>
<bean id="after" class="spring33.ProxySubjectAfterAdvice">
</bean>
<bean id="realsubject" class="spring33.RealSubject">
</bean>
<bean id="proxy" class="org.springframework.aop.framework.ProxyFactoryBean">
<property name="proxyInterfaces">
<value>spring33.Subject</value>
</property>
<property name="target">
<ref bean="realsubject"/>
</property>
<property name="interceptorNames">
<list>
<value>after</value>
<value>before</value>
</list>
</property>
</bean>
</beans>
ProxySubjectBeforeAdvice.java
import java.lang.reflect.Method;
import org.springframework.aop.MethodBeforeAdvice;
public class ProxySubjectBeforeAdvice implements MethodBeforeAdvice {
public void before(Method arg0, Object[] arg1, Object arg2) throws Throwable {
System.out.println("择菜");
}
}
ProxySubjectAfterAdvice.java
import java.lang.reflect.Method;
import org.springframework.aop.AfterReturningAdvice;
public class ProxySubjectAfterAdvice implements AfterReturningAdvice {
public void afterReturning(Object arg0, Method arg1, Object[] arg2,
Object arg3) throws Throwable {
System.out.println("刷锅");
}
}
RealSubject.java
public class RealSubject implements Subject{
public void request(){
System.out.println("做菜");
}
}
Subject.java interface
public interface Subject {
public void request();
}
定义AOP 注解(Annotation) 采用Aspect定义切面 spring2.x
通过spring获取对象时,一定要通过接口获取
applicationContext.xml
<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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd">
<aop:aspectj-autoproxy/> <!-- 启用aspectj对Annotation的支持 -->
<bean id="securityHandler" class="com.cjc.spring.SecurityHandler"/>
<bean id="userManager" class="com.cjc.spring.UserManagerImpl"/><!--当本类的add*和del*方法执行的时候,会执行代理类方法-->
</beans>
代理类
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
@Aspect
public class SecurityHandler {
@Pointcut("execution(* add*(..)) || execution(* del*(..))")
//因为该方法只是个依赖的标识,所以定义成private
private void allAddMethod(){};
@Before("allAddMethod()")
private void checkSecurity() {
System.out.println("----------checkSecurity()---------------");
}
@After("allAddMethod()")
private void afterFunc() {
System.out.println("----------after---------------");
}
}
定义AOP 配置文件定义 spring2.x
applicationContext.xml
<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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd">
<bean id="securityHandler" class="com.bwie.SecurityHandler"/>
<bean id="userManager" class="com.bwie.UserManagerImpl"/>
<aop:config>
<aop:aspect id="security" ref="securityHandler">
<aop:pointcut id="allAddMethod" expression="execution(* com.bwie.UserManagerImpl.add*(..))"/>
<aop:before method="checkSecurity" pointcut-ref="allAddMethod"/>
<aop:after method="afterFunc" pointcut-ref="allAddMethod"/>
</aop:aspect>
</aop:config>
</beans
代理类
public class SecurityHandler {
private void checkSecurity() {
System.out.println("----------checkSecurity()---------------");
}
private void afterFunc() {
System.out.println("----------after---------------");
}
}
CGLIB
CGLIB是针对类实现代理,主要是对指定的类生成一个子类,覆盖其中的方法。因为是继承,所以该类或方法最好不要声明成final
Spring默认使用的是基于Java Dynamic Proxy模式实现,这样任何的接口都能被代理
如果一个业务对象没有实现某一个接口,那么CGLIB将被使用
spring1 声明性事务
<bean id="hibernateTransactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory">
<ref local="sessionFactory"/>
</property>
</bean>
<!-- Transactional proxy for the services -->
<bean id="hibernateTxProxy" lazy-init="true" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
<property name="transactionManager"><ref bean="hibernateTransactionManager"/></property>
<property name="transactionAttributes">
<props>
<prop key="add*">PROPAGATION_REQUIRED</prop>
<prop key="save*">PROPAGATION_REQUIRED</prop>
<prop key="remove*">PROPAGATION_REQUIRED</prop>
<prop key="load*">PROPAGATION_REQUIRED,readOnly</prop>
<prop key="get*">PROPAGATION_REQUIRED,readOnly</prop>
<prop key="do*">PROPAGATION_REQUIRED</prop>
<prop key="import*">PROPAGATION_REQUIRED</prop>
</props>
</property>
<property name="target"><bean class="com.bwie.common.db.BaseBiz" autowire="byName"/></property>
</bean>
spring2 声明性事务
applicationContext.xml
<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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd">
<bean id="test" class="com.Test">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<bean id="mysql" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName"
value="com.mysql.jdbc.Driver">
</property>
<property name="url" value="jdbc:mysql://localhost:3306/test"></property>
<property name="username" value="root"></property>
</bean>
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource">
<ref bean="mysql" />
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">
org.hibernate.dialect.MySQLDialect
</prop>
</props>
</property>
<property name="mappingResources">
<list>
<value>com/pojo/T1.hbm.xml</value>
</list>
</property>
</bean>
<!-- 事务管理器 -->
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<!-- 配置事务的传播特性 -->
<tx:advice id="advice" transaction-manager="transactionManager">
<tx:attributes >
<tx:method name="save*" propagation="REQUIRED"/>
<tx:method name="delete*" propagation="REQUIRED"/>
<tx:method name="update*" propagation="REQUIRED"/>
</tx:attributes>
</tx:advice>
<aop:config>
<aop:pointcut id="allmethod" expression="execution(* save*(..))"/>
<aop:advisor pointcut-ref="allmethod" advice-ref="advice"/>
</aop:config>
</beans>
Test.java 事务控制的类
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import com.pojo.T1;
public class Test extends HibernateDaoSupport implements ITest{
public void save()
{
T1 t1 = new T1();
getHibernateTemplate().save(t1);
int i = 1/0;
}
}