I've been trying to figure out why my simple aspect is not getting executed. I looked at the answers of similar problems but i still can't get it to work.
My intention is to wrap the execution of a method annotated with a custom annotation with an AOP advice that will track how long the method takes to run. When i run my test, i see the output of the method but the advice is not being run (i'm expecting it to log some output).
Here's the Aspect class:
#Aspect
class LatencyProfiler {
private LatencyTrackerFactory factory = LatencyTrackerFactory.NOOP;
#Pointcut(value="execution(#ProfileLatency * *(..)) && #annotation(annotation)", argNames="annotation")
public void profiled(ProfileLatency annotation) {}
#Around(value="profiled(annotation)", argNames="pjp,annotation")
public Object profile(ProceedingJoinPoint pjp, ProfileLatency annotation) throws Throwable {
ILatencyTracker tracker;
try {
tracker = factory.create(annotation.trackerName(), annotation.trackerNameSuffix());
} catch (ConfigException e) {
throw new RuntimeException(e);
}
tracker.begin();
Object ret = pjp.proceed();
tracker.end(null);
return ret;
}
#Optional
public void setFactory(LatencyTrackerFactory factory) {
this.factory = factory;
}
}
Followed by the Annotation:
#Retention(RetentionPolicy.RUNTIME)
#Target(ElementType.METHOD)
public #interface ProfileLatency {
String trackerName();
String trackerNameSuffix() default"[unassigned]";
}
Followed by a test class:
public class Test {
private static final Log LOG = LogFactory.getLog(Test.class);
#PostConstruct
public void init() {
Executors.newSingleThreadScheduledExecutor().schedule(new Runnable() {
#Override
public void run() {
for(int i = 0; i < 60; i++) {
foo();
LOG.info("HERE");
}
}
}, 2000, TimeUnit.MILLISECONDS);
}
#ProfileLatency(trackerName = "latency", trackerNameSuffix = "s")
public void foo() {
try {
Thread.sleep(1000);
} catch (InterruptedException ignored) {
}
}
}
Spring configuration:
<context:annotation-config/>
<aop:aspectj-autoproxy>
<aop:include name="latencyProfileAspect"/>
</aop:aspectj-autoproxy>
<bean
id = "latencyLogger"
class = "util.logging.LatencyLogger"
/>
<bean
id = "trackerFactory"
class = "util.latency.LatencyTrackerFactoryImpl">
<constructor-arg value = "config/latency-config.xml"/>
<constructor-arg ref = "latencyLogger"/>
</bean>
<bean
id = "latencyProfileAspect"
class = "util.latency.aop.LatencyProfiler"
p:factory-ref = "trackerFactory"
/>
<bean id = "test" class="util.Test"/>
and finally the test's output:
21:20:37,930 INFO main/SpringMain - Ready.
21:20:40,928 INFO pool-4-thread-1/Test - HERE
21:20:41,927 INFO pool-4-thread-1/Test - HERE
21:20:42,926 INFO pool-4-thread-1/Test - HERE
21:20:43,925 INFO pool-4-thread-1/Test - HERE
21:20:44,924 INFO pool-4-thread-1/Test - HERE
...
Any advice is greatly appreciated.
So i fiddled with this around a bit and got it to work. I modified the aspect as follows:
#Aspect
public class LatencyProfiler {
private static final Log LOG = LogFactory.getLog(LatencyProfiler.class);
#Around("#annotation(annotation)")
public Object profile(ProceedingJoinPoint pjp, ProfileLatency annotation) throws Throwable {
ILatencyTracker tracker = ILatencyTracker.NOOP;
try {
tracker = StaticLatencyTrackerFactory.getTracker(annotation.trackerName(), annotation.trackerNameSuffix());
} catch (Exception e) {
LOG.error(e);
}
LatencyContext ctx = tracker.beginContext();
Object ret = pjp.proceed();
ctx.end();
return ret;
}
/*
* special purpose factory method
*/
public static LatencyProfiler aspectOf() {
return MyAspectHolder.instance;
}
/**
* private class holding the singleton
*/
private static class MyAspectHolder {
static final LatencyProfiler instance = new LatencyProfiler();
}
}
i also changed the spring configuration to be:
<context:annotation-config/>
<aop:aspectj-autoproxy proxy-target-class="true"/>
<bean
id = "latencyProfileAspect"
class = "util.latency.aop.LatencyProfiler"
factory-method = "aspectOf"
/>
<bean id = "test" class="util.Test"/>
Related
The issue is the same as this question
I am working with the spring aop at annotation aspect, one of my service bean cannot install annotation advice but others can.
Then I debug and found out the reason, the code in BeanFactoryAdvisorRetrievalHelper indicates that if an advice bean is in creation, the serviceBean will failed to install the advisor.
if (this.beanFactory.isCurrentlyInCreation(name))
public List<Advisor> findAdvisorBeans() {
// Determine list of advisor bean names, if not cached already.
String[] advisorNames = this.cachedAdvisorBeanNames;
if (advisorNames == null) {
// Do not initialize FactoryBeans here: We need to leave all regular beans
// uninitialized to let the auto-proxy creator apply to them!
advisorNames = BeanFactoryUtils.beanNamesForTypeIncludingAncestors(
this.beanFactory, Advisor.class, true, false);
this.cachedAdvisorBeanNames = advisorNames;
}
if (advisorNames.length == 0) {
return new ArrayList<>();
}
List<Advisor> advisors = new ArrayList<>();
for (String name : advisorNames) {
if (isEligibleBean(name)) {
if (this.beanFactory.isCurrentlyInCreation(name)) {
if (logger.isTraceEnabled()) {
logger.trace("Skipping currently created advisor '" + name + "'");
}
}
else {
try {
advisors.add(this.beanFactory.getBean(name, Advisor.class));
}
catch (BeanCreationException ex) {
Throwable rootCause = ex.getMostSpecificCause();
if (rootCause instanceof BeanCurrentlyInCreationException) {
BeanCreationException bce = (BeanCreationException) rootCause;
String bceBeanName = bce.getBeanName();
if (bceBeanName != null && this.beanFactory.isCurrentlyInCreation(bceBeanName)) {
if (logger.isTraceEnabled()) {
logger.trace("Skipping advisor '" + name +
"' with dependency on currently created bean: " + ex.getMessage());
}
// Ignore: indicates a reference back to the bean we're trying to advise.
// We want to find advisors other than the currently created bean itself.
continue;
}
}
throw ex;
}
}
}
}
return advisors;
}
The interceptor is
public class DemoInterceptor implements MethodInterceptor {
#Override
public Object invoke(MethodInvocation invocation) throws Throwable {
//do something
return new Object();
}
}
The advisor config is
config way 1
public class DemoAdvisor implements PointcutAdvisor {
private MethodInterceptor methodInterceptor;
private Pointcut pointcut;
public DemoAdvisor(MethodInterceptor methodInterceptor) {
this.methodInterceptor = methodInterceptor;
this.pointcut = new AnnotationMatchingPointcut(null, DemoAnno.class);
}
#Override
public Advice getAdvice() {
return this.methodInterceptor;
}
#Override
public boolean isPerInstance() {
return true;
}
#Override
public Pointcut getPointcut() {
return this.pointcut;
}
}
#Configuration
public class AnnoAspectConfig {
#Bean
public DemoAdvisor DemoAdvisor() {
DemoAdvisor advisor = new DemoAdvisor(demoInterceptor());
return advisor;
}
#Bean
public DemoInterceptor demoInterceptor() {
DemoInterceptor demoInterceptor = new DemoInterceptor();
return demoInterceptor;
}
}
Then I think it maybe the problem in the advice config, so I change the advice config to the below
config way 2
#Configuration
public class ValidationAdvisor {
#Bean
public DefaultPointcutAdvisor demoAdvisor() {
DemoInterceptor interceptor = new DemoInterceptor();
AnnotationMatchingPointcut annotationMatchingPointcut = AnnotationMatchingPointcut.forMethodAnnotation(DemoAnno.class);
DefaultPointcutAdvisor advisor = new DefaultPointcutAdvisor();
advisor.setPointcut(annotationMatchingPointcut);
advisor.setAdvice(interceptor);
return advisor;
}
}
Then everything goes find, so the question is, why the config way 1 cause the failed of advisor installation while the config way 2 did not?
First I'm not sure if it's a good idea to do all this.
Goal is to create some interfaces with annotations to hide legacy position based string access out of a configuration database, without implementing each interface.
Declarative configured Interface:
public interface LegacyConfigItem extends ConfigDbAccess{
#Subfield(length=3)
String BWHG();
#Subfield(start = 3, length=1)
int BNKST();
#Subfield(start = 4, length=1)
int BEINH();
:
}
Base interface for runtime identification
public interface ConfigDbAccess{
}
Dummy implementation without functionality, may change.
public class EmptyImpl {
}
Beanfactory and MethodInvocation interceptor, to handle the unimplemented methods.
#Component
public class InterfaceBeanFactory extends DefaultListableBeanFactory {
protected static final int TEXT_MAX = 400;
#Autowired
private EntityRepo entityRepo;
public <T> T getInstance(Class<T> legacyInterface, String key) {
ProxyFactory factory = new ProxyFactory(new EmptyImpl());
factory.setInterfaces(legacyInterface);
factory.setExposeProxy(true);
factory.addAdvice(new MethodInterceptor() {
#Override
public Object invoke(MethodInvocation invocation) throws Throwable {
KEY keyAnnotation = invocation.getThis().getClass().getAnnotation(Key.class);
String key= keyAnnotation.key().toUpperCase();
String ptart = invocation.getMethod().getDeclaringClass().getSimpleName();
Vpt result = entityRepo.getOne(new EntityId(ptart.toUpperCase(), schl.toUpperCase()));
Subfield sub = invocation.getMethod().getAnnotation(Subfield.class);
//TODO: Raise missing Subfield annotation
int start = sub.start();
int length = sub.length();
if (start + length > TEXT_MAX) {
//TODO: Raise invalid Subfield config
}
String value = result.getTextField().substring(start,start+length);
return value;
}
});
return (T) factory.getProxy();
}
#Override
protected Map<String, Object> findAutowireCandidates(String beanName, Class<?> requiredType, DependencyDescriptor descriptor) {
Map<String, Object> map = super.findAutowireCandidates(beanName, requiredType, descriptor);
if (ConfigDbAccess.class.isAssignableFrom(requiredType )) {
:
#SpringBootApplication
public class JpaDemoApplication {
#Autowired
private ApplicationContext context;
public static void main(String[] args) {
SpringApplication app = new SpringApplication(JpaDemoApplication.class);
// app.setApplicationContextClass(InterfaceInjectionContext .class);
app.run(args);
}
public class InterfaceInjectionContext extends AnnotationConfigApplicationContext {
public VptInjectionContext () {
super (new InterfaceBeanFactory ());
}
}
So far I got all this stuff working, except when I try to set the applications Context class to my DefaultListableBeanFactory, I'm killing the Spring boot starter web. The application starts, injects the the Autowired fields with my intercepted pseudo implementaition --- and ends.
I think I'm doing something wrong with registering the DefaultListableBeanFactory, but I've no idea how to do it right.
To get this answered:
M. Deinum pointed me to a much simpler solution:
Instead of creating a BeanFactory I installed a BeanPostProcessor with this functioniality.
#RestController
public class DemoRestController {
#Autowired
VptService vptService;
#ConfigItem(key="KS001")
private PrgmParm prgmKs001;
#ConfigItem(key="KS002")
private PrgmParm prgmKs002;
public DemoRestController() {
super();
}
Where the ConfigItem annotation defines the injection point.
Next I created a CustomBeanPostProcessor which scans all incoming beans for
fields having a ConfigItem annotation
#Component
public class CustomBeanPostProcessor implements BeanPostProcessor {
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
for (Field field : bean.getClass().getDeclaredFields()) {
SHL cfgDef = field.getAnnotation(ConfigItem.class);
if (cfgDef != null) {
Object instance = getlInstance(field.getType(), cfgDef.key());
boolean accessible = field.isAccessible();
field.setAccessible(true);
try {
field.set(bean, instance);
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
field.setAccessible(accessible);
}
}
return bean;
}
The getInstnce(field.getType(),cfgDef.key()) creates a proxy with the MethodInterceptor, which does the work.
There are a lot of things to finalize, but all in all it looks good to me.
It's for the first time that Inside an AspectJ I may need to access a local private autowired field of a Repository in order to do some stuff on >exactly< that instance.
I created a pointcut that focuses on each method of every #Repository annotated class. When the pointcut fires, I get the current class instance from which I want to get the bean field.
This is the way:
#Repository
public class MyDao {
#Autowired
private MyBean bean;
public List<Label> getSomething() {
// does something...
}
}
#Aspect
#Component
public class MyAspect {
#Pointcut("within(#org.springframework.stereotype.Repository *)")
public void repositories() {
}
#Before("repositories()")
public void setDatabase(JoinPoint joinPoint) {
try {
Field field = ReflectionUtils.findField(joinPoint.getThis().getClass(), "bean"); // OK since here - joinPoint.getThis().getClass() -> MyDao
ReflectionUtils.makeAccessible(field); // Still OK
Object fieldValue = ReflectionUtils.getField(field, joinPoint.getThis());
System.out.println(fieldValue == null); // true
// should do some stuff with the "fieldValue"
} catch (Exception e) {
e.printStackTrace();
}
}
}
fieldValue is always null even if I create something like private | public | package String something = "blablabla"; instead.
I have ensured that "bean" is actually instantiated when the application starts (verified with the debugger).
I followed How to read the value of a private field from a different class in Java?
What I am missing? | Is it possible? | Are there any different ways?
#springbootlearner suggested this approach access class variable in aspect class
All I had to do is to replace the joinPoint.getThis() with joinPoint.getTarget()
And the final solution is:
#Aspect
#Component
public class MyAspect {
/**
*
*/
#Pointcut("within(#org.springframework.stereotype.Repository *)")
public void repositories() {
}
/**
* #param joinPoint
*/
#Before("repositories()")
public void setDatabase(JoinPoint joinPoint) {
Object target = joinPoint.getTarget();
// find the "MyBean" field
Field myBeanField = Arrays.stream(target.getClass().getDeclaredFields())
.filter(predicate -> predicate.getType().equals(MyBean.class)).findFirst().orElseGet(null);
if (myBeanField != null) {
myBeanField.setAccessible(true);
try {
MyBean bean = (MyBean) myBeanField.get(target);// do stuff
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}
}
}
I have a android application, but it is not relevant.
I have a class called "Front controller" which will receive some message
through it's constructor. The message, for brievity, could be an integer.
I want somewhere else to create a new controller which will execute
a method based on the integer defined above
public class OtherController {
#MessageId("100")
public void doSomething(){
//execute this code
}
#MessageId("101")
public void doSomethingElse(){
//code
}
}
The front controller could be something like this:
public class FrontController {
private int id;
public FrontController(int id){
this.id=id;
executeProperControllerMethodBasedOnId();
}
public void executeProperControllerMethodBasedOnId(){
//code here
}
public int getId(){
return id;
}
}
So, if the Front Controller will receive the integer 100, it
will execute the method annotated with #MessageId(100). The
front controller don't know exactly the class where this method
is.
The problem which I found is that I need to register somehow
each controller class. I Spring I had #Component or #Controller
for autoloading. After each controllers are register, I need to
call the properly annotated method.
How to achieve this task? In Spring MVC, I had this system
implemented, used to match the HTTP routes. How could I implement
this in a plain java project?
Any suggestions?
Thanks to Google Reflections (hope you can integrate this in your android project.)
<dependency>
<groupId>org.reflections</groupId>
<artifactId>reflections-maven</artifactId>
<version>0.9.8</version>
</dependency>
For optimisation I've added the requirement to also annotate the class with MessageType annotation and the classes should be in the same package (org.conffusion in my example):
#Retention(RetentionPolicy.RUNTIME)
#Target(ElementType.TYPE)
public #interface MessageType {
}
The OtherController looks like:
#MessageType
public class OtherController {
#MessageId(id=101)
public void method1()
{
System.out.println("executing method1");
}
#MessageId(id=102)
public void method2()
{
System.out.println("executing method2");
}
}
The implementation will look like:
public void executeProperControllerMethodBasedOnId() {
Set<Class<?>> classes = new org.reflections.Reflections("org.conffusion")
.getTypesAnnotatedWith(MessageType.class);
System.out.println("found classes " + classes.size());
for (Class<?> c : classes) {
for (Method m : c.getMethods()) {
try {
if (m.isAnnotationPresent(MessageId.class)) {
MessageId mid = m.getAnnotation(MessageId.class);
Object o = c.newInstance();
if (mid.id() == id)
m.invoke(o);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
Maybe you can optimise and build a static hashmap containing already scanned message ids.
You need to implement some of the work by yourself using reflection, I would recommend to prepare message handlers on initial phase in regards to performance. Also you possibly want to think about Singleton/Per Request controllers. Some of the ways to implement the solution:
interface MessageProcessor {
void execute() throws Exception;
}
/* Holds single instance and method to invoke */
class SingletonProcessor implements MessageProcessor {
private final Object instance;
private final Method method;
SingletonProcessor(Object instance, Method method) {
this.instance = instance;
this.method = method;
}
public void execute() throws Exception {
method.invoke(instance);
}
}
/* Create instance and invoke the method on execute */
class PerRequestProcessor implements MessageProcessor {
private final Class clazz;
private final Method method;
PerRequestProcessor(Class clazz, Method method) {
this.clazz = clazz;
this.method = method;
}
public void execute() throws Exception {
Object instance = clazz.newInstance();
method.invoke(instance);
}
}
/* Dummy controllers */
class PerRequestController {
#MessageId(1)
public void handleMessage1(){System.out.println(this + " - Message1");}
}
class SingletonController {
#MessageId(2)
public void handleMessage2(){System.out.println(this + " - Message2");}
}
class FrontController {
private static final Map<Integer, MessageProcessor> processors = new HashMap<Integer, MessageProcessor>();
static {
try {
// register your controllers
// also you can scan for annotated controllers as suggested by Conffusion
registerPerRequestController(PerRequestController.class);
registerSingletonController(SingletonController.class);
} catch (Exception e) {
throw new ExceptionInInitializerError();
}
}
private static void registerPerRequestController(Class aClass) {
for (Method m : aClass.getMethods()) {
if (m.isAnnotationPresent(MessageId.class)) {
MessageId mid = m.getAnnotation(MessageId.class);
processors.put(mid.value(), new PerRequestProcessor(aClass, m));
}
}
}
private static void registerSingletonController(Class aClass) throws Exception {
for (Method m : aClass.getMethods()) {
if (m.isAnnotationPresent(MessageId.class)) {
MessageId mid = m.getAnnotation(MessageId.class);
Object instance = aClass.newInstance();
processors.put(mid.value(), new SingletonProcessor(instance, m));
}
}
}
/* To process the message you just need to look up processor and execute */
public void processMessage(int id) throws Exception {
if (processors.containsKey(id)) {
processors.get(id).execute();
} else {
System.err.print("Processor not found for message " + id);
}
}
}
Ok so I have build a converter and added it to the dispatcher xml. But it won't work. I don't understand how the controller should know when it should use the converter. In my jsp page I check multiply checkboxes. Each checkbox holds an id of a developer. Spring should make a set of developers from these id's. I have the feeling I'm missing something in the controller. I used to do it with editors and then you would override the initbinder method. I don't know how to do it with converters.
Thank you in advance,
David
so first I made a class implementing the interface:
public class DeveloperConverter implements Converter<String, Developer> {
private GameOrganizer gameOrganizer;
public void setGameOrganizer(GameOrganizer gameOrganizer) {
this.gameOrganizer = gameOrganizer;
}
public Developer convert(String s) {
long id2 = Long.parseLong(s);
Developer type = gameOrganizer.getDeveloper(id2);
return type;
}
}
then I added the bean to the dispatcher xml:
<bean id="conversionService" class="org.springframework.context.support.ConversionServiceFactoryBean">
<property name="converters">
<list>
<bean class="converters.GameConverter" />
<bean class="converters.DeveloperConverter" />
</list>
</property>
</bean>
And the controller:
#Controller
#RequestMapping("/AddGame")
public class GameFormController {
#Autowired
private GameOrganizer gameOrganizer;
private DeveloperConverter developerEditor;
private GameValidator gameValidator;
private ConversionService service;
public GameFormController() {
setGameValidator(new GameValidator());
}
public void setGameOrganizer(GameOrganizer gameOrganizer) {
this.gameOrganizer = gameOrganizer;
}
public void setDeveloperEditor(DeveloperConverter developerEditor) {
this.developerEditor = developerEditor;
developerEditor.setGameOrganizer(gameOrganizer);
}
public void setGameValidator(GameValidator gameValidator) {
this.gameValidator = gameValidator;
}
#RequestMapping(method = RequestMethod.GET)
private String showForm(ModelMap model) {
return "AddGame";
}
#ModelAttribute("editGame")
private Game GameformBackingObject(HttpServletRequest request) throws Exception {
Game game = null;
long id = ServletRequestUtils.getLongParameter(request, "id");
if (id <= 0) {
game = new Game();
} else {
game = new Game();
game.setId(gameOrganizer.getGame(id).getId());
game.setDevelopers(gameOrganizer.getGame(id).getDevelopers());
game.setGameNaam(gameOrganizer.getGame(id).getGameNaam());
game.setImages(gameOrganizer.getGame(id).getImages());
game.setPrijs(gameOrganizer.getGame(id).getPrijs());
}
return game;
}
#RequestMapping(method = RequestMethod.POST)
protected String doSubmitAction(#ModelAttribute("editGame") Game game, BindingResult result) throws Exception {
gameValidator.validate(game, result);
if (result.hasErrors()) {
return "AddGame";
} else {
if (game.getId() <= 0) {
gameOrganizer.addGame(game);
} else {
gameOrganizer.update(game);
}
return "forward:/Gamedatabase.htm";
}
}
#ModelAttribute("allDevelopers")
private Set<Developer> getDevelopers() throws Exception {
Set<Developer> developers = gameOrganizer.getAllDevelopers();
return developers;
}
#ModelAttribute("currentId")
private long getCurrentId(HttpServletRequest request) throws ServletRequestBindingException {
long id = ServletRequestUtils.getLongParameter(request, "id");
return id;
}
}
I suppose you have not configured the conversion service for Spring MVC in your XML configuration:
<mvc:annotation-driven conversion-service="conversionService" />