Spring AOP using AspectJ LTW not working - aspectj

I am using spring aop and have defined some aspects. Spring LTW is enabled on my tomcat.
In my application context:
<context:load-time-weaver/>
<aop:aspectj-autoproxy proxy-target-class="false"/>
Aspects are working fine too! but the target class is proxied! causing ClassCastException: can not convert $Proxy...
Note that I don't my target classes to be proxied!

If you are using AspectJ LTW you only need the tag
<context:load-time-weaver/>
in your Spring context file. So you can remove,
<aop:aspectj-autoproxy proxy-target-class="false"/>
If the target class is proxied is because LTW with AspectJ is not configured in a good way, for this reason is not AspectJ who is handling your advices, and is Spring who is doing that. For this reason you see proxy based target class.
Check this links,
http://static.springsource.org/spring/docs/3.2.2.RELEASE/spring-framework-reference/html/aop.html#aop-aj-ltw
http://static.springsource.org/spring/docs/3.2.2.RELEASE/spring-framework-reference/html/aop.html#aop-aj-ltw-environments

Related

Does Spring Data JPA internally use Hibernate & why my app is working if I am not giving dialect property?

I just started learning Spring Data JPA, I connected to mysql in localhost and able to save a record but I am unable to understand why it is working if I am not giving dialect property in properties file and is hibernate a default implementation of spring data instead of ibatis or Eclispe link, because in my pom.xml I just added the dependency of spring-data-jpa and never mentioned what kind of JPA implementation I want to use.
application.properties
spring.jpa.hibernate.ddl-auto=create
spring.jpa.properties.hibernate.
spring.datasource.url=jdbc:mysql://localhost:3306/initsoftware
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.username=ppppppp
spring.datasource.password=xxxxxxx
logging.level.root=DEBUG
spring.jpa.show-sql=true
Since you have an application.properties I assume you are using Spring Boot and not just Spring Data JPA.
In order to use JPA with Spring Boot you would typically add spring-boot-starter-data-jpa to your dependencies. This indeed comes with Hibernate out of the box as you can see when you inspect the dependencies.
Spring Data JPA itself doesn't come with a JPA implementation. You have to add that.
iBatis is not a JPA implementation.
If the assumption above doesn't match your scenario you can use the maven dependency plugin to inspect your (transient) dependencies. The following is a good starting point.
mvn dependency:tree -Dverbose
If you use a different build tool, it probably has a similar feature.

Packaging JPA entities in a jar inside a Spring Boot application

I am refactoring a JEE REST (using JAX-RS 2.0) application as a Spring Boot application. My old app is packaged in a .war and has a jar file with entities and the persistence.xml configuration file for JPA. This jar is copied into WEB-INF/lib directory. I know Spring JPA works a different way and I don't use persistence.xml now but I wonder if I can package my JPA entity classes in a jar and include them in my Spring Boot apps just like I am doing now. This way I can easily reuse that jar in different Spring Boot Applications.
I'm pretty certain you can do this since I have done the same on one of my projects very recently. The only thing you need to do is make sure that you add an #EntityScan annotation on your main Spring Boot config class with the base package of your entities in the JAR.
#EntityScan("my.external.jar.entity.package")
Spring Boot doesn't really care whether the JPA entities are packages as a separate jar or included into the application. Its a runtime framework and in runtime classes can be loaded from the jar (it should reside in BOOT-INF/lib or 'directly' from the *.class files in the spring boot artifact.
Now there is a rule in spring boot, that says that it will scan for beans (including entities) only in the package where your "main" class resides or under it. This is done in order to avoid long process of analysis of, say, third-party classes that you might use. These third-party classes are usually not spring aware at all, at certainly do not contain any spring beans.
Example:
Say, you place your "main" class (the one annotated with #SpringBootApplication) in the package: com.mycompany.myapp
In this case, the following packages will be scanned (just a couple of examples):
com.mycompany.myapp
com.mycompany.myapp.web
com.mycompany.myapp.services.bl
com.mycompany.myapp.whatever.doesnt.matter
...
The following packages won't be scanned however (again, examples, not the full list):
com.mycompany
com.anothercompany
org.hibernate
If you want to to "alter" this default rule and place the entities in the package that doesn't adhere this convention, for example com.mycompany.jpa.entities then you should indeed use #EntityScan annotation as our colleagues have already suggested.
You can read about this topic here. You might also need to get familiar with #EnableJpaRepositories if you're using spring data but, while related, its a different topic.
In my case I had this problem, and after importing the library in the application's pom.xml, in the SpringBoot Project Main class, insert an #EntityScan annotation with the first package and *. Like this: #EntityScan ("br.*")

Facing Spring Roo issue while converting JPA project to non-JPA project

I have a Spring - JPA project generated using Spring Roo. Now for some requirement we are replacing our JPA layer with some other framework (MyBatis) which is not JPA-compliant. I have done the changes and they are working fine also. I have removed all the JPA dependencies from my pom.xml file.
I am facing issues with my JUnit test project which is used to test DAO layer. Spring Roo is looking for the javax.persistence.Entity class which is part of JPA specific jar.
Please find below the error details below:
can't determine annotations of missing type javax.persistence.Entity
when weaving type ****.**.***Test
when weaving classes
when weaving
when batch building BuildConfig[null] #Files=12 AopXmls=#0
[Xlint:cantFindType]
error at (no source information available
Can anyone please advise me how to remove this error without adding a JPA dependency?
You could try to make a push-in from your test .aj file to the related .java file and then remove the necessary code (imports, annotations) that makes that your Spring Roo shell shows that error.
If you are not sure about how to make a push-in, you could read Spring Roo 1.3.2.RELEASE documentation http://docs.spring.io/spring-roo/docs/1.3.2.RELEASE/reference/html/removing.html#removing-step-by-step-1
Regards,

Reaching Spring's tx:advice from pure AspectJ

I enabled LTW for my Spring application and as long as the advised bean in question is free of Spring's AOP features, the classes are woven properly - that is, in-method bytecode modification rather than proxies.
However, once I add tx:annotation-driven and #Transactional to that bean, all methods, even those having nothing to do with Spring features, that previously were woven with proper bytecode, now all get called via JDK proxy.
Which probably means I have to let go of tx:annotation-driven and configure AspectJ to use Spring's tx:advice for methods annotated with Spring's #Transactional.
But how do I access Spring beans from AspectJ configuration?
The only solution I found is to add AspectJ compiler to the build toolchain and implement the AbstractTransactionAspect.aj aspect and on Spring startup call TheImplementedAspect.aspectOf().setTransactionManager().

Eclipse Spring Builder set properties with Groovy beans

I typically use groovy to construct simple bean but the Spring IDE plugin to eclipse fails to build when I try to set a property that is generated by groovy without an explicit setter. For example,
class MyGrooyClass {
def propertyA
}
and in the spring configuration file I have something that looks like:
<bean id="MyGroovyClassBean" class="MyGroovyClass">
<property name="propertyA" value="someValue"/>
</bean>
The spring builder says there is no such property but it is in the bytecode since it is automatically generated by groovy. If I don't validate that bean, everything works, so spring can resolve the property, but it seems to be an issue with the plugin. Is there a way to work around this or to disable validating a particular bean?
EDIT: I can construct the bean using the groovy specific syntax
<lang:groovy id="..." script-source="...">
<lang:property name="propertyA" value="someValue"/>
</lang>
but it seems odd that I should need to do this just for the plugin.
Thanks,
Jeff
It definitely looks like a bug in the Spring IDE plugin. I've also had issues where the content assist does not show auto-complete for properties of a Groovy bean.
I see the same issue in the project I am working on. Consequently I do not use the Spring Validator.
As confirmed by Chris Dail, this is a bug in the Spring IDE plugin. I posted it in the Spring forums http://forum.springsource.org/showthread.php?p=271607&posted=1#post271607 and it has been fixed in the nightly build.