IoC和AOP使用拓展
spring提供了通过构造方法赋值得能力,成为构造注入。
AOP(Aspect Oriented Programming):面向切面编程,也是一种变成思想。(静态角度)
OOP(Object Oriented Programming):面向对象编程。(动态代理:JDK动态代理 CGLIB动态代理)
代理模(Proxy):为其他对象提供一个代理以控制对这个对象的访问。
适合的场合:远程代理 虚拟代理 安全代理(只需要简单了解不做详解)
Spring的经典AOP配置方案
1、使用的是Aspectj第三方框架,实现了AOP思想
2、注解配置的AOP
3、纯POJO 就是一个普通的类aop:config
在spring中配置文件中将Demo对象一构造方法注入的方式赋值给User类对象相关属性的关键代码
上面Demo类可不根据属性顺序写哦。
使用p命名空间实现属性注入
使用p命名空间改进配置,注意使用前要添加P命名空间声明:
然后根据P标签给Demo里面的属性赋值
注入不同数据类型
Spring提供了不同标签来实现各种不同类型参数的注入,这些标签对于设值注入和构造注入都实用。
先创建两个类 1.TestEntity 2.User
TestEntity:
package com.bdqn.cn;
import java.util.*;
public class TestEntity {
private String specialCharacter1; // 特殊字符值1
private String specialCharacter2; // 特殊字符值2
private User innerBean; // JavaBean类型
private List<String> list; // List类型
private String[] array; // 数组类型
private Set<String> set; // Set类型
private Map<String, String> map; // Map类型
private Properties props; // Properties类型
private String emptyValue; // 注入空字符串值
private String nullValue = "init value"; // 注入null值
public void setSpecialCharacter1(String specialCharacter1) {
this.specialCharacter1 = specialCharacter1;
}
public void setSpecialCharacter2(String specialCharacter2) {
this.specialCharacter2 = specialCharacter2;
}
public void setInnerBean(User user) {
this.innerBean = user;
}
public void setList(List<String> list) {
this.list = list;
}
public void setArray(String[] array) {
this.array = array;
}
public void setSet(Set<String> set) {
this.set = set;
}
public void setMap(Map<String, String> map) {
this.map = map;
}
public void setProps(Properties props) {
this.props = props;
}
public void setEmptyValue(String emptyValue) {
this.emptyValue = emptyValue;
}
public void setNullValue(String nullValue) {
this.nullValue = nullValue;
}
public void showValue() {
System.out.println("特殊字符1:" + this.specialCharacter1);
System.out.println("特殊字符2:" + this.specialCharacter2);
System.out.println("内部Bean:" + this.innerBean.getUsername());
System.out.println("List属性:" + this.list);
System.out.println("数组属性[0]:" + this.array[0]);
System.out.println("Set属性:" + this.set);
System.out.println("Map属性:" + this.map);
System.out.println("Properties属性:" + this.props);
System.out.println("注入空字符串:[" + this.emptyValue + "]");
System.out.println("注入null值:" + this.nullValue);
}
@Override
public String toString() {
return "TestEntity{" +
"specialCharacter1='" + specialCharacter1 + '\'' +
", specialCharacter2='" + specialCharacter2 + '\'' +
", innerBean=" + innerBean +
", list=" + list +
", array=" + Arrays.toString(array) +
", set=" + set +
", map=" + map +
", props=" + props +
", emptyValue='" + emptyValue + '\'' +
", nullValue='" + nullValue + '\'' +
'}';
}
}
用户实体类:User类
配置对应的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.xsd">
<bean id="test" class="com.bdqn.cn.TestEntity">
<!--使用<![CDATA[]]>标记处理XML特殊字符-->
<property name="specialCharacter1">
<value><![CDATA[P&G]]></value>
</property>
<!--把XML特殊字符替换为实体引用-->
<property name="specialCharacter2">
<value>P&G</value>
</property>
<!--定义内部bean-->
<property name="innerBean">
<bean class="com.bdqn.cn.User">
<property name="username">
<value>Mr.Inner</value>
</property>
</bean>
</property>
<!--注入List类型-->
<property name="list">
<list>
<!--定义List中的元素-->
<value>足球</value>
<value>篮球</value>
</list>
</property>
<!--注入数组类型-->
<property name="array">
<set>
<!--定义set或数组中的元素-->
<value>足球</value>
<value>篮球</value>
</set>
</property>
<!--注入Map类型-->
<property name="map">
<map>
<!--定义Map中的键值对-->
<entry>
<key>
<value>basketball</value>
</key>
<value>篮球</value>
</entry>
</map>
</property>
<!--注入空字符串值-->
<property name="emptyValue">
<value></value>
</property>
<!--注入null值-->
<property name="nullValue">
<null/>
</property>
</bean>
</beans>
对应的测试类:
运行结果为:
TestEntity{specialCharacter1='P&G', specialCharacter2='P&G', [email protected], list=[足球, 篮球], array=[足球, 篮球], set=null, map={basketball=篮球}, props=null, emptyValue='', nullValue='null'}
使用注释定义bean
常用注解:
自动注入:@Resources,@Autowired
Bean定义:@Component、@Repository、@Service 和 @Constroller
@Component是个泛化概念,可以用在任何层次。如果是web开发,尽量用@Repository、@Service 和 @Constroller
bean的定义:
被称作bean的对象是构成应用程序的支柱也是由Spring IoC容器管理的。bean是一个被实例化,组装,并通过Spring IoC容器所管理的对象。这些bean是由用容器提供的配置元数据创建的。
bean定义包含称为配置元数据的信息,下述容器也需要知道配置元数据:
如何创建一个bean
bean的生命周期的详细信息
bean的依赖关系
使用注释定义bean:
首先在Spring配置文件中添加对content命名空间的声明,然后使用content命名空间下的component-scan标签扫描注释标注的类。base-package属性制定了需要扫描的标准包(多个包名可用逗号隔开)。Spring会扫描这些保重所有的类,获取bean的定义信息。
使用Bean实现IoC配置
xml文件配置:
数据访问层:
业务逻辑层:
测试类:
总结:
Spring提供了设置注入,构造注入等以来注入方式。
使用p命名空间可以简化属性注入的配置。
spring提供的增强处理类型包括前置增强,后置增强、异常抛出增强、环绕增强、最终增强等。
通过Schema形式将POJO的方法配置成切面,所有标签包括aop:aspect、aop:before、aop:after-returning、aop:around、aop:after-thorwing、aop:after等。
用来定义Bean组件的装配可以通过@Component、@Repository、@Service、@Controller。
Bean组件的装配可以通过@Autowried、@Qualifier以及@Resource实现。
在Spring配置文件中使用content:compontent-scan元素扫描包含注释的类,完成初始化。
使用注释方式定义切面可以简化配置功能,常用注释有@Aspect、@before、@AfterReturning、@Around、@AfterThrowing、@After等。
通过在配置文件中添加aop:aspectj-autoproxy元素,就可以启用对@AspectJ注释的支持。