Spring框架学习之 IOC & AOP
回顾:
hibernate 主配置文件、元数据文件mapping
struts2 主配置文件 package 不能重名
拦截器、异常处理、全局结果 - 针对当前包
spring管理对象的容器
spring搭建步骤
1.导包。4 + 2
beans+context+core+expression
log4j+apache-commons-logging
2.实体类
3.配置文件管理对象
约束:xsi + beans-prefix空
beans - bean[name class]
4.开启容器
ApplicationContext ClassPathXmlApplicationContext
5.获得对象 getBean
src下的 配置文件applicationContext.xml可以引入其他模块
< !-- 引入其他模块 -->
< import resource=“b_create/applicationContext.xml”/>
例如:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.springframework.org/schema/beans"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.3.xsd ">
<!-- 引入其他模块 -->
<import resource="b_create/applicationContext.xml"/>
</beans>
spring管理对象的方式
IOC:Inverse Of Control
反转控制 - 原理反射
主动创建对象,然后自己进行依赖注入
将创建对象的权限、依赖的注入操作交给Spring容器,
程序员只需要向容器索要对象
IOC需要有DI做支持:Dependency Injection
set方法
构造器
成员变量
基本类型:基本数据类型+String
引用类型
集合类型 array list set map properties
Spring中的DI 4种
容器:
ApplicationContext
spring容器创建对象的方式 3种
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.springframework.org/schema/beans"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.3.xsd ">
<!-- 管理对象 -->
<!-- spring容器创建对象的方式 3 种 -->
<!-- 1.构造器创建 -->
<bean name="user" class="domain.User"></bean>
<!-- 2.静态工厂方式:工厂类中提供静态方法 -->
<bean name="user1" class="b_create.Factory" factory-method="getUser" ></bean>
<!-- 3.工厂方法方式:工厂类中提供普通方法 -->
<!-- 管理factory对象 -->
<bean name="factory" class="b_create.Factory"></bean>
<bean name="user2" factory-method="newUser" factory-bean="factory"></bean>
</beans>
工厂创建对象 Factory.java
package b_create;
import domain.User;
public class Factory {
// 提供一个静态方法,获得对象
public static User getUser() {
System.out.println("static Factory");
return new User();
}
// 提供普通方法
public User newUser() {
System.out.println("Factory method");
return new User();
}
}
获得对象 Test02.java
package b_create;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import domain.User;
public class Test02 {
@Test
public void test01() {
// 1.开启spring容器 - 加载配置文件
ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
// 2.索要对象 - 通过无参构造器实例化
User user = (User) ac.getBean("user2");
System.out.println(user);
}
}
注入方式:
1.set注入(重点掌握) < bean name="" class="" scope="" init-method="" destroy-method="" p:属性> //p属性是在p命名空间时使用的
2.构造器注入(掌握)
3.p命名空间 - 添加命名空间(了解)
4.spel注入(了解)
5.复杂类型注入
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.springframework.org/schema/beans"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd">
<!-- 1.set注入 -->
<bean name="user" class="domain.User">
<!-- property属性
|- name: 属性名
|- value: 基本类型 值
|- ref: 注入的对象name
-->
<property name="name" value="lucy"></property>
<property name="age" value="19"></property>
<property name="car" ref="car1"></property>
</bean>
<bean name="car1" class="domain.Car" >
<property name="name" value="兰博基尼"></property>
<property name="color" value="黄色"></property>
</bean>
<!-- 2.构造器注入 -->
<bean name="user1" class="domain.User">
<!-- 构造器参数:
|— type:参数类型 - 完整类型
|- value:普通类型值
|- ref:引用类型值 对象name
|- index:参数的索引
-->
<constructor-arg type="String" value="tom" index="1" ></constructor-arg>
<constructor-arg type="Integer" value="20"></constructor-arg>
</bean>
<!-- 3.p命名空间注入 - 了解 -->
<bean name="user2" class="domain.User"
p:age="23" p:name="jack" p:car-ref="car1">
</bean>
<!-- 4.表达式spel注入 - 了解 -->
<bean name="user3" class="domain.User">
<property name="name" value="#{user1.name}"></property>
<property name="age" value="#{user.age}"></property>
</bean>
<!-- 复杂类型注入 -->
<bean name="user4" class="domain.User">
<!-- list类型 -->
<!-- 集合中只有一个值 -->
<!-- <property name="list" value="haha" ></property> -->
<!-- 集合中多个值 -->
<property name="list">
<list>
<value>haha</value>
<value>haha</value>
<value>haha</value>
<!-- <ref bean=""/> -->
</list>
</property>
<!-- array类型和list完全一样 -->
<!-- <property name="arr" value="lucy"></property> -->
<property name="arr">
<array>
<value>lucy</value>
<value>tom</value>
</array>
</property>
<!-- set类型和list、array一致,标签由list变为set标签 -->
<!-- map类型 -->
<property name="map">
<map>
<entry key="user1" value-ref="user"></entry>
</map>
</property>
<!-- properties类型,泛型全都是String -->
<property name="pros">
<props>
<prop key="driver">com.mysql.jdbc.Driver</prop>
</props>
</property>
</bean>
</beans>
实体类User.java
package domain;
import java.util.List;
import java.util.Map;
import java.util.Properties;
public class User {
private String name;
private Integer age;
private Car car;//引用
private List<String> list;//集合
private String[] arr;//数组
private Map<String, User> map;//Map
private Properties pros;//Properties 类似 Map<String,String>
public Properties getPros() {
return pros;
}
public void setPros(Properties pros) {
this.pros = pros;
}
public Map<String, User> getMap() {
return map;
}
public void setMap(Map<String, User> map) {
this.map = map;
}
public String[] getArr() {
return arr;
}
public void setArr(String[] arr) {
this.arr = arr;
}
public List<String> getList() {
return list;
}
public void setList(List<String> list) {
this.list = list;
}
public User(Integer age, String name) {
super();
System.out.println("age --- name");
this.name = name;
this.age = age;
}
public User(String name, Integer age) {
super();
System.out.println("name --- age");
this.name = name;
this.age = age;
}
public Car getCar() {
return car;
}
public void setCar(Car car) {
this.car = car;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public User() {
super();
System.out.println("User init");
}
// 为了实例化对象后,做的一些初始化动作
public void haha() {
System.out.println("haha");
}
// 为了在对象销毁之前执行的动作
public void destroy() {
System.out.println("User destroy");
}
@Override
public String toString() {
return "User [name=" + name + ", age=" + age + ", car=" + car + "]";
}
}
实体类Car.java
package domain;
public class Car {
private String name;
private String color;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
@Override
public String toString() {
return "Car [name=" + name + ", color=" + color + "]";
}
public Car() {
super();
// TODO Auto-generated constructor stub
}
}
测试类 Test03.java
package c_injection;
import java.util.Arrays;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import domain.User;
public class Test03 {
@Test
public void test01() {
// 1.开启spring容器 - 加载配置文件
ApplicationContext ac = new ClassPathXmlApplicationContext("c_injection/applicationContext.xml");
// 2.索要对象 - 通过无参构造器实例化
User user = (User) ac.getBean("user4");
// 容器中只有一个User类型的对象
//User bean = ac.getBean(User.class);
// System.out.println(bean);
System.out.println(user.getList());
System.out.println(Arrays.toString(user.getArr()));
System.out.println(user.getPros().getProperty("driver"));
}
}
注解 取代配置文件中 部分配置信息
注解:包 + aop
添加context命名空间
<context:component-scan base-package=""
使用注解需要在配置文件中开启文件扫描 - 需要在约束中加 context
base-package - 指定注解扫描的包,包下面所有的后代包都会被扫描
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.springframework.org/schema/beans"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd ">
<!-- 开启注解扫描
base-package: 指定注解扫描的包
包下面所有的后代包都会被扫描
-->
<context:component-scan base-package="d_annotation"></context:component-scan>
<bean name="user1" class="d_annotation.User" >
<property name="name" value="jack"></property>
<property name="age" value="30"></property>
</bean>
<bean name="car1" class="d_annotation.Car">
<property name="name" value="帕拉梅拉"></property>
<property name="color" value="土豪金"></property>
</bean>
</beans>
@Component(“name”)
Repository 持久层
Service 业务层
Controller 控制层(web) 这四个同一等级
@Value
@AutoWired
@Qualifier(“name”)
@Resource(name="")
@Scope(scopeName=“singleton”)
@PostConstrut
@PreDestroy
Car.java实体类 使用注解
package d_annotation;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
@Component
public class Car {
@Value("玛莎拉蒂")
private String name;
@Value("绿色")
private String color;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
@Override
public String toString() {
return "Car [name=" + name + ", color=" + color + "]";
}
public Car() {
super();
// TODO Auto-generated constructor stub
}
}
User.java实体类使用注解
package d_annotation;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import javax.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Repository;
import org.springframework.stereotype.Service;
@Component("user") // 默认添加名字 user - 类名首字母小写
// @Controller // web
// @Service // service
// @Repository // dao
@Scope(scopeName = "prototype")
public class User {
@Value("zhangsan")
private String name;
@Value("10")
private Integer age;
// @Autowired // 自动装配
// @Qualifier("car1")
@Resource(name = "car1")
private Car car;
private List<String> list;
private String[] arr;
private Map<String, User> map;
private Properties pros;
// 为了实例化对象后,做的一些初始化动作
@PostConstruct // init-method
public void haha() {
System.out.println("haha");
}
// 为了在对象销毁之前执行的动作
@PreDestroy // destroy-method
public void destroy() {
System.out.println("User destroy");
}
public Properties getPros() {
return pros;
}
public void setPros(Properties pros) {
this.pros = pros;
}
public Map<String, User> getMap() {
return map;
}
public void setMap(Map<String, User> map) {
this.map = map;
}
public String[] getArr() {
return arr;
}
public void setArr(String[] arr) {
this.arr = arr;
}
public List<String> getList() {
return list;
}
public void setList(List<String> list) {
this.list = list;
}
public User(Integer age, String name) {
super();
System.out.println("age --- name");
this.name = name;
this.age = age;
}
public User(String name, Integer age) {
super();
System.out.println("name --- age");
this.name = name;
this.age = age;
}
public Car getCar() {
return car;
}
public void setCar(Car car) {
this.car = car;
}
public String getName() {
return name;
}
// @Value("zhangsan") // 符合属性封装的特点
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public User() {
super();
System.out.println("User init");
}
@Override
public String toString() {
return "User [name=" + name + ", age=" + age + ", car=" + car + "]";
}
}
Test04.java测试类
package d_annotation;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Test04 {
@Test
public void test01() {
// 1.开启spring容器 - 加载配置文件
ApplicationContext ac = new ClassPathXmlApplicationContext("d_annotation/applicationContext.xml");
// 2.索要对象 - 通过无参构造器实例化
User user = (User) ac.getBean("user");
System.out.println(user);
}
}
Spring和JUnit的整合
Dao
service
导包:test // spring-test-4.3.9.RELEASE.jar
整合时重点 需要在 测试类中加上
@RunWith(SpringJUnit4ClassRunner.class)
// 指定容器对应的配置文件
@ContextConfiguration(locations = “classpath:e_junit/applicationContext.xml”)
applicationContext.xml配置文件 同样 使用注解需要在配置文件中开启注解扫描
< context:component-scan base-package=“e_junit”>< /context:component-scan>
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.springframework.org/schema/beans"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd ">
<!-- 开启注解扫描
base-package: 指定注解扫描的包
包下面所有的后代包都会被扫描
-->
<context:component-scan base-package="e_junit"></context:component-scan>
</beans>
UserDaoTest.java 测试类
package e_junit;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
// 将junit和 spring 容器进行整合
@RunWith(SpringJUnit4ClassRunner.class)
// 指定容器对应的配置文件
@ContextConfiguration(locations = "classpath:e_junit/applicationContext.xml")
public class UserDaoTest {
@Autowired
UserDao ud;
@Autowired
User u;
@Test
public void test01() {
User user = ud.getUser();
System.out.println(user);
System.out.println(u);
}
@Test
public void test02() {
User user = ud.getUser();
System.out.println(user);
}
public void test03() {
User user = ud.getUser();
System.out.println(user);
}
}
User.java 实体类
package e_junit;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import javax.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Repository;
import org.springframework.stereotype.Service;
@Component("user") // 默认添加名字 user - 类名首字母小写
public class User {
private String name;
private Integer age;
@Resource(name = "car")
private Car car;
public User(Integer age, String name) {
super();
System.out.println("age --- name");
this.name = name;
this.age = age;
}
public User(String name, Integer age) {
super();
System.out.println("name --- age");
this.name = name;
this.age = age;
}
public Car getCar() {
return car;
}
public void setCar(Car car) {
this.car = car;
}
public String getName() {
return name;
}
// @Value("zhangsan") // 符合属性封装的特点
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public User() {
super();
System.out.println("User init");
}
@Override
public String toString() {
return "User [name=" + name + ", age=" + age + ", car=" + car + "]";
}
}
Car.java 实体类
package e_junit;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
@Component
public class Car {
@Value("玛莎拉蒂")
private String name;
@Value("绿色")
private String color;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
@Override
public String toString() {
return "Car [name=" + name + ", color=" + color + "]";
}
public Car() {
super();
// TODO Auto-generated constructor stub
}
}
UserDaoImpl.java实现类
package e_junit;
import org.springframework.stereotype.Repository;
@Repository
public class UserDaoImpl implements UserDao {
@Override
public User getUser() {
// 模拟方法
System.out.println("UserDao getUser");
return new User("rose", 23);
}
}
1.IOC
创建对象三种方式:(1)构造器创建(2)静态工厂方式创建(3)工厂方法创建
DI:4种方式 set注入 构造器注入 p命名空间注入(了解) spel注入(了解)
2.注解取代配置文件
3.Spring + JUnit整合
4.AOP
代理
动态代理
Cglib代理
AOP:面向切面编程 以下是可以观光的内容了 涉及源码思路
UserServiceImpl.java实现类
package f_aop;
public class UserServiceImpl implements UserService {
@Override
public int login() {
System.out.println("登录了");
System.out.println("返回了");
return 0;
}
@Override
public void register() {
System.out.println("注册了");
System.out.println("注册成功了");
}
}
模拟AOP代理模式中 动态代理:代理对象和被代理对象拥有相同的父接口
ProxyTest1.java
package f_aop;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
// 动态代理 => 观光代码
// 代理对象和被代理对象拥有相同父接口
public class ProxyTest1 {
public static void main(String[] args) {
UserService us = new UserServiceImpl();
int login = us.login();
System.out.println(login);
System.out.println("--------");
// 运行时,生成的是代理对象
/*
* loader: 类加载器
* interfaces: 被代理对象所有的父接口
* InvocationHandler: 代理后的增强方法
*/
UserService proxy = (UserService) Proxy.newProxyInstance(
UserServiceImpl.class.getClassLoader(),
UserServiceImpl.class.getInterfaces(),
new InvocationHandler() {
@Override
/*
* proxy:增强后的对象-代理对象
* method:要增强的方法
* args:执行原方法的参数
*/
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
// 前增强代码
System.out.println("开启事务");
// 调用原方法
Object re = method.invoke(us, args);
// 后增强代码
System.out.println("提交事务");
return re;
}
});
int login2 = proxy.login();
System.out.println(login2);
System.out.println(proxy instanceof UserServiceImpl);
}
}
模拟AOP代理模式中 Cglib代理:代理对象继承被代理对象
package f_aop;
import org.springframework.cglib.proxy.Enhancer;
import org.springframework.cglib.proxy.MethodInterceptor;
// cglib代理 => 观光代码
// 代理对象继承被代理对象
public class ProxyTest2 {
public static void main(String[] args) {
Enhancer eh = new Enhancer();
// 1.设置父类
eh.setSuperclass(UserServiceImpl.class);
// 2.增强代码
eh.setCallback((MethodInterceptor) (arg0, arg1, arg2, arg3) -> {
// 前增强
System.out.println("开启事务");
// 原来方法调用
Object re = arg3.invokeSuper(arg0, arg2);
// 后增强
System.out.println("提交事务");
return re;
});
// 3.创建对象
UserServiceImpl us = (UserServiceImpl) eh.create();
us.register();
System.out.println(us instanceof UserServiceImpl);
}
}
5.名词:切入点、通知、织入、切面