Reputation: 1957
I'm starting a project based on Stephan Zerhusen's excellent SpringBoot + JWT demo. It installs and runs fine in my SpringToolSuite project. My further development of it is running into problems.
I want to separate Stephan's code (org.zerhusen) from my business logic (com.mypackage). No configuration I've tried of @SpringBootApplication and @ComponentScan have worked for me.
The SpringBoot startup program is:
package org.zerhusen;
[SNIP]
@SpringBootApplication
public class JwtDemoApplication {
public static void main(String[] args) {
SpringApplication.run(JwtDemoApplication.class, args);
}
}
If I merely change the package from org.zerhusen to com.mypackage and run the program again I see:
APPLICATION FAILED TO START
***************************
Description:
Field userRepository in org.zerhusen.security.service.JwtUserDetailsServiceImpl required a bean of type 'org.zerhusen.security.repository.UserRepository' that could not be found.
Action:
Consider defining a bean of type 'org.zerhusen.security.repository.UserRepository' in your configuration.
Now UserRepository is just an interface:
package org.zerhusen.security.repository;
[SNIP]
public interface UserRepository extends JpaRepository<User, Long> {
User findByUsername(String username);
}
but that interface isn't implemented by user code. And because I'm only using H2 at this point (I'll get to JPA and MySQL later) the interface is merely referenced within the implementation of UserDetailsService implementation:
package org.zerhusen.security.service;
[SNIP]
@Service
public class JwtUserDetailsServiceImpl implements UserDetailsService {
@Autowired
private UserRepository userRepository;
@Override
public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
[SNIP]
}
There are lots of stackoverflow articles telling me to use configurations, but I already have:
@SpringBootApplication(scanBasePackages = {"org.zerhusen"})
@ComponentScan(basePackages = {"org.zerhusen"})
@EnableJpaRepositories(basePackages = {"org.zerhusen.security.repository.UserRepository"})
@EntityScan(basePackages = {"org.zerhusen"})
public class JwtDemoApplication {
[SNIP]
}
and none of that works for me.
I don't want to force everything into a single package tree -- how would I meld my code with Stephan's without rewriting his? -- but my configurations aren't getting my program to run.
Can someone give me a correct configuration for my situation?
Thanks, Jerome.
UPDATE ON 7/30, 12:04 CDT Using the comments I changed the @EnableJpaRepositories statement, using just the class. With that change the application works properly.
With that in mind I tried variations, commenting out one annotation at a time:
I'll take this success (a working app thanks to all responders) but don't yet understand why exactly these four annotations are needed.
Upvotes: 2
Views: 999
Reputation: 2431
In your @EnableJpaRepositories
you can use basePackagesClass
instead of basePackages
or set org.zerhusen.security.repository
if you prefer to keep basePackages
.
Upvotes: 2
Reputation: 77167
The problem is in your @EnableJpaRepositories
--you specified the class name instead of the package name. Just drop the arguments entirely (and, in fact, you should be able to eliminate everything but @SpringBootApplication
--@ComponentScan
is included in it).
As a rule, Boot's defaults are a good general-purpose compromise, so don't start explicitly specifying things unless you have an actual clash with the defaults.
Upvotes: 3