Tag Archives: aop

How to Solve Error: Failed to execute goal org.codehaus.mojo:……..

How to Solve:Failed to execute goal org.codehaus.mojo:exec-maven-plugin:3.0.0:exec (default-cli) on project spring_aop: Command execution failed.error


error:

Failed to execute goal org.codehaus.mojo:exec-maven-plugin:3.0.0:exec (default-cli) on project 
spring_aop: Command execution failed.

Solution 1: in this case, I will use the main method to execute the program and use the unit test @ test instead

~It used to be like this: running with main

public class MyTest {
    public static void main(String[] args) {
    
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");

        //Dynamic proxies represent interfaces
        UserService userservice = (UserService) context.getBean("userservice");

        userservice.add();
    }
}

~Changed to: use @ test

import com.niuyun.service.UserService;
import com.niuyun.service.UserServiceImpl;
import org.junit.Test;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class MyTest {

    @Test
    public void test(){
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");

        //Dynamic proxies represent interfaces
        UserService userservice = (UserService) context.getBean("userservice");

        userservice.add();
    }
}

~Finish the work and run successfully

! Expansion

@Test is the basis of JUnit testing, and its functions are as follows:
1. Specify the type of exception to be thrown
2. Test the running time of the code.


Solution 2: if you have to use the main function, you can introduce two plug-ins in Maven (compiler can not be introduced), but it may lead to Chinese garbled.

Maven compiler plugin: used to compile java files, specify JDK version, etc.
exec Maven plugin: used to execute class files, in which the path of executing class should be indicated in plug-in configuration.

<build>
     <plugins>
         <plugin>
             <groupId>org.codehaus.mojo</groupId>
             <artifactId>exec-maven-plugin</artifactId>
             <version>1.6.0</version>
             <executions>
                 <execution>
                     <goals>
                         <goal>java</goal>
                     </goals>
                 </execution>
             </executions>
             <configuration>
                 <classpathScope>test</classpathScope>
             </configuration>
         </plugin>
     </plugins>
</build>

~Run successfully


Solve the problem of garbled code

Method 1:
in setting – & gt; maven-> Runner-> Fill in the column VM options
with – dfile. Encoding = GB2312

Method 2: add in pom.xml (I tried this method, but I didn’t solve the garbled code, which may be useful to you)

<properties>
	<!-- Encoding when copying files -->
	<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
	<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
	<!-- Compile-time coding -->
    <maven.compiler.encoding>UTF-8</maven.compiler.encoding>
</properties> 

Aspecj cannot intercept method annotations on an interface

Aspecj cannot intercept method annotations on an interface

Aspecj can’t intercept the method annotation on the interface, it can only act on the method of the implementation class. At this time, it needs to use methodinterceptor to implement.

@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
public @interface AopTest {
}

Interface

public interface TestAOPI {
    @AopTest
    public String test();

}

Implementation class 1

@Service
public class TestAOPService implements TestAOPI{
    @Override
    public String test() {
        return "service";
    }
}

Implementation class 2

@Service
public class TestAOPService2 implements TestAOPI{
	@AopTest
    @Override
    public String test() {
        return "service";
    }
}

Aspecj (partially valid)

If and only if the @ aoptest annotation is added to the method of the implementation class, it will take effect (implementation class 2), but implementation class 1 will not

@Aspect
@Configuration
public class AopTestAspect {
  /**
     * Identifies the method annotated with OperationLog
     */
    @Pointcut("@annotation(com.example.demo1.config.AopTest)")
    public void methodHasAopTestAnnotation() {
    }
    
    @Around("methodHasAopTestAnnotation()")
    public Object doAround(ProceedingJoinPoint joinPoint) throws Throwable {
        System.out.println("aop!!!");
        return joinPoint.proceed();
    }
}

Solution

It needs to be changed to the following way by manual

@Configuration
public class AopTestConfiguration {
	@Bean
    public Advisor methodPointcutAdvisor() {
        AopTestMethodPointcutAdvisor advisor = new AopTestMethodPointcutAdvisor();
        advisor.setAdvice(new AopTestInterceptor());
        return advisor;
    }

    class AopTestInterceptor implements MethodInterceptor {
        @Override
        public Object invoke(MethodInvocation invocation) throws Throwable {
            String name = invocation.getMethod().getName();
            System.out.println("==============" + name + " before ================");
            Object result = invocation.proceed();
            System.out.println("==============" + name + " after ================");
            return result;
        }
    }
    public class AopTestMethodPointcutAdvisor extends StaticMethodMatcherPointcutAdvisor {
        @Override
        public boolean matches(Method method, Class<?> targetClass) {
        	// Implementing a class method with a target annotation on it
            if(method.isAnnotationPresent(AopTest.class)){
                return true;
            }
            // The method has a corresponding interface method and the interface method is annotated
            Class<?>[] interfaces = method.getDeclaringClass().getInterfaces();
            for (int i = 0; i < interfaces.length; i++) {
                Method[] methods = interfaces[i].getMethods();
                for (int j = 0; j < methods.length; j++) {
                    if(methods[j].getName().equals(method.getName())){
                        return methods[j].isAnnotationPresent(AopTest.class);
                    }
                }
            }
            return false;
        }
    }
}

Failed to execute goal org.apache.tomcat.maven:tomcat7-maven-plugin:2.2:run (default-cli) on project

Complete error message

Failed to execute goal org.apache.tomcat.maven:tomcat7-maven-plugin:2.2:run (default-cli) on project jurisdiction_controller: Could not start Tomcat


Start the Maven project wrong, use Tomcat embedded in it
The solution
The dependent use of servlets is introduced in the pom.xml file


Change after

add scope tags, and the values is provided (only role in the compilation and testing, and at the same time not pass)
The scope label specifies the range of values
compile
The default scope, which means that all Dependencies can be used throughout the lifecycle. Furthermore, these dependencies are passed to the projects on which they depend. Applies to all phases and will be released along with the project
provided
Similar to compile, but indicating that dependency is provided by JDKs or containers, such as Servlet APs and some Java EE APIs. This scope can only be used during compilation and testing and has no transitivity.
runtime
Dependency does not apply at compile time, but does apply at run and test times, such as JDBC drivers, for the run and test phases.
test
Indicates that dependency is applied at test time, not at run time. Used only during testing, to compile and run test code. Will not be released with the project.
system
Similar to Provided, but provided in the system as an external JAR, Maven does not look for it in a repository.