BAEL-3828 Where Should @Service Annotation Be Kept? (#10430)
* BAEL-3828 add code samples and tests for the article * BAEL-3828 fix maven test folder structure and test executions * BAEL-3828 fix indentation in continuation lines
This commit is contained in:
+22
@@ -0,0 +1,22 @@
|
||||
package com.baeldung.annotations.service;
|
||||
|
||||
import com.baeldung.annotations.service.abstracts.AbstractAuthenticationService;
|
||||
import com.baeldung.annotations.service.interfaces.AuthenticationService;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
|
||||
@SpringBootApplication
|
||||
public class AuthApplication {
|
||||
|
||||
@Autowired
|
||||
private AuthenticationService inMemoryAuthService;
|
||||
|
||||
@Autowired
|
||||
private AbstractAuthenticationService ldapAuthService;
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(AuthApplication.class, args);
|
||||
}
|
||||
|
||||
}
|
||||
+12
@@ -0,0 +1,12 @@
|
||||
package com.baeldung.annotations.service.abstracts;
|
||||
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
@Service
|
||||
public abstract class AbstractAuthenticationService {
|
||||
|
||||
public boolean authenticate(String username, String password) {
|
||||
return false;
|
||||
}
|
||||
|
||||
}
|
||||
+14
@@ -0,0 +1,14 @@
|
||||
package com.baeldung.annotations.service.concretes;
|
||||
|
||||
import com.baeldung.annotations.service.interfaces.AuthenticationService;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
@Service
|
||||
public class InMemoryAuthenticationService implements AuthenticationService {
|
||||
|
||||
@Override
|
||||
public boolean authenticate(String username, String password) {
|
||||
return false;
|
||||
}
|
||||
|
||||
}
|
||||
+14
@@ -0,0 +1,14 @@
|
||||
package com.baeldung.annotations.service.concretes;
|
||||
|
||||
import com.baeldung.annotations.service.abstracts.AbstractAuthenticationService;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
@Service
|
||||
public class LdapAuthenticationService extends AbstractAuthenticationService {
|
||||
|
||||
@Override
|
||||
public boolean authenticate(String username, String password) {
|
||||
return true;
|
||||
}
|
||||
|
||||
}
|
||||
+10
@@ -0,0 +1,10 @@
|
||||
package com.baeldung.annotations.service.interfaces;
|
||||
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
@Service
|
||||
public interface AuthenticationService {
|
||||
|
||||
boolean authenticate(String username, String password);
|
||||
|
||||
}
|
||||
-23
@@ -1,23 +0,0 @@
|
||||
package com.baeldung.annotations;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
import static org.junit.jupiter.api.Assertions.assertAll;
|
||||
|
||||
public class EmployeeApplicationTest {
|
||||
private final ApplicationContextRunner contextRunner = new ApplicationContextRunner()
|
||||
.withUserConfiguration(EmployeeApplication.class);
|
||||
|
||||
@Test
|
||||
void whenApplicationContextRuns_thenContainAllDefinedBeans() {
|
||||
contextRunner.run(context -> assertAll(
|
||||
() -> assertTrue(context.containsBeanDefinition("employee")),
|
||||
() -> assertTrue(context.containsBeanDefinition("seniorEmployee")),
|
||||
() -> assertTrue(context.containsBeanDefinition("doctor")),
|
||||
() -> assertTrue(context.containsBeanDefinition("hospital")),
|
||||
() -> assertFalse(context.containsBeanDefinition("student")),
|
||||
() -> assertTrue(context.containsBeanDefinition("teacher"))));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user