How to avoid beans which are not required in Spring during component scan?
up vote
-1
down vote
favorite
In my project we have around 30 beans in a package. I want to load only a Details bean from that package using component scan and how to avoid other beans loading ? In the below example i am interested in only on Details bean other beans not required to load? But the below one is not working?
Example :
com.spring.test
Student.java
Details.java
Contacts.java
@Configuration
@ComponentScan(basePackages={,"com.spring.test"},
includeFilters=@Filter(type=FilterType.REGEX, pattern="com.spring.test.Details"), excludeFilters=@Filter(type=FilterType.REGEX, pattern="com.spring.test.*^((?!Details).)*$"))
public class AppConfig {
}
java regex spring spring-mvc spring-boot
add a comment |
up vote
-1
down vote
favorite
In my project we have around 30 beans in a package. I want to load only a Details bean from that package using component scan and how to avoid other beans loading ? In the below example i am interested in only on Details bean other beans not required to load? But the below one is not working?
Example :
com.spring.test
Student.java
Details.java
Contacts.java
@Configuration
@ComponentScan(basePackages={,"com.spring.test"},
includeFilters=@Filter(type=FilterType.REGEX, pattern="com.spring.test.Details"), excludeFilters=@Filter(type=FilterType.REGEX, pattern="com.spring.test.*^((?!Details).)*$"))
public class AppConfig {
}
java regex spring spring-mvc spring-boot
1
Don't put 30 unrelated beans in the same package then.
– Boris the Spider
16 hours ago
it is already defined in my project i should not move to another package
– user3094331
16 hours ago
Then don't scan and just create an@Bean
method for it. Also the fact that thoseStudent
andContacts
classes are in there makes me even wonder you should even scan those. Those look more like domain objects then services etc. to me.
– M. Deinum
14 hours ago
add a comment |
up vote
-1
down vote
favorite
up vote
-1
down vote
favorite
In my project we have around 30 beans in a package. I want to load only a Details bean from that package using component scan and how to avoid other beans loading ? In the below example i am interested in only on Details bean other beans not required to load? But the below one is not working?
Example :
com.spring.test
Student.java
Details.java
Contacts.java
@Configuration
@ComponentScan(basePackages={,"com.spring.test"},
includeFilters=@Filter(type=FilterType.REGEX, pattern="com.spring.test.Details"), excludeFilters=@Filter(type=FilterType.REGEX, pattern="com.spring.test.*^((?!Details).)*$"))
public class AppConfig {
}
java regex spring spring-mvc spring-boot
In my project we have around 30 beans in a package. I want to load only a Details bean from that package using component scan and how to avoid other beans loading ? In the below example i am interested in only on Details bean other beans not required to load? But the below one is not working?
Example :
com.spring.test
Student.java
Details.java
Contacts.java
@Configuration
@ComponentScan(basePackages={,"com.spring.test"},
includeFilters=@Filter(type=FilterType.REGEX, pattern="com.spring.test.Details"), excludeFilters=@Filter(type=FilterType.REGEX, pattern="com.spring.test.*^((?!Details).)*$"))
public class AppConfig {
}
java regex spring spring-mvc spring-boot
java regex spring spring-mvc spring-boot
edited 16 hours ago
asked 18 hours ago
user3094331
1009
1009
1
Don't put 30 unrelated beans in the same package then.
– Boris the Spider
16 hours ago
it is already defined in my project i should not move to another package
– user3094331
16 hours ago
Then don't scan and just create an@Bean
method for it. Also the fact that thoseStudent
andContacts
classes are in there makes me even wonder you should even scan those. Those look more like domain objects then services etc. to me.
– M. Deinum
14 hours ago
add a comment |
1
Don't put 30 unrelated beans in the same package then.
– Boris the Spider
16 hours ago
it is already defined in my project i should not move to another package
– user3094331
16 hours ago
Then don't scan and just create an@Bean
method for it. Also the fact that thoseStudent
andContacts
classes are in there makes me even wonder you should even scan those. Those look more like domain objects then services etc. to me.
– M. Deinum
14 hours ago
1
1
Don't put 30 unrelated beans in the same package then.
– Boris the Spider
16 hours ago
Don't put 30 unrelated beans in the same package then.
– Boris the Spider
16 hours ago
it is already defined in my project i should not move to another package
– user3094331
16 hours ago
it is already defined in my project i should not move to another package
– user3094331
16 hours ago
Then don't scan and just create an
@Bean
method for it. Also the fact that those Student
and Contacts
classes are in there makes me even wonder you should even scan those. Those look more like domain objects then services etc. to me.– M. Deinum
14 hours ago
Then don't scan and just create an
@Bean
method for it. Also the fact that those Student
and Contacts
classes are in there makes me even wonder you should even scan those. Those look more like domain objects then services etc. to me.– M. Deinum
14 hours ago
add a comment |
2 Answers
2
active
oldest
votes
up vote
0
down vote
Add @Lazy in your bean definition or add @Lazy at class level to load all beans lazily.
New contributor
add a comment |
up vote
0
down vote
Your Java config file should be marked with @Configuration
annotation. After then your Spring application loads configuration from this file.
See: https://docs.spring.io/spring-javaconfig/docs/1.0.0.M4/reference/html/ch02.html
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
Add @Lazy in your bean definition or add @Lazy at class level to load all beans lazily.
New contributor
add a comment |
up vote
0
down vote
Add @Lazy in your bean definition or add @Lazy at class level to load all beans lazily.
New contributor
add a comment |
up vote
0
down vote
up vote
0
down vote
Add @Lazy in your bean definition or add @Lazy at class level to load all beans lazily.
New contributor
Add @Lazy in your bean definition or add @Lazy at class level to load all beans lazily.
New contributor
New contributor
answered 17 hours ago
Ajish Thankachan
411
411
New contributor
New contributor
add a comment |
add a comment |
up vote
0
down vote
Your Java config file should be marked with @Configuration
annotation. After then your Spring application loads configuration from this file.
See: https://docs.spring.io/spring-javaconfig/docs/1.0.0.M4/reference/html/ch02.html
add a comment |
up vote
0
down vote
Your Java config file should be marked with @Configuration
annotation. After then your Spring application loads configuration from this file.
See: https://docs.spring.io/spring-javaconfig/docs/1.0.0.M4/reference/html/ch02.html
add a comment |
up vote
0
down vote
up vote
0
down vote
Your Java config file should be marked with @Configuration
annotation. After then your Spring application loads configuration from this file.
See: https://docs.spring.io/spring-javaconfig/docs/1.0.0.M4/reference/html/ch02.html
Your Java config file should be marked with @Configuration
annotation. After then your Spring application loads configuration from this file.
See: https://docs.spring.io/spring-javaconfig/docs/1.0.0.M4/reference/html/ch02.html
answered 17 hours ago
h0nzan
1812213
1812213
add a comment |
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53236602%2fhow-to-avoid-beans-which-are-not-required-in-spring-during-component-scan%23new-answer', 'question_page');
}
);
Post as a guest
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
1
Don't put 30 unrelated beans in the same package then.
– Boris the Spider
16 hours ago
it is already defined in my project i should not move to another package
– user3094331
16 hours ago
Then don't scan and just create an
@Bean
method for it. Also the fact that thoseStudent
andContacts
classes are in there makes me even wonder you should even scan those. Those look more like domain objects then services etc. to me.– M. Deinum
14 hours ago