一、IOC
基本注入和集合注入
实体类Bean--配置文件(applicationContext)--测试类(Test)
1.1 实体Bean:属性+getter()方法+setter()方法
//----此处省略其他属性的getter()和setter()方法----
1.2 Spring配置文件: applicationContext.xml
1.3 Test类:用于通过 ApplicationContext的方法getBean(..)获取myBean的属性值。
---------------------------运行结果-----------------------------
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)
2.2 切面(TransactionAspect)
2.3 配置文件(applicationContext)
2.4 测试类(Test)
基本注入和集合注入
实体类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="tarena.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(
"tarena/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(* tarena.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();
}
}