@Jayfeather
2018-12-02T17:22:17.000000Z
字数 6314
阅读 844
Spring
| 更新时间 | 更新内容 |
|---|---|
| 20181126 | 创建该笔记,实现IOC与DI功能 |
| 20181203 | 补充IOC,DI,AOP思维导图,更新AOP代码,部分代码添加注释 |
| TODO | 1.完善注释,思维导图 2. 继续学习数据库的运用 |
//你没看错,这个和上次就是同一篇文章~
AOP代理混乱(原因待查)
报错:
Unsatisfied dependency expressed through field 'car'; nested exception is org.springframework.beans.factory.BeanNotOfRequiredTypeException: Bean named 'car' is expected to be of type 'object.Car' but was actually of type 'object.$Proxy15'
处理:
args()导入参数问题
问题:错误的在声明通用切点处导入参数
本程序实现了Spring框架的AOP和DI功能
------------------------------(该思维导图未完)-----------------------------
具体技术细节参见《Spring实战》在此不予赘述
(导入了AOP相关包)

springConfig.xml、springConfig.java为Sping的设置文件(两个文件作用一致、仅仅是两种不同的实现)
Car.java、People.java 待创建为Bean的组建类。在程序中,我们要将People类注入Car类中,实现控制反转的功能。
CarTest验证Bean是否成功创建、验证是否成功注入。
Reminding:用于AOP实验(目前只有一个环绕通知)
<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans"xmlns:c="http://www.springframework.org/schema/c"xmlns:p="http://www.springframework.org/schema/p"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:aop="http://www.springframework.org/schema/aop"xmlns:context="http://www.springframework.org/schema/context"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"><bean class="object.People" id="people"/><bean class="object.Car" id="car"><constructor-arg name="people" ref="wangZiXuan"/></bean><!--<constructor-arg ref="people" />--><bean class="object.Reminding" id="reminding"/><bean class="object.WangZiXuan" id="wangZiXuan"/><aop:config proxy-target-class="true">//<!--这是一个环绕通知-->,<aop:aspect ref="reminding"><aop:pointcut id="printSpeed" expression="execution(* object.Car.printSpeed(int)) "/><!--通知通用连接点声明--><aop:aroundpointcut="execution(* object.Car.printSpeed(int)) and args(maxSpeed)"method="saftyFirst"/> <!--↑和上面那个东西是等价的--></aop:aspect></aop:config></beans>
package object;import object.*;import org.aspectj.lang.annotation.Before;import org.springframework.beans.factory.BeanFactory;import org.springframework.beans.factory.config.ConfigurableBeanFactory;import org.springframework.context.annotation.*;import org.springframework.core.env.Environment;import org.springframework.core.type.AnnotatedTypeMetadata;import org.springframework.aop.*;@Configuration//@ComponentScans() //自动扫描并生成bean//@ComponentScan(basePackageClasses = {Car.class})@EnableAspectJAutoProxy(proxyTargetClass = true)public class springConfig {@Bean@Primary //首要注入的Beanpublic People peopleBean() {return new People();}@Bean@Scope(ConfigurableBeanFactory.SCOPE_SINGLETON) //限制Bean的作用域(当前为单例)public Car carBean(People people) {return new Car(people);}@Bean@Conditional(IsWangZiXuan.class) //判断是否启用Beanpublic WangZiXuan WangZiXuanBean() {return new WangZiXuan();}@Beanpublic Reminding reminding() {return new Reminding();}}class IsWangZiXuan implements Condition{//Bean判断类,要实现condition接口//还没弄懂,写个return true;先放着public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata){Environment env = context.getEnvironment();//return env.containsProperty("Car");return true;}}
package object;import org.junit.contrib.java.lang.system.SystemOutRule;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Component;@Componentpublic class Car implements Cars {private int speed;private People people;private int maxSpeed;Car(People people){this.people=people;this.speed=people.favoriteSpeed;this.maxSpeed=120;}@Autowiredpublic void setPeople(People people){this.people=people;speed=people.favoriteSpeed;}public void run(){this.speed=people.favoriteSpeed;}public int getSpeed(){return speed;}public void printSpeed(int maxSpeed){System.out.print(speed);}public void printFavoriteSpeed(){System.out.print(people.getFavoriteSpeed());}public int getMaxSpeed() {return maxSpeed;}}interface Cars{public void setPeople(People people);public void run();public int getSpeed();}interface Cars{public void setPeople(People people);public void run();public int getSpeed();}
package object;import org.springframework.stereotype.Component;@Componentpublic class People {int favoriteSpeed=100;public int getFavoriteSpeed() {return favoriteSpeed;}public void setFavoriteSpeed(int favoriteSpeed) {this.favoriteSpeed = favoriteSpeed;}}@Componentclass WangZiXuan extends People{int tall;WangZiXuan(){this.favoriteSpeed=1000;this.tall=170;}public int getTall() {return tall;}public void setTall(int tall) {this.tall = tall;}}
package object;import org.aspectj.lang.*;import org.aspectj.lang.annotation.Aspect;import org.aspectj.lang.annotation.Before;import org.aspectj.lang.annotation.Pointcut;import org.springframework.stereotype.Component;import org.springframework.aop.aspectj.*;@Aspect@Componentpublic class Reminding {// @Pointcut("execution(* object.Car.printSpeed(..))")//Java配置文件的 定义切点注释// public void safty(){} //这个也是的//// @Before("safty()") //这个还是的(但是目前用的是XML 所以这个注释就没用了)public void saftyFirst(ProceedingJoinPoint JP,int maxSpeed){//环绕通知(之前写了前置和后置通知,不过被替换为更复杂的环绕通知)try {System.out.println("Safty First,Max speed is "+maxSpeed);//前置JP.proceed(); //执行的程序在这System.out.println("\nPark Car");//后置}catch (Throwable e){System.out.println("You die");//异常处理}}}
package object;import org.junit.Rule;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;import static org.junit.Assert.*;import org.junit.contrib.java.lang.system.SystemOutRule;@RunWith(SpringJUnit4ClassRunner.class)@ContextConfiguration(locations = "/config/springConfig.xml")public class CarTest {@Autowiredprivate Car car;@Autowiredprivate People people;@Rulepublic final SystemOutRule log=new SystemOutRule().enableLog();@Testpublic void isNOtNull(){assertNotNull(car);}@Testpublic void peopleIsNotNull(){assertNotNull(people);}@Testpublic void getMaxSpeed(){car.printSpeed();assertEquals("100",log.getLog());}}