Spring BeforeAdvice
Необходимо с помощью аннотаций spring, сделать инкрементор обновления веб приложения на основе этого примера https://spring.io/guides/gs/rest-service/ , важно, что нельзя менять исходный код класса GretingsController, кроме удаления флажка final у переменных. Это мой первый вопрос, я старался сам сделать, но у меня не выходит, в интернете старые варианты реализации, а я новичок, не судите строго. Заранее извиняюсь за неправильную терминологию.
Сам инкрементор, в ворнингах пишет, что method и args не определяются
@Component
public class Incrementer implements MethodBeforeAdvice {
private Greeting g;
@Override
public void before(Method method, Object[] args, Object target) {
g.setId(100);
System.out.print("1"); // не выводится в консоль
}
@Autowired
public Incrementer(Greeting greeting) {
g = greeting;
g.setId(100);
System.out.println(greeting.getId()); //выводится в консоль 100, однако Json все равно выводит 1, а не 100
}
}
Greeting
@Component
public class Greeting {
private long id;
private String content;
public Greeting(long id, String content) {
this.id = id;
this.content = content;
}
public Greeting(Greeting greeting) {
}
public Greeting() {
}
public long getId() {
return id;
}
public String getContent() {
return content;
}
public void setId(int i) {
id = id+i;
}
}
GreetingController
@RestController
public class GreetingController {
private static String template = "Hello, %s!";
private AtomicLong counter = new AtomicLong();
@GetMapping("/greeting")
public Greeting greeting(@RequestParam(value = "name", defaultValue = "World") String name) {
return new Greeting(counter.incrementAndGet(), String.format(template, name));
}
}
XML-файл
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id ="greetingBean" class="com.example.restservice.Greeting">
</bean>
<bean id ="incrementerBean" class = "com.example.restservice.Incrementer"/>
<bean id ="incrementerProxy"
class ="org.springframework.aop.framework.ProxyFactoryBean">
<property name="target" ref ="greetingBean"/>
<property name="interceptorNames">
<list>
<value>incrementerBean</value>
</list>
</property>
</bean>