CS393 Week2
CS393 Week2
CS 393
Developing Backend Applications
with Spring Framework
Esma Meral
[email protected]
Week - 2
@
Bean CustomerRepository
2.Annotation
Definitions
Based
ProductController
3.Java Based
@Component
@Component("paymentService")
public class PaymentServiceImpl implements PaymentService {
Application Context
paymentService -> instance of PaymentServiceImpl
@Configuration
public class AppConfig {
@Bean
public ProductRepository createProductRepo() {
//Object Creation
ProductRepository repo=new ProductRepository();
repo.setUrl("jdbc:derby://localhost:1527/demodb;create=true");
repo.setUsername("emeral");
repo.setPassword("password");
return repo;
}
}
@Configuration
public class Application {…}
@Configuration
@ComponentScan(basePackages ={"com.sample.repo","com.ozyegin.demo"})
public class Application {
@Configuration @Configuration
@Import(ConfigA.class) public class ConfigA {
public class ConfigB { @Bean
@Bean public A a() {
public B b() { return new A();
return new B(); }
} }
}
ClassPathXmlApplicationContext
FileSystemXmlApplicationContext
ApplicationContext
AnnotationConfigApplicationContext
WebApplicationContext
ApplicationContext context =
new ClassPathXmlApplicationContext(new String[]
{"services.xml", "daos.xml"});
Configuration Spring
Application Context
produces
Context
Application Context
transferService
TransferServiceImpl
transferRepository
JdbcTransferRepository
dataSource
DriverManagerDataSource
transfer(“$50”,”1”,”2”)
TransactionInterceptor TransferServiceImpl
begin commit
TransactionInterceptor
●Without IoC:
▪ Many people, many jugs of water, many plastic cups.
▪ When somebody wants to drink water, he fills a cup,
drinks it, and throws the cup on the floor.
▪ After an hour or something, we have a floor covered with
plastic cups and water.
https://martinfowler.com/articles/injection.html#InversionOfControl
} @Autowired without a
@Qualifier
@Component("visa")
public class VisaPaymentServiceImpl implements CreditCardPaymentService{ matches any instance
CardPaymentService
} regardless of the name.
@Component("paymentService") If there is more than one,
public class PaymentServiceImpl implements PaymentService { we need to qualify it
@Autowired
public PaymentServiceImpl( @Qualifier("visa") CreditCardPaymentService cardPaymentService ) {
...
}
}
CS 393 Developing Backend Applications with Spring Framework Esma Meral 37
Setter Injection
@Component("visa")
public class VisaPaymentServiceImpl implements CreditCardPaymentService{
}
} @Resource requires a @Qualifier
CS 393 Developing Backend Applications with Spring Framework Esma Meral 38
Injecting Scalar Values
equivalent
@Bean
@Lazy
public MyLazyBean lazyBean () {
return new MyLazyBean();
}
IoC & DI
@Component("barFormatter")
public class BarFormatter implements Formatter {
... NoUniqueBeanDefinition
}
Exception
@Component
public class FooService {
@Autowired
private Formatter formatter;
}
CS 393 Developing Backend Applications with Spring Framework Esma Meral 45
Autowire Need for Disambiguation…
●By using the @Qualifier annotation, eliminate the issue of which bean
needs to be injected.
@Autowired
@Qualifier("fooFormatter")
private Formatter formatter;
}
@Autowired
private Formatter fooFormatter;
@Component
public class FooFormatter implements Formatter {
//...
}
CS 393 Developing Backend Applications with Spring Framework Esma Meral 50
Reference
https://www.baeldung.com/spring-qualifier-annotation