集中管理类第二版
目录(单击可跳转对应目录)
1.初步认识SPRING.......................................................... 1
1.1简单认识............................................................ 1
1.2简单DEMO........................................................... 2
1.3细节讨论............................................................ 4
1.4本节小结............................................................ 6
1.5接口编程配合SPRING开发............................................. 7
2.配置BEAN................................................................ 9
2.1详细讲解配置BEAN................................................... 9
2.2生命周期BEAN的.................................................... 10
2.3配置数组........................................................... 11
2.4配置LIST集合...................................................... 12
2.5配置Map集合....................................................... 13
2.6用SET、构造、REF、内部来配置BEAN.................................. 14
2.7自动配置........................................................... 18
2.8自动与手动可以混合使用............................................. 21
2.9SPRING提供的特殊BEAN.............................................. 21
3.面相切面编程AOP........................................................ 23
3.1切面编程概述....................................................... 23
3.2简单的DEMO........................................................ 25
4.配置SRPING详解......................................................... 32
4.1配置SPIRNGMVC的XML文件解读....................................... 32
**************************************************************************************************************************************************************
1.初步认识SPRING
1.1简单认识
①SPRING是容器框架,用于配置bean并维护bean之间关系的框架;bean是java中的任何对象,可以是javabean/service/action/数据源/dao/ioc控制反转/di依赖注入;
**************************************************************************************************************************************************************
1.2简单DEMO
①开发一个SRPING项目
②引包,SPRING的包,日志包commons-logging,studyDelete.xml
③POJO类
package com.day.studyDelete.service;
public class UserService {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public void sayHello(){
System.out.println("你好 "+name);
}
}
④Main类
package com.day.studyDelete.test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.day.studyDelete.service.UserService;
public class Test {
public static void main(String args[]){
//传统
/*UserService userService=new UserService();
userService.setName("宋江");
userService.sayHello();*/
//spring
ApplicationContext ac=new ClassPathXmlApplicationContext("/spring/studyDelete.xml");
UserService userService=(UserService) ac.getBean("userService");
userService.sayHello();
}
}
⑤studyDelete.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" xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd ">
<!-- 配置bean,spring自动创建实例,UserService的set方法必须写,否则难注入 。这里就体现了注入的概念-->
<bean id="userService" class="com.day.studyDelete.service.UserService">
<property name="name">
<value>诸葛亮</value>
</property>
</bean>
</beans>
**************************************************************************************************************************************************************
1.3细节讨论
①studyDelete.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" xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd ">
<!-- 配置bean,spring自动创建实例,UserService的set方法必须写,否则难注入 。这里就体现了注入的概念-->
<bean id="userService" class="com.day.studyDelete.service.UserService">
<property name="name">
<value>诸葛亮</value>
</property>
</bean>
<bean id="beyService" class="com.day.studyDelete.service.BeyService">
<property name="name" value="卧龙" />
</bean>
</beans>
②POJO类
package com.day.studyDelete.service;
public class UserService {
private String name;
private BeyService beyService;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public void sayHello(){
System.out.println("你好 "+name);
beyService.sayBey();
}
public BeyService getBeyService() {
return beyService;
}
public void setBeyService(BeyService beyService) {
this.beyService = beyService;
}
}
③studyDelete.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" xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd ">
<!-- 配置bean,spring自动创建实例,UserService的set方法必须写,否则难注入 。这里就体现了注入的概念-->
<bean id="userService" class="com.day.studyDelete.service.UserService">
<property name="name">
<value>诸葛亮</value>
</property>
<!-- 在 userService引入配置的另外一个bean 这就体现了类与类之间的关系维护了用ref-->
<property name="beyService" ref="beyService"/>
</bean>
<bean id="beyService" class="com.day.studyDelete.service.BeyService">
<property name="name" value="卧龙" />
</bean>
</beans>
**************************************************************************************************************************************************************
1.4本节小结
①SPRING实际上是一个容器框架,可以理解为map,可以配置各种bean,并且可以维护bean与bean之间的关系,当我们需要使用某个bean,我们使用getBean("XXX")使用即可。
②IOC—>Inversion of Control控制反转,就是把创建对象,维护对象的关系的权利从程序中转移到spring容器中。程序本身不再维护
③学习框架最重要的就是学习配置。
④di是什么 Dependency Injection依赖注入和ioc是差不多的,只是认为di更能准确的表达spring的作用。
⑤工具类保证单态
//工具类保证单态
package com.day.studyDelete.util;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class ACUtil {
private static ApplicationContext ac=null;
private ACUtil(){
}
static{
ac=new ClassPathXmlApplicationContext("/spring/studyDelete.xml");
}
public static ApplicationContext getAC(){
return ac;
}
}
⑥SPIRNG的层次图
**************************************************************************************************************************************************************
1.5接口编程配合SPRING开发
①接口编程配合spring开发可以更好的达到程序间(层与层)的解耦
②一个ChangeLetter接口
package com.day.studyDelete.myInterface;
public interface ChangeLetter {
public String change();
}
③两个实现接口的类
package com.day.studyDelete.myInterfaceImp;
import com.day.studyDelete.myInterface.ChangeLetter;
public class BigLetter implements ChangeLetter{
private String str;
public String change() {
return str.toUpperCase();
}
public String getStr() {
return str;
}
public void setStr(String str) {
this.str = str;
}
}
package com.day.studyDelete.myInterfaceImp;
import com.day.studyDelete.myInterface.ChangeLetter;
public class SmallLetter implements ChangeLetter{
private String str;
public String change() {
return str.toLowerCase();
}
public String getStr() {
return str;
}
public void setStr(String str) {
this.str = str;
}
}
④两个类配置到SPRING容器中
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd ">
<!-- 配置bean,spring自动创建实例,UserService的set方法必须写,否则难注入 。这里就体现了注入的概念-->
<!-- <bean id="changeLetter" class="com.day.studyDelete.myInterfaceImp.BigLetter">
<property name="str" value="abc" />
</bean> -->
<bean id="changeLetter" class="com.day.studyDelete.myInterfaceImp.SmallLetter">
<property name="str" value="ABC" />
</bean>
</beans>
⑤实现开发
package com.day.studyDelete.test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.day.studyDelete.myInterface.ChangeLetter;
import com.day.studyDelete.myInterfaceImp.BigLetter;
import com.day.studyDelete.service.BeyService;
import com.day.studyDelete.service.UserService;
import com.day.studyDelete.util.ACUtil;
public class Test {
public static void main(String args[]){
//spring
ApplicationContext ac=ACUtil.getAC();
/*BigLetter bigLetter=(BigLetter) ac.getBean("changeLetter");
System.out.println(bigLetter.change());*/
//使用接口访问bean
ChangeLetter changeLetter=(ChangeLetter) ac.getBean("changeLetter");
System.out.println(changeLetter.change());
}
}
**************************************************************************************************************************************************************
2.配置BEAN
2.1详细讲解配置BEAN
①ac应用上下文bean;作用域配置,配置下创建两次,打印对象就能看到;
②加载ac的时候,spring容器中的bean就已经被创建了;
③bean工厂获取bean,有点类似延迟加载;
**************************************************************************************************************************************************************
2.2生命周期BEAN的
**************************************************************************************************************************************************************
2.3配置数组
①文件SPRING.XML配置
<property name="empName">
<list>
<value>诸葛亮</value>
<value>刘备</value>
<value>张飞</value>
<value>赵云</value>
</list>
</property>
②主类代码
package com.day.studyDelete.test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.day.studyDelete.service.Department;
import com.day.studyDelete.util.ACUtil;
public class Test {
public static void main(String args[]){
//spring
ApplicationContext ac=ACUtil.getAC();
/*BigLetter bigLetter=(BigLetter) ac.getBean("changeLetter");
System.out.println(bigLetter.change());*/
//使用接口访问bean
Department department=(Department) ac.getBean("department");
System.out.println(department.getName());
for(String empName:department.getEmpName()){
System.out.println(empName);
}
}
}
**************************************************************************************************************************************************************
2.4配置LIST集合
①实体类
package com.day.studyDelete.service;
import java.util.List;
public class Department {
private String name;
private String empName[];
private List<Employee> empList;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String[] getEmpName() {
return empName;
}
public void setEmpName(String[] empName) {
this.empName = empName;
}
public List<Employee> getEmpList() {
return empList;
}
public void setEmpList(List<Employee> empList) {
this.empList = empList;
}
}
②文件SPRING.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" xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd ">
<!-- 配置bean,spring自动创建实例,UserService的set方法必须写,否则难注入 。这里就体现了注入的概念 -->
<bean id="department" class="com.day.studyDelete.service.Department">
<property name="name" value="IT部门" />
<property name="empName">
<list>
<value>诸葛亮</value>
<value>刘备</value>
<value>张飞</value>
<value>赵云</value>
</list>
</property>
<property name="empList">
<list>
<ref bean="employee1"/>
<ref bean="employee2"/>
</list>
</property>
</bean>
<bean id="employee1" class="com.day.studyDelete.service.Employee">
<property name="name" value="诸葛亮" />
</bean>
<bean id="employee2" class="com.day.studyDelete.service.Employee">
<property name="name" value="司马懿" />
</bean>
</beans>
**************************************************************************************************************************************************************
2.5配置Map集合
①实体类
②文件SPRING.XML配置
③主函数
package com.day.studyDelete.test;
import java.util.Map.Entry;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.day.studyDelete.service.Department;
import com.day.studyDelete.service.Employee;
import com.day.studyDelete.util.ACUtil;
public class Test {
public static void main(String args[]){
//spring
ApplicationContext ac=ACUtil.getAC();
/*BigLetter bigLetter=(BigLetter) ac.getBean("changeLetter");
System.out.println(bigLetter.change());*/
//使用接口访问bean
Department department=(Department) ac.getBean("department");
for(Entry<String,Employee> entry:department.getEmpMap().entrySet()){
System.out.println(entry.getKey()+" "+entry.getValue().getName());
}
}
}
**************************************************************************************************************************************************************
2.6用SET、构造、REF、内部来配置BEAN
①SET的方式注入基本形式
<!-- 使用Set方式注入,主要掌握的方式 -->
<!--
<span style="color:#ff0000;"><bean id="helloService" class="com.xiami.spring.HelloWorld"
abstract="false" lazy-init="default" autowire="default">
<property name="str">
<value type="java.lang.String">hello world</value>
</property>
</bean></span>
-->
<!-- 使用构造方法方式注入 ,保证构造参数匹配,而且存在,不然会报错。 -->
<bean id="helloService" class="com.xiami.spring.HelloWorld"
abstract="false" lazy-init="default" autowire="default">
<constructor-arg>
<value type="java.lang.String">构造方法注入方式</value>
</constructor-arg>
</bean>
②通过指向REF注入与内部注入
ref注入:
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> <!-- 在spring框架加载时创建 -->
<bean id="borrowDao" class="dao.impl.BorrowDao" primary="true" lazy-init="true">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
内部bean的方式注入:
<bean id="orderservice" class="cn.itcast.service.OrderServiceBean" primary="true">
<property name="orderDao">
<bean class = "cn.itcast.service.OrderServiceBean"/>//不再使用ref
</property>
</bean>
采用这种方式的时候,每次注入的cn.itcast.service.OrderServiceBean只能为orderservice服务;
③Properties用for与Entry循环遍历
A.实体类
package com.day.studyDelete.service;
import java.util.List;
import java.util.Map;
import java.util.Properties;
public class Department {
private String name;
private String empName[];
private List<Employee> empList;
private Map<String,Employee> empMap;
private Properties properties;
public Properties getProperties() {
return properties;
}
public void setProperties(Properties properties) {
this.properties = properties;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String[] getEmpName() {
return empName;
}
public void setEmpName(String[] empName) {
this.empName = empName;
}
public List<Employee> getEmpList() {
return empList;
}
public void setEmpList(List<Employee> empList) {
this.empList = empList;
}
public Map<String, Employee> getEmpMap() {
return empMap;
}
public void setEmpMap(Map<String, Employee> empMap) {
this.empMap = empMap;
}
}
B.文件SPRING.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" xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd ">
<!-- 配置bean,spring自动创建实例,UserService的set方法必须写,否则难注入 。这里就体现了注入的概念 -->
<bean id="department" class="com.day.studyDelete.service.Department">
<property name="name" value="IT部门" />
<property name="empName">
<list>
<value>诸葛亮</value>
<value>刘备</value>
<value>张飞</value>
<value>赵云</value>
</list>
</property>
<property name="empList">
<list>
<ref bean="employee1" />
<ref bean="employee2" />
<ref bean="employee1" />
<ref bean="employee2" />
</list>
</property>
<!-- 给map配置值 -->
<property name="empMap">
<map>
<entry key="1" value-ref="employee1"></entry>
<entry key="1" value-ref="employee2"></entry>
<entry key="2" value-ref="employee2"></entry>
<entry key="3" value-ref="employee2"></entry>
</map>
</property>
<!-- 给属性集合配置值 -->
<property name="properties">
<props>
<prop key="properties1">A</prop>
<prop key="properties2">Hi</prop>
<prop key="properties3">Hello</prop>
</props>
</property>
</bean>
<bean id="employee1" class="com.day.studyDelete.service.Employee">
<property name="name" value="诸葛亮" />
</bean>
<bean id="employee2" class="com.day.studyDelete.service.Employee">
<property name="name" value="司马懿" />
</bean>
</beans>
C.主类,遍历与Map集合遍历很像
package com.day.studyDelete.test;
import java.util.Map.Entry;
import java.util.Properties;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.day.studyDelete.service.Department;
import com.day.studyDelete.service.Employee;
import com.day.studyDelete.util.ACUtil;
public class Test {
public static void main(String args[]){
//spring
ApplicationContext ac=ACUtil.getAC();
/*BigLetter bigLetter=(BigLetter) ac.getBean("changeLetter");
System.out.println(bigLetter.change());*/
//使用接口访问bean
Department department=(Department) ac.getBean("department");
for(Entry<Object,Object> entry:department.getProperties().entrySet()){
System.out.println(entry.getKey()+" "+entry.getValue());
}
}
}
**************************************************************************************************************************************************************
2.7自动配置
①自动配置概念
②实体类狗
package com.day.studyDelete.service;
public class Dog {
private String name;
private int age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
③实体类主人
package com.day.studyDelete.service;
public class Master {
private String name;
private Dog dog;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Dog getDog() {
return dog;
}
public void setDog(Dog dog) {
this.dog = dog;
}
}
④文件SPRING.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" xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd ">
<!-- 配置Master -->
<bean id="master" class="com.day.studyDelete.service.Master" autowire="byName">
<property name="name" value="诸葛亮" />
<!-- 传统,黄色部分用自动装备就可以省略了 -->
<!-- <property name="dog" ref="dog"/> -->
</bean>
<!-- 配置Dog -->
<bean id="dog" class="com.day.studyDelete.service.Dog">
<property name="name" value="大黄" />
<property name="age" value="3" />
</bean>
</beans>
⑤主体类
package com.day.studyDelete.test;
import java.util.Map.Entry;
import java.util.Properties;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.day.studyDelete.service.Master;
import com.day.studyDelete.util.ACUtil;
public class Test {
public static void main(String args[]){
//spring
ApplicationContext ac=ACUtil.getAC();
Master master=(Master) ac.getBean("master");
System.out.println(master.getName()+"—>养了一只—>"+master.getDog().getName());
}
}
**************************************************************************************************************************************************************
2.8自动与手动可以混合使用
**************************************************************************************************************************************************************
2.9SPRING提供的特殊BEAN
①特殊BEAN的作用
②SPRING中BEAN的种类
A.自己维护的BEAN
B.SPRING提供的特殊的数据库BEAN
3.面相切面编程AOP
3.1切面编程概述
3.2简单的DEMO
①文件SPRING.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" xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd ">
<!-- 配置被代理对象 -->
<bean id="serviceOne" class="com.day.studyDelete.service.ServiceOne">
<property name="name" value="诸葛亮" />
</bean>
<!-- 配置前置通知 -->
<bean id="myMethodBeforeAdvice" class="com.day.studyDelete.advice.MyMethodBeforeAdvice" />
<!-- 配置后置通知 -->
<bean id="myAfterReturningAdivce" class="com.day.studyDelete.advice.MyAfterReturningAdivce" />
<!-- 配置代理对象 -->
<bean id="proxyFactoryBean" class="org.springframework.aop.framework.ProxyFactoryBean">
<!-- 配置代理接口集合 -->
<property name="proxyInterfaces">
<list>
<value>com.day.studyDelete.myInterface.MyInterface</value>
<value>com.day.studyDelete.myInterface.MyInterfaceTwo</value>
</list>
</property>
<!-- 把通知织入到代理对象 ,就是拦截器 -->
<property name="interceptorNames">
<list>
<!-- 织入前置通知 -->
<value>myMethodBeforeAdvice</value>
<!-- 织入后置通知 -->
<value>myAfterReturningAdivce</value>
</list>
</property>
<!-- 配置被代理对象 -->
<property name="target" ref="serviceOne" />
</bean>
</beans>
②环绕通知的配置
A.实体类配置
package com.day.studyDelete.advice;
import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
public class MyMethodInterceptor implements MethodInterceptor {
public Object invoke(MethodInvocation invocation) throws Throwable {
// TODO Auto-generated method stub
System.out.println("调用方法前执行....");
Object object=invocation.proceed();
System.out.println("调用方法后执行....");
System.out.println("———————————————————分割线——————————————————————————");
return object;
}
}
————————————————————————————————————package com.day.studyDelete.advice;
import java.lang.reflect.Method;
import org.springframework.aop.ThrowsAdvice;
public class MyThrowsAdvice implements ThrowsAdvice {
//该接口是标识性接口,没有任何方法,必须满足一下样式
public void afterThrowing(Method m,Object[] os,Object targets,Exception e){
System.out.println("系统有BUG"+e.getMessage());
System.out.println("———————————————————分割线——————————————————————————");
}
}
B.文件SPRING.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" xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd ">
<!-- 配置被代理对象 -->
<bean id="serviceOne" class="com.day.studyDelete.service.ServiceOne">
<property name="name" value="诸葛亮" />
</bean>
<!-- 配置前置通知 -->
<bean id="myMethodBeforeAdvice" class="com.day.studyDelete.advice.MyMethodBeforeAdvice" />
<!-- 配置后置通知 -->
<bean id="myAfterReturningAdivce" class="com.day.studyDelete.advice.MyAfterReturningAdivce" />
<!-- 配置环绕对象 -->
<bean id="myMethodInterceptor" class="com.day.studyDelete.advice.MyMethodInterceptor" />
<!-- 配置异常对象 -->
<bean id="myThrowsAdvice" class="com.day.studyDelete.advice.MyThrowsAdvice" />
<!-- 配置代理对象 -->
<bean id="proxyFactoryBean" class="org.springframework.aop.framework.ProxyFactoryBean">
<!-- 配置代理接口集合 -->
<property name="proxyInterfaces">
<list>
<value>com.day.studyDelete.myInterface.MyInterface</value>
<value>com.day.studyDelete.myInterface.MyInterfaceTwo</value>
</list>
</property>
<!-- 把通知织入到代理对象 ,就是拦截器 -->
<property name="interceptorNames">
<list>
<!-- 织入前置通知 -->
<value>myMethodBeforeAdvice</value>
<!-- 织入后置通知 -->
<value>myAfterReturningAdivce</value>
<!-- 织入环绕通知 -->
<value>myMethodInterceptor</value>
<!-- 织入异常通知 -->
<value>myThrowsAdvice</value>
</list>
</property>
<!-- 配置被代理对象 -->
<property name="target" ref="serviceOne" />
</bean>
</beans>
③自定义切入点,那些方法使用通知。要使用引入通知
A.文件SPRING.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" xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd ">
<!-- 配置被代理对象 -->
<bean id="serviceOne" class="com.day.studyDelete.service.ServiceOne">
<property name="name" value="诸葛亮" />
</bean>
<!-- 配置前置通知 -->
<bean id="myMethodBeforeAdvice" class="com.day.studyDelete.advice.MyMethodBeforeAdvice" />
<!-- 配置后置通知 -->
<bean id="myAfterReturningAdivce" class="com.day.studyDelete.advice.MyAfterReturningAdivce" />
<!-- 配置环绕对象 -->
<bean id="myMethodInterceptor" class="com.day.studyDelete.advice.MyMethodInterceptor" />
<!-- 配置异常对象 -->
<bean id="myThrowsAdvice" class="com.day.studyDelete.advice.MyThrowsAdvice" />
<!-- 定义前置通知切入点 -->
<bean id="myNameMatchMethodPointcutAdvisor"
class="org.springframework.aop.support.NameMatchMethodPointcutAdvisor">
<property name="advice" ref="myMethodBeforeAdvice"></property>
<property name="mappedNames">
<list>
<value>sayHello</value>
</list>
</property>
</bean>
<!-- 配置代理对象 -->
<bean id="proxyFactoryBean" class="org.springframework.aop.framework.ProxyFactoryBean">
<!-- 配置代理接口集合 -->
<property name="proxyInterfaces">
<list>
<value>com.day.studyDelete.myInterface.MyInterface</value>
<value>com.day.studyDelete.myInterface.MyInterfaceTwo</value>
</list>
</property>
<!-- 把通知织入到代理对象 ,就是拦截器 -->
<property name="interceptorNames">
<list>
<!-- 织入前置通知 -->
<value>myNameMatchMethodPointcutAdvisor</value>
<!-- 织入后置通知 -->
<value>myAfterReturningAdivce</value>
<!-- 织入环绕通知 -->
<value>myMethodInterceptor</value>
<!-- 织入异常通知 -->
<value>myThrowsAdvice</value>
</list>
</property>
<!-- 配置被代理对象 -->
<property name="target" ref="serviceOne" />
</bean>
</beans>
**************************************************************************************************************************************************************
4.配置SRPING详解
4.1配置SPIRNGMVC的XML文件解读
①配置的具体解释