Generate method aspectOf() with gradle - java

Can't generate aspectOf() method wih gradle plugin.
My AspectJ config class and some aspect class:
#Aspect
public class SendToAspect {
#Around("execution (public * *(..)) && #annotation(ann)")
public Object execute(final ProceedingJoinPoint point, final SendTo ann) throws Throwable {
return point.proceed();
}
}
#Configuration
#EnableAspectJAutoProxy
public class AspectjConfiguration {
#Bean
public SendToAspect sendToAspect() {
return Aspects.aspectOf(SendToAspect.class);
}
}
My build.gradle:
plugins {
id 'org.springframework.boot' version '2.2.7.RELEASE'
id 'io.spring.dependency-management' version '1.0.8.RELEASE'
id "io.freefair.aspectj.base" version "5.1.0"
}
allprojects {
apply plugin: 'io.spring.dependency-management'
repositories {
mavenCentral()
}
dependencyManagement {
dependencies {
dependency 'org.projectlombok:lombok:1.18.12'
}
}
}
subprojects {
apply plugin: 'java'
group 'com.example'
version '0.0.1-snapshot'
sourceCompatibility = 1.8
dependencies {
compileOnly 'org.projectlombok:lombok'
}
}
configure([project(':my-service')]) {
apply plugin: 'org.springframework.boot'
apply plugin: 'io.freefair.aspectj.base'
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-web'
implementation 'org.springframework.boot:spring-boot-starter-aop'
implementation 'org.springframework.boot:spring-boot-starter-test'
implementation 'org.springframework:spring-aspects'
implementation 'org.aspectj:aspectjrt'
}
}
After building and running my springBoot app I get the same error:
error creating bean with name 'sendToAspect' defined in class path resource
[com/example/config/aspectj/AspectjConfiguration.class]: Bean instantiation via factory method failed;
nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.example.utils.aop.SendToAspect]:
Factory method 'sendToAspect' threw exception; nested exception is org.aspectj.lang.NoAspectBoundException:
Exception while initializing com.restaurantclub.soa.utils.aop.SendToAspect: java.lang.NoSuchMethodException: com.restaurantclub.soa.utils.aop.SendToAspect.aspectOf()
What needs to be done or which plugin should i use to work?
My Gradle version: 5.2.1

Solution - just use 'io.freefair.aspectj.post-compile-weaving' instead io.freefair.aspectj.base

Related

Java/Spring error: 'create()' in 'reactor.netty.http.server.HttpServer' cannot be applied to '(java.lang.String, int)'

I am doing a Java / Spring course. Here is the code:
package com.pinodev.helloServer;
import reactor.netty.http.server.HttpServer;
public class HelloServerApplication {
public static void main(String[] args) {
HttpServer server = HttpServer.create("localhost",8080);
}
}
build.gradle:
plugins {
id 'org.springframework.boot' version '2.4.2'
id 'io.spring.dependency-management' version '1.0.11.RELEASE'
id 'java'
}
group = 'com.pinodev'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '11'
repositories {
mavenCentral()
}
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-webflux'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
testImplementation 'io.projectreactor:reactor-test'
}
test {
useJUnitPlatform()
}
This is where the error occurs
error: 'create()' in 'reactor.netty.http.server.HttpServer' cannot be applied to '(java.lang.String, int)'
You should create the server as follows:
HttpServer.create()
.host("0.0.0.0")
.port(8080)
.handle((req, res) -> res.sendString(Flux.just("hello")))
.bind()
.block();
The method create() is the only one available in HttpServer. The create with two parameters must have existed in a previous version.
class HttpServer doesn't have parameters in create() method, for setting it you should use builder method for example:
HttpServer.create()
.host("127.0.0.1")
.port(8080)
.handle((req, res) -> res.sendString(Flux.just("hello")))
.bind()
.block();
see more here

Cannot resolve reference to bean 'mongoTemplate' while setting bean property 'mongoOperations' while deploying AWS Lambda

I have a spring cloud function project for running the code in AWS Lambda.
The spring boot application runs fine locally. But gives the below error when deployed in AWS Lambda.
Error:-
{
"errorMessage": "Error creating bean with name 'associationService' defined in file [/var/task/org/qmetech/service/AssociationService.class]: Unsatisfied dependency expressed through constructor parameter 0; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'userRepository': Cannot resolve reference to bean 'mongoTemplate' while setting bean property 'mongoOperations'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'mongoTemplate' available",
"errorType": "org.springframework.beans.factory.UnsatisfiedDependencyException",
"stackTrace": [
The project has a root project which has a subproject 'GetAssociationService'
rootproject - build.gradle
plugins {
id 'org.springframework.boot' version '2.2.5.RELEASE'
id 'io.spring.dependency-management' version '1.0.9.RELEASE'
id 'java'
id 'com.github.johnrengelman.shadow' version '5.2.0'
}
bootJar { enabled = false }
jar { enabled = true }
subprojects {
group = 'org.qmetech'
version = '1.0.0'
sourceCompatibility = '1.8'
targetCompatibility = '1.8'
apply plugin: 'org.springframework.boot'
apply plugin: 'io.spring.dependency-management'
apply plugin: 'com.github.johnrengelman.shadow'
apply plugin: 'java'
}
repositories { mavenCentral ( ) }
ext { set ( 'springCloudVersion' , "Hoxton.SR3" ) }
dependencies {
testImplementation ( 'org.springframework.boot:spring-boot-starter-test' ) {
exclude group: 'org.junit.vintage' , module: 'junit-vintage-engine'
}
}
ext.libraries = [
commonlibraries: ['org.springframework.boot:spring-boot-starter:2.2.5.RELEASE' ,
'org.springframework.cloud:spring-cloud-function-context' ,
'org.springframework.cloud:spring-cloud-function-adapter-aws:2.0.1.RELEASE' ,
'com.amazonaws:aws-lambda-java-log4j:1.0.0' ,
'org.springframework.boot:spring-boot-starter-web:2.2.5.RELEASE' ,
'org.springframework.cloud:spring-cloud-starter-function-web:1.0.0.RELEASE' ,
'org.springframework.boot.experimental:spring-boot-thin-layout:1.0.11.RELEASE' ,
'org.springframework.cloud:spring-cloud-starter-function-web:1.0.0.RELEASE' ,
'org.springframework.data:spring-data-mongodb:2.2.5.RELEASE'
] ,
]
dependencyManagement {
imports {
mavenBom "org.springframework.cloud:spring-cloud-dependencies:${springCloudVersion}"
}
}
test { useJUnitPlatform ( ) }
ChildProject - build.gradle
sourceCompatibility = 1.8
targetCompatibility = 1.8
repositories {
mavenCentral()
}
springBoot {
mainClassName = 'org.qmetech.GetAssociationService'
}
dependencies {
dependencies {
compile libraries.commonlibraries
implementation 'org.springframework.boot:spring-boot-starter-data-mongodb'
}
}
UserRepository.java
package org.qmetech.repository;
import org.qmetech.domain.User;
import org.springframework.data.mongodb.repository.MongoRepository;
import org.springframework.stereotype.Repository;
#Repository
public interface UserRepository extends MongoRepository<User, Integer> { }
AssociationService.java
package org.qmetech.service;
import org.qmetech.domain.User;
import org.qmetech.repository.UserRepository;
import org.springframework.stereotype.Component;
import java.util.List;
import java.util.function.Function;
#Component
public class AssociationService implements Function<String, List<User>> {
private final UserRepository userRepository;
public AssociationService(UserRepository userRepository) {
this.userRepository = userRepository;
}
#Override
public List<User> apply(String uppercaseRequest) {
List<User> users = userRepository.findAll();
return users;
}
}
The Complete Code can be found here - https://github.com/iftekharkhan09/Services
Can Anyone please tell me what am I doing wrong?
Thanks!
You need to create beans of MongoTemplate since there are none to be injected by default configuration.
#Bean
public MongoClient mongoClient() {
return new MongoClient("localhost", 27017);
}
#Bean
public MongoTemplate mongoTemplate(MongoClient mongoClient) throws Exception {
return new MongoTemplate(mongoClient, "databaseName");
}
Also, don't forget to use com.mongodb.client.MongoClient which replaces reprecated com.mongodb.MongoClient as of Spring Boot 2.2.

Unable to run spark, phoenix/hbase with Spring Boot: Detected both log4j-over-slf4j.jar AND bound slf4j-log4j12.jar on the class path

When I try to compile/build a Spring Boot project with Spark/Phoenix dependencies, it says there's too many SLF4J's, and when i try to exclude a package, it throws the above error.
How can i get them to work together?
buildscript {
ext {
springBootVersion = '1.5.6.RELEASE'
}
repositories {
mavenCentral()
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
}
}
apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'org.springframework.boot'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = 1.8
repositories {
mavenCentral()
}
//THIS DOESN'T WORK
configurations {
compile.exclude group:'ch.qos.logback'
}
dependencies {
compile('org.springframework.boot:spring-boot-starter')
/* EXCLUDING HERE ALSO DIDN'T WORK */
compile('org.springframework.boot:spring-boot-starter-web')
compile('org.apache.spark:spark-sql_2.10:1.5.2')
compile('org.apache.spark:spark-core_2.10:1.5.2')
compile('org.apache.phoenix:phoenix-spark:4.7.0-HBase-1.1')
testCompile('org.springframework.boot:spring-boot-starter-test')
}

Spring boot webflux application won't start IllegalStateException

Here is my build.gradle
buildscript {
ext {
springBootVersion = '2.0.0.M2'
}
repositories {
mavenCentral()
jcenter()
maven { url "https://repo.spring.io/snapshot" }
maven { url "https://repo.spring.io/milestone" }
}
dependencies {
classpath "org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}"
}
}
apply plugin: 'java'
apply plugin: 'org.springframework.boot'
apply plugin: 'io.spring.dependency-management'
sourceCompatibility = 1.8
repositories {
mavenCentral()
jcenter()
maven { url "https://repo.spring.io/snapshot" }
maven { url "https://repo.spring.io/milestone" }
}
dependencies {
compile 'org.springframework.boot:spring-boot-starter-security',
'org.springframework.boot:spring-boot-starter-mail',
'org.springframework.boot:spring-boot-starter-validation',
'org.springframework.boot:spring-boot-starter-webflux',
// support libs
'commons-io:commons-io:2.5',
'commons-codec:commons-codec:1.10',
'org.apache.commons:commons-lang3:3.4',
'org.apache.commons:commons-collections4:4.1',
//validation
'javax.validation:validation-api:1.1.0.Final'
compileOnly 'org.springframework.boot:spring-boot-configuration-processor',
'org.projectlombok:lombok'
testCompile 'org.springframework.boot:spring-boot-starter-test'
}
When I tried to run application it fails with java.lang.IllegalStateException: java.lang.NullPointerException
Stacktrace:
java.lang.IllegalStateException: java.lang.NullPointerException
at org.springframework.boot.web.reactive.context.ReactiveWebServerApplicationContext.stopAndReleaseReactiveWebServer(ReactiveWebServerApplicationContext.java:152) ~[spring-boot-2.0.0.M2.jar:2.0.0.M2]
at org.springframework.boot.web.reactive.context.ReactiveWebServerApplicationContext.refresh(ReactiveWebServerApplicationContext.java:52) ~[spring-boot-2.0.0.M2.jar:2.0.0.M2]
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:750) [spring-boot-2.0.0.M2.jar:2.0.0.M2]
at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:386) [spring-boot-2.0.0.M2.jar:2.0.0.M2]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:327) [spring-boot-2.0.0.M2.jar:2.0.0.M2]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1245) [spring-boot-2.0.0.M2.jar:2.0.0.M2]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1233) [spring-boot-2.0.0.M2.jar:2.0.0.M2]
at com.getcredit.bankscraper.Application.main(Application.java:10) [main/:na]
Caused by: java.lang.NullPointerException: null
at org.springframework.boot.web.embedded.netty.NettyWebServer.stop(NettyWebServer.java:113) ~[spring-boot-2.0.0.M2.jar:2.0.0.M2]
at org.springframework.boot.web.reactive.context.ReactiveWebServerApplicationContext.stopAndReleaseReactiveWebServer(ReactiveWebServerApplicationContext.java:148) ~[spring-boot-2.0.0.M2.jar:2.0.0.M2]
... 7 common frames omitted
Project was generated via start.spring.io site.
After some tests, I've found out my problem. That was an NPE exception in init method of my bean annotated with #PostConstruct. You can reproduce this issue like this:
#Component
class Test {
String str;
#PostConstruct
private void init() {
System.out.println(str.length());
}
}
I don't understand why spring throws so unclear exception.

Springboot war in tomcat not working

I'm trying to deploy a war generated with springboot in Tomcat, in which I have succeeded previously, but can't figure out what is wrong this time.
The most important code is added:
config/AppConfig
#Configuration
#EnableTransactionManagement
#Component
//#EnableWebMvc
#ComponentScan("com.singleuseapps")
public class AppConfig {
LaptopsApplication
#SpringBootApplication
#ComponentScan(basePackageClasses = {AppConfig.class,LaptopsApplication.class})
public class LaptopsApplication extends SpringBootServletInitializer {
#Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
return builder.sources(LaptopsApplication.class);
}
public static void main(String[] args) {
SpringApplication.run(LaptopsApplication.class, args);
}
build.gradle
buildscript {
ext {
springBootVersion = '1.4.2.RELEASE'
}
repositories {
mavenCentral()
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
}
}
apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'org.springframework.boot'
apply plugin: 'idea'
apply plugin: 'war'
springBoot{
executable = true
}
jar {
baseName = 'laptops'
}
war {
baseName = 'laptops'
}
sourceCompatibility = 1.8
targetCompatibility = 1.8
repositories {
mavenCentral()
}
dependencies {
compile('org.springframework.boot:spring-boot-starter-web')
compile("org.springframework.boot:spring-boot-starter-data-jpa")
compile 'org.hibernate:hibernate-core:5.0.9.Final'
compile("com.h2database:h2")
testCompile('org.springframework.boot:spring-boot-starter-test')
compile 'javax.servlet:javax.servlet-api:3.1.0'
compile 'org.javassist:javassist:3.15.0-GA'
compile 'mysql:mysql-connector-java:5.1.31'
compile 'commons-dbcp:commons-dbcp:1.4'
compile('org.springframework.boot:spring-boot-starter-jdbc')
providedRuntime("org.springframework.boot:spring-boot-starter-tomcat")
compileOnly "org.projectlombok:lombok:1.16.10"
compile "io.springfox:springfox-swagger2:2.6.0"
compile 'io.springfox:springfox-swagger-ui:2.6.0'
compile 'com.sun.mail:javax.mail'
compile( 'org.springframework.boot:spring-boot-starter-mail')
}
Hope someone gets why I failed.
[edit]
Line in catalina.out
Caused by: org.springframework.beans.factory.BeanDefinitionStoreException: Failed to parse configuration class [com.singleuseapps.LaptopsApplication]; nested exception is java.io.FileNotFoundException: class path resource [com/singleuseapps/cart/TestMailController.class] cannot be opened because it does not exist
You should:
remove #Component from AppConfig (it's already annotated with #Configuration)
remove .configure method from LaptopsApplication, it's already annotated with #SpringBootApplication and is automatically a configuration class you don't need to add it again
Ensure your controller class is in the package specified by your two #ComponentScan annotations (you probably just need the one on your main app class)

Categories

Resources