CAS 7.0.0, how to scan self created beans, and manage by spring

288 views
Skip to first unread message

Char Lin

unread,
Jun 19, 2023, 6:38:33 AM6/19/23
to CAS Community
Hi, all.
Just like the title, CAS 7.0.0, I can't atowired the bean annotated by @Service and @Component... How to configure scan them and add to spring bean factories?

20230619181953.png
From the picture, I started the cas application, but report the 'userService is null'
who can help me, thank you very much!

David Malia

unread,
Jun 19, 2023, 11:51:44 PM6/19/23
to cas-...@apereo.org
This isn't really a CAS issue, but rather understanding how Spring configuration works.

Create a Spring configuration class with the following annotations adjusting accordingly to your environment.

package some.package;
@Configuration
@ComponentScan(basePackages = {"some.package.you.want.scanned","some.other.package.you.want.scanned"})
@EnableConfigurationProperties(CasConfigurationProperties.class)
public class YourSpringConfigurationClass  {
@Autowired
private CasConfigurationProperties casProperties;
.
.
//you can choose to create beans here as well.
.
}

In your MyAuthenticationHandler class,  add  the annotation @Component to it.  This will allow the @ComponentScan annotation to find your bean, and create it.

In the jar file where your class lives,
add a folder META-INF, and a spring.factories file in it.
It typically would live in at src/main/resources/META-INF/spring.factories inside your source directory.

In the spring.factories file, add the line
org.springframework.boot.autoconfigure.EnableAutoConfiguration=some.package.YourSpringConfigurationClass, some.package.AnotherSpringConfigurationClassIfNeeded

This tells Spring where to find your Spring configuration class.



References:



--
- Website: https://apereo.github.io/cas
- Gitter Chatroom: https://gitter.im/apereo/cas
- List Guidelines: https://goo.gl/1VRrw7
- Contributions: https://goo.gl/mh7qDG
---
You received this message because you are subscribed to the Google Groups "CAS Community" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cas-user+u...@apereo.org.
To view this discussion on the web visit https://groups.google.com/a/apereo.org/d/msgid/cas-user/68985f35-fdd1-4077-8f06-612ea0331959n%40apereo.org.

Char Lin

unread,
Jun 20, 2023, 1:44:04 AM6/20/23
to CAS Community, David Malia
Hi, thank you very much for your detailed reply first.
I has tried the approach you proposed no long ago.  as:

0C683B77-DA25-4c02-B029-71AB4F233D8E.png

and added to org.springframework.boot.autoconfigure.AutoConfiguration.imports. But it is no use, these beans annotated by @Component can't create by spring still. 
Of curse sure, @Bean create is correct, but my project will have so many beans in the future, i can't @Bean them one by one... 

Now I have found the solution to this problem. 
It  is missing a file at ./build/classes/java/main/META-INF/spring.components. 
but all modules of CAS7.0.0 exist this file, so these beans created by myself won't be scanned.

Just inject the following dependencies at build.gradle then compile or build project, it will automatically generate 'spring.components' file:
BAAD5B16-5AB2-408b-8206-D13EC90757FD.png

Ray Bon

unread,
Jun 21, 2023, 11:20:24 AM6/21/23
to cas-...@apereo.org, dma...@gmail.com
In addition to David's links

Ray

On Mon, 2023-06-19 at 22:36 -0700, 'Char Lin' via CAS Community wrote:
Notice: This message was sent from outside the University of Victoria email system. Please be cautious with links and sensitive information.

Petr Bodnár

unread,
Jul 1, 2023, 12:33:09 AM7/1/23
to CAS Community, Ray Bon, David Malia
chaof...@foxitsoftware.com, thanks for sharing your findings!

It looks like CAS started using "spring.components" (and the indexer) in v6.4.0-RC2 already - see https://github.com/apereo/cas/commit/9b98f13507c6aae101cae94f338840e02ba09330. I don't quite understand why (the commit message says just "add indexer; fix tests" - not really helpful; moreover when there are still "org.springframework.boot.autoconfigure.AutoConfiguration.imports" files which seem to basically list the same (*Configuration) classes and all CAS beans seem to be created via @Bean methods anyway). I also wonder if this shouldn't be documented, as this change seems to be able to break overlays which were based on an older CAS version and used the @ComponentScan annotation that no longer has the expected effect - as stated here, on SO and in Spring official docs? Feel free to correct me if I overlooked st.
Reply all
Reply to author
Forward
0 new messages