编码爱好者

每天进步一点点

SPRING--Spring中IOC(反转控制) 和 AOP(面向方面编程)

一、IOC
基本注入和集合注入


实体类Bean--配置文件(applicationContext)--测试类(Test)

1.1 实体Bean:属性+getter()方法+setter()方法
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;
}

//----此处省略其他属性的getter()和setter()方法----

1.2 Spring配置文件: 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类:用于通过 ApplicationContext的方法getBean(..)获取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();
 }
}



---------------------------运行结果-----------------------------
www.163.com
xuliang
123456
25
[ibm, sun, oracle]
[music, sleeping, sports]
{mySister=yanmei, myMother=wenxian, myFather=guoxian}
---------------------------------------------------------------------

二、AOP(面向方面编程)
Aspect Oriented Programming
目标对象(TargetObject)和切面(TransactionAspect)--配置文件(applicationContext)--测试类(Test)

2.1 目标对象类(TargetObject)
public class TargetObject {
 public void add(){
   System.out.println("我添加了一些东西。。。");
 }
 public void update(){
 System.out.println("我更新了一些东西。。。");
 }
}



2.2 切面(TransactionAspect)
import org.aspectj.lang.ProceedingJoinPoint;

public class TransactionAspect {
 public void beforeMethod() {
   System.out.println("对象方法开始之前的操作。。。");
 }
 public void afterMethod() {
   System.out.println("对象方法之后的操作。。。");
 }
 public Object roundMethod(ProceedingJoinPoint pjp) throws Throwable {
   System.out.println("环绕方法上。。。");
   Object object = pjp.proceed();
   System.out.println("环绕方法下。。。。");
   return object;
 }
 public void throwMethod() {
   new RuntimeException();
   System.out.println("异常出现执行的操作。。。");
 }
 public void afterReturningMethod() {
   System.out.println("---afterReturning--的操作。。。");
 }
}



2.3 配置文件(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/beans
 http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
 http://www.springframework.org/schema/aop
 http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">
 <!-- 目标对象 -->
 <bean id="targetObject" class="tarena.myAop.TargetObject"></bean>
 <!-- 切面对象 -->
 <bean id="transactionAspect" class="tarena.myAop.TransactionAspect"></bean>
 <aop:config>
 <!-- 定义切面aspect-->
   <aop:aspect id="myAspect" ref="transactionAspect">
 <!--切入点pointcut-->
     <aop:pointcut id="transcut" expression="execution(* jorton.myAop.*.* (..))" />
 <!--定义通知--前置通知、后置通知、最终通知、环绕通知>
     <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.4 测试类(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();
 }
}


 

阅读更多
个人分类: frame框架
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭