IOC (Reverse control) and AOP (Aspect-Oriented Programming) in spring-spring)

Source: Internet
Author: User

I. IOC
Basic injection and set Injection


Entity-class Bean -- configuration file (applicationcontext) -- Test)

1.1 Entity Bean: Property + getter () method + setter () method

public class MyBean {private String url;private String name;private String pwd;private int age;private List list;private Set set;private Map map;private Properties prop;public String getUrl() { return url;}public void setUrl(String url) { this.url = url;}

// ---- The getter () and setter () Methods of other attributes are omitted here ----

1.2 spring configuration file: applicationcontext. xml

<?xml version="1.0" encoding="UTF-8"?><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-3.0.xsd"> <bean id="myBean" class="jorton.MyIoc.MyBean">   <property name="url">     <value>www.163.com</value>   </property>   <property name="name">     <value>xuliang</value>   </property>   <property name="pwd" value="123456"></property>   <property name="age">     <value>25</value>   </property>   <property name="list">     <list>       <value>ibm</value>       <value>sun</value>       <value>oracle</value>     </list>   </property>   <property name="set">     <set>       <value>music</value>       <value>sleeping</value>       <value>sports</value>     </set>   </property>   <property name="map">     <map>       <entry key="mySister" value="yanmei"></entry>       <entry key="myMother" value="wenxian"></entry>       <entry key="myFather" value="guoxian"></entry>     </map>   </property> </bean></beans>



1.3 Test class: used to passThe getbean (...) method of applicationcontext gets the property value of mybean.

import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;public class Test { public static void main(String[] args) {   ApplicationContext ctx = new ClassPathXmlApplicationContext(       "jorton/MyIoc/applicationContext.xml");   MyBean myBean = (MyBean) ctx.getBean("myBean");   System.out.println(myBean.getUrl());   System.out.println(myBean.getName());   System.out.println(myBean.getPwd());   System.out.println(myBean.getAge());   System.out.println(myBean.getList());   System.out.println(myBean.getSet());   System.out.println(myBean.getMap());   System.out.println(); }}



--------------------------- Running result -----------------------------
Www.163.com
Xuliang
123456
25
[IBM, sun, Oracle]
[Music, sleeping, sports]
{Mysister = Yanmei, mymother = Wenxian, myfather = guoxian}
---------------------------------------------------------------------

2. AOP (Aspect-Oriented Programming)
Aspect Oriented Programming
Target object and transactionaspect -- applicationcontext -- Test)

2.1 target object class (targetobject)

Public class targetobject {public void add () {system. Out. println ("I added something... ");} Public void Update () {system. Out. println (" I updated something... ");}}



2.2Transactionaspect)

Import org. aspectj. Lang. proceedingjoinpoint; public class transactionaspect {public void beforemethod () {system. Out. println ("the operation before the object method starts... ");} Public void aftermethod () {system. Out. println (" operation after the object method... ");} Public object roundmethod (proceedingjoinpoint pjp) throws throwable {system. Out. println (" surround method... "); Object = pjp. Proceed (); system. Out. println (" surround method .... "); Return object;} public void throwmethod () {New runtimeexception (); system. Out. println (" the operation is executed due to an exception... ");} Public void afterreturningmethod () {system. Out. println (" --- afterreturning -- operation... ");}}


2.3Configuration file (applicationcontext)

<? XML version = "1.0" encoding = "UTF-8"?> <Beans xmlns = "http://www.springframework.org/schema/beans" xmlns: AOP = "http://www.springframework.org/schema/aop" xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance" xsi: schemalocation = "http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-3.0.xsdhttp://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd"> <! -- Target object --> <bean id = "targetobject" class = "tarena. myaop. targetobject"> </bean> <! -- Cut surface object --> <bean id = "transactionaspect" class = "tarena. myaop. transactionaspect"> </bean> <AOP: config> <! -- Define aspect --> <AOP: aspect id = "myaspect" ref = "transactionaspect"> <! -- Pointcut --> <AOP: pointcut id = "transcut" expression = "execution (* jorton. myaop. *. * (...)"/> <! -- Define notification -- pre-notification, post-notification, final notification, and surround notification> <AOP: Before pointcut-ref = "transcut" method = "beforemethod"/> <AOP: after pointcut-ref = "transcut" method = "aftermethod"/> <AOP: After-returning pointcut-ref = "transcut" method = "afterreturningmethod"/> <AOP: around pointcut-ref = "transcut" method = "roundmethod"/> </AOP: aspect> </AOP: config> </beans>

 

2.4Test class (TeSt)

import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;public class AOPTest { public static void main(String[] args){   ApplicationContext ctx=new ClassPathXmlApplicationContext(       "tarena/myAop/applicationContext.xml");   TargetObject targetObject= (TargetObject) ctx.getBean("targetObject");   targetObject.add(); }}

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.