A consultant
An expression of notification (notification adviser packaging / enhancer)
Advisor:
name matching method:
NameMecthMethodPointcutAdvisor
1. Define a traffic class
cn.spring.advisor Package; / * * * service interface * / public interface the IService { // business methods public void doSome (); public void say (); }
2, an enhanced definition the class that implements the interface enhancements
package cn.spring.advisor; public class IServiceImpl implements IService{ @Override public void doSome() { System.out.println("=============真实业务============="); } @Override public void say() { System.out.println("=============say============="); } }
3.applicationContext.xml
The traffic classes and the enhancements which is injected into the container Spring
<!--注入业务Bean-->
<bean id="iServiceImpl" class="cn.spring.advisor.IServiceImpl"></bean>
<!--切面/通知-->
<bean id="myAdvisor" class="cn.spring.advisor.MyAdvisor"></bean>
Packing enhanced use of consultants
<bean id="advisor" class="org.springframwork.aop.support.NameMecthMethodPointcutAdvisor"> <property name="advice" ref="MyAdvisor"/> <property name="mapperNames" value="*do*"/>
</bean>
Agent proxy object using the project generator
<bean id="proxyFactory" class="ProxyFactoryBean"> <property name="in" value="advisor"/> <property name="target" ref="IService"/> </bean>
Automatic proxy adviser:
default automatic proxy adviser
applicationContext.xml
1. The definition of a service class
2. The definition of a class that implements enhanced enhanced interfaces
3.applicationContext.xml
3.1 to traffic classes and among the enhancements Spring poured into the container
3.2 using enhanced packaging consultants
<bean id = "advisor" class = "org .springframwork.aop.support.RegrexMethodPointcutAdvisor ">
<Property name =" the advice "REF =" MyAdvisor "/>
<Property name =" pattens "value =." do *. * "/>
</ the bean>
3.3 automatic proxy consultants
<bean class = "DefaultAdvisorAutoProxyCreator" / >
BeanName automatic proxy advisers
1 definition of a service class 2 . Reinforcement defines a class that implements an interface enhancement . 3 .applicationContext.xml . 3 .1 traffic classes and among the enhancements Spring poured into the container . 3 .2 packaging consultant using enhanced <the bean ID = " Advisor " class = " org.springframwork.aop.support.RegrexMethodPointcutAdvisor " > <Property name = " the advice " REF = " MyAdvisor " /> <Property name = " pattens " value = " . do *. * " /> </ the bean>3 .3 automatic proxy advisers <bean class="BeanNameAutoProxyCreator"> <property name="beanNames" value="iService"/> <property name="Inters" value="advisor"/> </bean>
Annotation .IOC two
IOC annotations need requires a large profile, scan package <context: compoent-scan base- package = "cn.spring" />
injection: the injection Bean Spring container
@Compoent achieve the injected traffic Bean
@Repository Dao specialized injection based @Compoent
@Target (ElementType.TYPE {})
@Retention (RetentionPolicy.RUNTIME)
@Documented
@Component
public @interface the Repository {
/**
* The value may indicate a suggestion for a logical component name,
* to be turned into a Spring bean in case of an autodetected component.
* @return the suggested component name, if any (or empty String otherwise)
*/
@AliasFor(annotation = Component.class)
String value() default "";
}
@Service special injection-Service
@Controller specifically target control layer
Positioning:
@Resource default name according to Bean positioning, if the name is empty, according to Type positioned
@Autowired default according to ByType injection once more compatible types appear, then we need to distinguish @Qualifier name
Case:
Dao layer interface
public interface IUserInfoMapper { public int addUser(UserInfo info); }
IUserInfoMapperImpl class implements the interface Dao
@Repository public class IUserInfoMapperImpl implements IUserInfoMapper { @Override public int addUser(UserInfo info) { System.out.println("添加成功"); return 1; } }
IUserInfoService接口
public interface IUserInfoService { public int addUser(UserInfo info); }
IUserInfoServiceImpl类实现了IUserInfoService接口
/** * @Service代表Service实现类的 标识,要让Spring管理Service */ @Service("iUserInfoServiceImpl") public class IUserInfoServiceImpl implements IUserInfoService{ //植入Dao层 //@Resource默认是根据ByName的方式,但是一旦名字为空,就根据ByType @Autowired private IUserInfoMapper iUserInfoMapper; @Override public int addUser(UserInfo info) { return iUserInfoMapper.addUser(info); } }
applicationContext.xml
<!--扫描注解:包扫描器--> <context:component-scan base-package="cn.spring"/>
三、注解增强
业务类
package cn.spring.aop; import org.springframework.stereotype.Service; /** * 业务类 */ @Service("IdoSomeService") public class IdoSomeService { public void doSome(){ System.out.println("业务当中的doSome方法"); } public void say(){ System.out.println("业务当中的say方法"); } }
增强类
package cn.spring.aop; import org.aspectj.lang.annotation.AfterReturning; import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.Before; import org.aspectj.lang.annotation.Pointcut; import org.springframework.stereotype.Component; /** * 增强类 */ @Aspect @Component public class MyAdvice { //定义一个空方法,为了可以应用切点表达式 @Pointcut("execution(* *..aop.*.do*(..))") public void pointCut(){ } //自定义增强方法 @Before("pointCut()") public void before(){ System.out.println("=================前置增强==================="); } //自定义方法增强 @AfterReturning("pointCut()") public void after(){ System.out.println("===============后置增强=============="); } }
applicationContext.xml
<!--开启AOP注解支持--> <aop:aspectj-autoproxy/>
Test测试类
public class AopTest { public static void main(String[] args) { ApplicationContext ctx=new ClassPathXmlApplicationContext("applicationContext.xml"); IdoSomeService bea=(IdoSomeService)ctx.getBean("IdoSomeService"); bea.doSome(); } }