我是Spring和AOP的新手。我正在尝试这个简单的事情,我创建了一个自定义注释,在任何方法之前放置应该执行一些代码。 这是我创建的注释
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "pacra1";
$conn = new mysqli($servername, $username, $password, $dbname);
"SET @v1 := (SELECT `opinion_id` FROM `pacra_client_opinion_relations` WHERE `client_id` = 97 LIMIT 1)";
$sql= " SELECT @v1, r.client_id,c.id,t.id,a.id,o.id,c.name as opinion, r.notification_date, t.title as ttitle,a.title as atitle,o.title as otitle, l.title as ltitle, s.title as stitle, pr.opinion_id, pc.id, pr.client_id as pr_client, pc.address, pc.liaison_one, city.id, pc.head_office_id, city.city, pc.title as cname
FROM og_ratings r
LEFT JOIN og_companies c
ON r.client_id = c.id
LEFT JOIN og_rating_types t
ON r.rating_type_id = t.id
LEFT JOIN og_actions a
ON r.pacra_action = a.id
LEFT JOIN og_outlooks o
ON r.pacra_outlook = o.id
LEFT JOIN og_lterms l
ON r.pacra_lterm = l.id
LEFT JOIN og_sterms s
ON r.pacra_sterm = s.id
LEFT JOIN companies_contact co
ON c.id = co.companies_id
LEFT JOIN pacra_client_opinion_relations pr
ON pr.opinion_id = c.id
LEFT JOIN pacra_clients pc
ON pc.id = pr.client_id
LEFT JOIN city
ON city.id = pc.head_office_id
WHERE r.client_id= @v1
ORDER BY r.id DESC
LIMIT 1";
$result = $conn->query($sql);
while ($row = $result->fetch_assoc()){
$liaison_one = $row['liaison_one'];
echo $liaison_one;
}
?>
接下来,我创建了包含逻辑
的Spring Aspect类 // Declares a custom annotation that validates json
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface JsonSchemaAnnotation {
}
这是我的简单测试类
@Aspect
public class UpdateUIMetadataInterceptor {
@Pointcut("execution(public * com.fico.cardinal.cm.*.*(..))")
public void anyPublicMethod() {
System.out.println("Running");
}
@Before("anyPublicMethod() && @annotation(jsonSchemaAnnotation)")
public void validateJson(ProceedingJoinPoint pjp) throws Throwable {
System.out.println("Running");
}
}
问题是我的春天永远不会被触发。我在public class ValidationTest {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("spring/configuration.xml");
String jsondata = "{\"id\": \"EXPENSE_REPORT\",\"properties\": {\"transactionType\": \"EXPENSE_REPORT\"},\"sections\": []} ]}";
ValidationTest test = new ValidationTest();
test.jsonValidationTest("dummy", jsondata);
((AbstractApplicationContext) context).close();
}
@JsonSchemaAnnotation
public void jsonValidationTest(String dummy, String jsondata) {
System.out.println("Success");
}
configuration.xml
有人能说出我错过的东西吗?
答案 0 :(得分:2)
您的代码有几个问题:
${token}
对象创建为由Spring管理的bean而不是使用新的ValidationTest
应为<aop:include name="UpdateUIMetadataInterceptor" />
;为了简单起见,你实际上可以坚持使用<aop:include name="updateUI"/>
<aop:aspectj-autoproxy/>
,因此将其删除;如果您需要访问参数ProceedingJoinPoint
{li> JoinPoint
参数应该出现在您方面的JsonSchemaAnnotation jsonSchemaAnnotation
方法中,正如frant.hartm 所指出的那样
醇>
答案 1 :(得分:1)
我认为您需要完全限定名称或方法中的参数:
FQN:
@Before("anyPublicMethod() && @annotation(your.package.JsonSchemaAnnotation)")
public void validateJson(ProceedingJoinPoint pjp) throws Throwable {
System.out.println("Running");
}
参数:
@Before("anyPublicMethod() && @annotation(jsonSchemaAnnotation)")
public void validateJson(ProceedingJoinPoint pjp, JsonSchemaAnnotation jsonSchemaAnnotation ) throws Throwable {
System.out.println("Running");
}
来源:http://docs.spring.io/spring/docs/current/spring-framework-reference/html/aop.html#aop-pointcuts
(你也需要使用豆子,正如Dmitry Kuskov指出的那样