ApplicationReadyEvent not listened to when declaring #ComponentScan for library package - spring

#componentscan({"com.test.cloud"})
#SpringBootApplication
public class TestClass {
public static void main(String[] args) {
ApplicationContext context = SpringApplication.run(TestClass.class, args);
}
}
public class TestClass2{
#eventlistener(ApplicationReadyEvent.class)
public void testMethod() {
.....
.....
}
}
Here if I remove #componentscan({"com.test.cloud"}) annotation then the testMethod is triggered. But when I use #componentscan({"com.test.cloud"}) then the testMethod is not triggered.
SpringBoot Version: 1.5.10.RELEASE

Worked by adding:
#componentscan({"com.test.cloud","com.example.demo"})

Related

How to do integration tests with ClasspathBeanDefinitionScanner in main class springboot

here's the test I created with JPA
import static org.assertj.core.api.Assertions.assertThat;
#AutoConfigureTestDatabase(replace = AutoConfigureTestDatabase.Replace.NONE)
#ExtendWith(SpringExtension.class)
#DataJpaTest
public class IntegrationTest {
#Autowired
private JpaUserRepository jpaUser;
#Test
#DisplayName("Test JPA")
void given123Password_whenPasswordIsNotValid_thenIsFalse() {
Boolean teste = this.jpaUser.existsById("Bruno");
assertThat(teste).isEqualTo(true);
}
}
here`s my main class
#SpringBootApplication
public class CleanArchitectureApplication {
public static void main(String[] args) {
SpringApplication.run(CleanArchitectureApplication.class);
}
}
Success case [https://i.stack.imgur.com/APbBu.png][1]
The problem starts when I add a classPathBeanScanner in my main class
#SpringBootApplication
public class CleanArchitectureApplication {
public static void main(String[] args) {
SpringApplication.run(CleanArchitectureApplication.class);
}
#Bean
BeanFactoryPostProcessor beanFactoryPostProcessor(ApplicationContext beanRegistry) {
return beanFactory -> {
genericApplicationContext((BeanDefinitionRegistry) ((AnnotationConfigServletWebServerApplicationContext) beanRegistry).getBeanFactory());
};
}
void genericApplicationContext(BeanDefinitionRegistry beanRegistry) {
ClassPathBeanDefinitionScanner beanDefinitionScanner = new ClassPathBeanDefinitionScanner(beanRegistry);
beanDefinitionScanner.addIncludeFilter(removeModelAndEntitiesFilter());
beanDefinitionScanner.scan("com.baeldung.pattern.cleanarchitecture");
}
static TypeFilter removeModelAndEntitiesFilter() {
return (MetadataReader mr, MetadataReaderFactory mrf) -> !mr.getClassMetadata()
.getClassName()
.endsWith("Model");
}
}
Error case [https://i.stack.imgur.com/IL0Qf.png][2]
I'm trying to implement clean architecture and abstracting main class in spring boot from this article I'm reading[https://www.baeldung.com/spring-boot-clean-architecture](https://www.stackoverflow.com/). but the problem starts when I try to do the integration test and it conflicts contexts and beans after adding the classPathBeanDefinitionScanner.
I've tried setting up different test contexts and different beans.
[1]: https://i.stack.imgur.com/APbBu.png
[2]: https://i.stack.imgur.com/IL0Qf.png

spring boot "#ComponentScan(nameGenerator = CustomGenerator.class)" not working jpaRepository bean

#ComponentScan(nameGenerator = CustomGenerator.class)
#SpringBootApplication
public class SampleApplication {
public static void main(String[] args) {
SpringApplication.run(SampleApplication.class, args);
}
}
if you do it with the above code, the name of jpa Repository Bean will not be changed
#SpringBootApplication
public class SampleApplication {
public static void main(String[] args) {
new SpringApplicationBuilder()
.beanNameGenerator(new CustomGenerator())
.sources(SampleApplication.class)
.run();
}
}
If the code is used above, the name of the jpa Repository bean will be changed, but the CustomGenerator will not be injected during the test
what should i do!! helppppp
it is the same as above!!

Why Spring Boot AOP pointcut not triggered

wanner test spring boot(1.5.20) aop with minimum code
class being aopped,
#Component
public class Test {
public Test() {
System.out.println("test constr");
}
public void print() {
System.out.println("test print");
}
}
aop class
#Aspect
#Component
public class LoggingAspect {
public LoggingAspect() {
System.out.println("aspect constr");
}
#After("execution(* *.Test.*(..))")
public void log(JoinPoint joinPoint) {
System.out.println("aspect print");
}
}
main class
#SpringBootApplication
#EnableAspectJAutoProxy(proxyTargetClass = true)
public class AopApplication implements CommandLineRunner {
public static void main(String[] args) {
SpringApplication.run(AopApplication.class, args);
}
#Autowired
private Test test;
#Override
public void run(String... strings) throws Exception {
test.print();
}
}
both Test bean and LoggingAspect bean is created. Test.pring is executed. However, the pointcut log() is never triggered. I searched so and found no answer. I also tried #EnableAspectJAutoProxy with proxyTargetClass = True or False. In my understanding this params force to use cglib for Test class.
please let me know what I missed
figure out. change from .Test. to com.example.aop.Test.*, then works.

#SpringbootApplication does not scan components in default package

Hi I have my main class of spring boot app in package com
and two other beans defined in com.bbh.fx.pack1.
As per my understanding SpringBootApplication will autoscan all the components in its pakacge and childern package. But it is not scanning. Not sure what i am missing
**package com.bbh.fx.pack1;**
import org.springframework.stereotype.Component;
import javax.annotation.PostConstruct;
#Component
public class BeanA {
#PostConstruct
public void init() {
System.out.println("in BEANA");
}
}
**package com;**
#SpringBootApplication
public class MainApp{
public static void main(String[] args) {
SpringApplication.run(MainApp.class, args);
}
}
You do not have a main in your MainApp your code should look like:
#SpringBootApplication
public class MainApp{
public static void main(String[] args) {
SpringApplication.run(MainApp.class, args);
}
}
I usually like to start all new applications from https://start.spring.io/ you can choose your project name, build type (maven or gradle) and select any spring dependencies you'd like included in the application.
Use #ComponentScan to scan components.
#SpringBootApplication
#ComponentScan("com")
public class MainApp{
public static void main(String[] args) {
SpringApplication.run(MainApp.class, args);
}
}

Why #PostConstruct not invoked in spring container?

I tried to add some entities in the db shema
config:
#Configuration
#ComponentScan(ApplicationConfig.basePackage)
public class ApplicationConfig {
public final static String basePackage = "test"
}
spring container invocation:
public class StartApp {
public static void main(String... args) throws Exception{
ApplicationContext context = new AnnotationConfigApplicationContext(ApplicationConfig.class);
TestEntityRepository repository = (TestEntityRepository) context.getBean("testEntityRepository");
repository.save(new TestEntity("test"));
}
}
target class with annotation:
public class PersistenceService {
#Autowired
TestEntityRepository testEntityRepository;
#PostConstruct
public void initialize(){
//repository.deleteAll();
testEntityRepository.save(new TestEntity("test1"));
testEntityRepository.save(new TestEntity("test2"));
testEntityRepository.save(new TestEntity("test3"));
}
}
as the result in table only one record - "test". At the Tomcat all works fine.
https://github.com/GlebSa/TestSpringJPA
It seems your PersistenceServiceis not recognized as a Service. Can you add the #Service to PersistenceService?
#Service
public class PersistenceService {
...
}
Hope this help.

Resources