@Configuration class SpinderMan {}
@Service class IronMan {}
@Controller class CaptainAmerica {}
@Repository class Wolverine {}
@Component class MisterFantastic {}
Tout bean Spring est un singleton
@Service
@Scope("prototype") // -> pas singleton
class Ultron {}
Tout bean dans "shared" devrait être @Lazy
@Service
class Thor {
private final Mjolnir mjolnir;
@Autowire
public Thor(@Lazy Mjolnir mjolnir) {
this.mjolnir = mjolnir; // peut être null
}
}
@Service
class CaptainAmerica {
@Transaction
public void handshake() {
// du code
}
@PreAuthorize("true")
public void hug() {
// du code
}
}
@Service
class AntMan {
private final Collection<Ant> ants;
@Autowire
public AntMan(Collection<Ant> ants) {
this.ants = ants;
}
}
Pour un tas de raison Spring va proxifier les classes avec Spring framework 5 + Spring Boot 2, il n'utilise plus que la méthode standard de création de proxy
interface InronMan {
String blast();
}
// ailleurs
InronMan ironMan = (InronMan) Proxy.newProxyInstance(
Thread.currentThread().getContextClassLoader(),
new Class[] { InronMan.class },
(proxy, method, args) -> "Tony Stark"
);
log.info("print => {}", ironMan.blast()); // affiche "print => biz"
class MyTest {
@Test
public void test() {
// GIVEN
ConfigurationManager.getConfigInstance()
.setProperty("BlackPanther", "T'Challa");
// WHEN
String myVar = DynamicPropertyFactory.getInstance()
.getStringProperty("BlackPanther", " T'Chaka")
.get();
// THEN
assertThat(myVar).isEqualsTo("T'Challa");
}
@After
public void after() {
ConfigurationManager.getConfigInstance().clear();
}
}
public class AwesomeTest {
@Rule
public MockitoRule mockitoRule = MockitoJUnit.rule();
@Mock
private IncredibleService incredibleService;
@InjectMocks
private InvincibleService invincibleService;
@Test
public void test() {
// some useless test
}
}