@Configuration
@EnableAspectJAutoProxy
public class Config {
@Bean
public CarProduction carProduction(@Value("bmw") String string){
return new CarProduction(string);
}
@Bean
public Spoiler spoiler(){
return new AddSpoiler();
}
}
@Aspect
public class AddSpoiler implements Spoiler {
@Override
@After("execution(* implementation.CarProduction.endProcessOfBuilding(..))")
public void createSpoiler() {
System.out.println("Creating spoiler for ...");
}
}
@Component
public class CarProduction {
private Bodywork bodywork;
private Chassis chassis;
private Engine engine;
private Wheels wheels;
public CarProduction( String typeOfFactory){
CarFactory carFactory = new FactoryChooser().factory(typeOfFactory);
this.bodywork = carFactory.createBodywork();
this.chassis = carFactory.createChassis();
this.engine = carFactory.createEngine();
this.wheels = carFactory.createWheels();
}
//just for test aspect
public void startProcessOfBuilding(){
System.out.println("Production of car have started");
buildCar();
}
private void buildCar(){
chassis.createChassis();
wheels.mountWheels();
bodywork.mountBodywork();
engine.createEngine();
}
//just for test aspect
public void endProcessOfBuilding(){
System.out.println("Process have finished sucesfully.");
}
}
public class Test {
public static void main(String[] args){
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(Config.class);
CarProduction carProduction1 = context.getBean(CarProduction.class);
carProduction1.startProcessOfBuilding(); // <-- pomiędzy tą metodą, a następną powienien zostać wypisany text klasy z adnotacją @Aspect
carProduction1.endProcessOfBuilding();
}
}
Moim problemem jest fakt, że aspekt się nie uruchamia nie widzę problemu - może zważywszy na porę. Jednakże żaden błąd nie wyskakuje. Kolejność adnotacji w springu jest istotna?Chciałbym zaznaczyć, że 'raczkuję' ze springiem ;)
Co robię źle ?