Spring_IOC / AOP comment
A, IOC comment
1. Spring for injecting into bean container:
- @Component: injecting into Spring bean container
- @Repository: a label layer Dao
- @Service: Service label for the business layer
- @Controller: a controller class label
2. for obtaining data to achieve fitting assembly Bean
- @Autowired: ByType default mode, if the class of the same name appears, it is not required in accordance with Type implanting ID specified @Qualifier
- . @ Resource: default ByName way, if the name does default in accordance with the injection ByType way
Case of IOC comment
(1) Large package annotations added profile scanner, package
<context:component-scan base-package="cn.spring.aop"/>
base-package is filled in the package name, package names with a plurality of ',' to distinguish
(2) The definition and implementation class corresponding interface
@Repository("mapperImpl") public class MapperImpl implements Mapper { @Override public int add(StudentBean stu) { System.out.println("123"); return 0; } }
@Service("StuServiceImpl") public class StuServiceImpl implements StuService { @Resource private Mapper mapper; @Override public int add(StudentBean stu) { return mapper.add(stu); } }
(3) Test Class
@Test public void IocTestBy(){ ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml"); StuService stuServiceImpl = (StuService)context.getBean("StuServiceImpl"); stuServiceImpl.add(new StudentBean()); }
Two, AOP notes Jane Shao
- @Aspect: Statement section
- @Ponitcut: public statement tangent point expression
- @Before: Front enhanced
- @AfterReturning: Rear enhanced
- @Around: Enhanced Surround
- @AfterThrowing: Enhanced exception is thrown
- @After: ultimately enhance
Example: Notes achieve a variety of enhancements
(1) Large configuration file open aop comment
<aop:aspectj-autoproxy/>
(2) interface
@Service("aopServices") public class AopServices{ public void doSome() throws Exception{ /* int i=1/0;*/ System.out.println("Service层业务"); } }
(3) Enhanced Class
@Aspect @Component public class Advices { // set pointcut expression @Pointcut ( "Execution (AOP .. * *. *. * (..))") public void Pointcut () { } // front reinforcing @Before ( "Pointcut ()") public void before () { System.out.println ( "front ========= ======="); } // rear reinforcing @ afterReturning ( "Pointcut ()") public void the afterReturning () { System.out.println ( "rear ======= =========="); } // surround enhancement @Around ( "Pointcut ()") public Object Around (JP ProceedingJoinPoint) throws the Throwable { System.out.println ( "surround front ======= =========="); = Jp.proceed the proceed Object (); System.out.println ( "surround rear ======= =========="); return the proceed; } // abnormal enhancement of @AfterThrowing (pointcut = "Execution (.. .. AOP * * * * (..))", the throwing = "EX") public void AfterThrowing (exception EX) { System.out.println ( "abnormal"); } // final enhanced @After ( "Pointcut ()") public void the After () { System.out.println ( "final by ========= ======="); } }
(4) Test Class
public class AopTest { public static void main(String[] args) { ApplicationContext context=new ClassPathXmlApplicationContext("applicaton.xml"); AopServices aopServices = (AopServices)context.getBean("aopServices"); try { aopServices.doSome(); } catch (Exception e) { e.printStackTrace(); } } }
A, IOC comment
1. Spring for injecting into bean container:
- @Component: injecting into Spring bean container
- @Repository: a label layer Dao
- @Service: Service label for the business layer
- @Controller: a controller class label
2. for obtaining data to achieve fitting assembly Bean
- @Autowired: ByType default mode, if the class of the same name appears, it is not required in accordance with Type implanting ID specified @Qualifier
- . @ Resource: default ByName way, if the name does default in accordance with the injection ByType way
Case of IOC comment
(1) Large package annotations added profile scanner, package
<context:component-scan base-package="cn.spring.aop"/>
base-package is filled in the package name, package names with a plurality of ',' to distinguish
(2) The definition and implementation class corresponding interface
@Repository("mapperImpl") public class MapperImpl implements Mapper { @Override public int add(StudentBean stu) { System.out.println("123"); return 0; } }
@Service("StuServiceImpl") public class StuServiceImpl implements StuService { @Resource private Mapper mapper; @Override public int add(StudentBean stu) { return mapper.add(stu); } }
(3) Test Class
@Test public void IocTestBy(){ ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml"); StuService stuServiceImpl = (StuService)context.getBean("StuServiceImpl"); stuServiceImpl.add(new StudentBean()); }
Two, AOP notes Jane Shao
- @Aspect: Statement section
- @Ponitcut: public statement tangent point expression
- @Before: Front enhanced
- @AfterReturning: Rear enhanced
- @Around: Enhanced Surround
- @AfterThrowing: Enhanced exception is thrown
- @After: ultimately enhance
Example: Notes achieve a variety of enhancements
(1) Large configuration file open aop comment
<aop:aspectj-autoproxy/>
(2) interface
@Service("aopServices") public class AopServices{ public void doSome() throws Exception{ /* int i=1/0;*/ System.out.println("Service层业务"); } }
(3) Enhanced Class
@Aspect @Component public class Advices { // set pointcut expression @Pointcut ( "Execution (AOP .. * *. *. * (..))") public void Pointcut () { } // front reinforcing @Before ( "Pointcut ()") public void before () { System.out.println ( "front ========= ======="); } // rear reinforcing @ afterReturning ( "Pointcut ()") public void the afterReturning () { System.out.println ( "rear ======= =========="); } // surround enhancement @Around ( "Pointcut ()") public Object Around (JP ProceedingJoinPoint) throws the Throwable { System.out.println ( "surround front ======= =========="); Object jp.proceed the proceed = (); System.out.println ( "surround rear ======= =========="); return the proceed; } // abnormal enhancement of @AfterThrowing (pointcut = "execution (* * .. aop .. * * (..)) ", the throwing =" EX ") public void AfterThrowing (exception EX) { System.out.println (" abnormal "); } // ultimately enhancing @After (" Pointcut () " ) public void the After () { System.out.println ( "final by ========= ======="); } }
(4) Test Class
public class AopTest { public static void main(String[] args) { ApplicationContext context=new ClassPathXmlApplicationContext("applicaton.xml"); AopServices aopServices = (AopServices)context.getBean("aopServices"); try { aopServices.doSome(); } catch (Exception e) { e.printStackTrace(); } } }