Ich lerne Frühlings- und Spring Boot und machte ein einfaches Bildungsprojekt. Dies ist nur ein einfaches Spring -Boot -Projekt ohne angegebene Konfigurationen.
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
4.0.0
org.springframework.boot
spring-boot-starter-parent
3.4.4
ep
ch9
0.0.1-SNAPSHOT
ch9
Demo project for Spring Boot
21
org.springframework.boot
spring-boot-starter-web
org.springframework.boot
spring-boot-starter-test
test
org.springframework
spring-aspects
org.aspectj
aspectjweaver
compile
org.springframework.boot
spring-boot-starter-thymeleaf
org.springframework.boot
spring-boot-maven-plugin
< /code>
ep.ch9.Controllers.Logincontroller:
@Controller
public class LoginController {
@Autowired
ApplicationContext applicationContext;
@GetMapping("/login")
...
}
@PostMapping("/login")
public String loginPost(
@Autowired LoginProcessor loginProcessor,
@Autowired LoggedUserManagementService loggedUserManagementService,
@RequestParam String username,
@RequestParam String password,
Model model) {
if(loginProcessor != null) {
System.out.println("loginProcess is not NULL!");
}
if(loggedUserManagementService != null) {
System.out.println("loggedUserManagementService is not NULL!");
}
loginProcessor.setUsername(username);
loginProcessor.setPassword(password);
boolean loggedIn = loginProcessor.login();
if(loggedIn) {
return "redirect:/main";
}
model.addAttribute("message", "Login failed!");
return "login.html";
}
}
< /code>
ep.ch9.users.loginProcessor:
@RequestScope
@Service
public class LoginProcessor implements LoginProcessorInterface{
private final LoggedUserManagementService loggedUserManagementService;
private String username;
private String password;
public LoginProcessor(LoggedUserManagementService lums) {
if(lums == null) {
System.out.println("LoginProcessor constructor: Lums is NULL!");
}
this.loggedUserManagementService = lums;
}
public boolean login() {
//some logic
}
//getters, setters
}
< /code>
ep.ch9.users.logged.loggedUserManArtementService:
@SessionScope
@Service
public class LoggedUserManagementService {
private String username;
//setter, getter
}
< /code>
ep.ch9.Configuration.ProjectConfig:
@Configuration
@ComponentScan(basePackages = {"ep.ch9.users", "ep.ch9.users.logged"})
public class ProjectConfig {
}
< /code>
Wenn die Postanforderung empfangen wurde, druckt es: < /p>
Loginprozessor -Konstruktor: Lums ist null! />
LoggedUserManagementService ist nicht null! "this.loggedUsermanagementService" ist null
at ep.ch9.users.loginprozessor.login (loginprozessor.java:33) ~ [Klassen /: na]
at ep.ch9.Controllers.Logincontroller.loginpost (loginpost /loginpost /loginpost /loginpost (loginpost /loginpost /logincontroller (logincontroller /> ....etc ..../p>
< /blockquote>
Spring injiziert Komponenten in die Controller einwandfrei, aber alle anderen Komponenten (Dienste, Repositories) erhalten Bohnen als Null. Was ist dieses Problem? Wird ein weiterer Kontext verwendet, um zu injizieren, dass es nicht erreichen kann? @Autowired ApplicationContext -Feld in LoginProcessor ist ebenfalls gleich Null.
Controller und Bohnen verwenden verschiedene Kontexte ⇐ Java
-
- Similar Topics
- Replies
- Views
- Last post