Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
Lab Exercises
Homework Exercise
This is provided to show the difference between XML based configuration versus
annotation based configuration which you will do in the next exercise. In this
exercise, you are going to configure your beans by declaring them in the XML
configuration file, beans.xml. In the next exercise, you are going to declare them
using @Service and @Repository annotations.
Tasks to be performed:
1.
2.
package com.softwarecampus.examples.dao;
public class CustomerDaoImpl implements CustomerDao {
package com.softwarecampus.examples.services;
import com.softwarecampus.examples.dao.CustomerDao;
public class CustomerServiceImpl implements CustomerService {
CustomerDao customerDao;
public void setCustomerDao(CustomerDao customerDao) {
this.customerDao = customerDao;
}
public String getCustomerGreeting() {
String greeting = "Hello, " + customerDao.getCustomerName();
return greeting;
}
}
4. Main.java
package com.softwarecampus.examples;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.softwarecampus.examples.services.CustomerService;
public class Main {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext(
new String[] { "beans.xml" });
CustomerService customerService =
(CustomerService) context.getBean("customerService");
System.out.println(customerService.getCustomerGreeting());
}
Learning points:
You can configure Service class with @Service annotation and Dao/Repository
classes with @Repository annotation
Tasks to be perfgormed:
1.
2.
3.
3. CustomerServiceImpl.java
package com.softwarecampus.examples.services;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.softwarecampus.examples.dao.CustomerDao;
@Service("customerService")
public class CustomerServiceImpl implements CustomerService {
@Autowired
CustomerDao customerDao;
public String getCustomerGreeting() {
String greeting = "Hello, " + customerDao.getCustomerName();
return greeting;
}
}
4. Main.java
package com.softwarecampus.examples;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.softwarecampus.examples.services.CustomerService;
public class Main {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext(
new String[] { "beans.xml" });
CustomerService customerService =
(CustomerService) context.getBean("customerService");
System.out.println(customerService.getCustomerGreeting());
}
}
package com.softwarecampus.examples.dao;
import org.springframework.stereotype.Repository;
@Repository("courseDao")
public class CourseDaoImpl implements CourseDao {
2. Modify the application so that the result of running the application displays the following.
Hello, Spring Student, Your are taking Spring Programming!
Tasks to be perfgormed:
1. Build and run "di_Configuration_XML" project
2. Study the project
accountRepository.findById("A123").getBalance()
accountRepository.findById("C456").getBalance()
Transfering 100 dollars from A123 to C456
accountRepository.findById("A123").getBalance()
accountRepository.findById("C456").getBalance()
= 1000.0
= 0.0
= 900.0
= 100.0
</bean>
<bean id = "transferService"
class = "com.softwarecampus.examples.account.service.TransferServiceImpl">
<constructor-arg name="accountRepository" ref="accountRepository"/>
</bean>
</beans>
2. Main.java
package com.softwarecampus.examples.account;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.junit.Assert.assertThat;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.softwarecampus.examples.account.domain.Account;
import com.softwarecampus.examples.account.repository.AccountRepository;
import com.softwarecampus.examples.account.service.TransferService;
public class Main {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext(
new String[] { "beans.xml" });
// retrieve the beans we'll use during testing
AccountRepository accountRepository = (AccountRepository)
context.getBean("accountRepository");
TransferService transferService = (TransferService)
context.getBean("transferService");
// create accounts to test against
accountRepository.add(new Account("A123", 1000.00));
accountRepository.add(new Account("C456", 0.00));
System.out.println("accountRepository.findById(\"A123\").getBalance() = " +
accountRepository.findById("A123").getBalance());
System.out.println("accountRepository.findById(\"C456\").getBalance() = " +
accountRepository.findById("C456").getBalance());
// check account balances before transfer
assertThat(accountRepository.findById("A123").getBalance(), equalTo(1000.00));
assertThat(accountRepository.findById("C456").getBalance(), equalTo(0.00));
// perform transfer
System.out.println("Transfering 100 dollars from A123 to C456 ");
transferService.transfer(100.00, "A123", "C456");
System.out.println("accountRepository.findById(\"A123\").getBalance() = " +
accountRepository.findById("A123").getBalance());
System.out.println("accountRepository.findById(\"C456\").getBalance() = " +
accountRepository.findById("C456").getBalance());
// check account balances after transfer
assertThat(accountRepository.findById("A123").getBalance(), equalTo(900.00));
assertThat(accountRepository.findById("C456").getBalance(), equalTo(100.00));
}
Annotating a class with the @Configuration indicates that the class can be used by
the Spring IoC container as a source of bean definitions.
Tasks to be perfgormed:
1. Build and run "di_Configuration_Java" project
2. Study the project
accountRepository.findById("A123").getBalance()
accountRepository.findById("C456").getBalance()
Transfering 100 dollars from A123 to C456
accountRepository.findById("A123").getBalance()
accountRepository.findById("C456").getBalance()
= 1000.0
= 0.0
= 900.0
= 100.0
com.softwarecampus.examples.account.repository.AccountRepository;
com.softwarecampus.examples.account.repository.InMemoryAccountRepository;
com.softwarecampus.examples.account.service.TransferService;
com.softwarecampus.examples.account.service.TransferServiceImpl;
@Configuration
public class AppConfig {
@Bean
public TransferService transferService() {
return new TransferServiceImpl(accountRepository());
}
@Bean
public AccountRepository accountRepository() {
return new InMemoryAccountRepository();
}
}
2. Main.java
package com.softwarecampus.examples.account;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.junit.Assert.assertThat;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import com.softwarecampus.examples.account.domain.Account;
import com.softwarecampus.examples.account.repository.AccountRepository;
import com.softwarecampus.examples.account.service.TransferService;
public class Main {
public static void main(String[] args) {
// Create a new AnnotationConfigApplicationContext, deriving bean
// definitions from the given annotated classes and automatically
// refreshing the context. Note that we don't use a context
// configuration file in this example.
ApplicationContext ctx = new
AnnotationConfigApplicationContext(AppConfig.class);
// retrieve the beans we'll use during testing
AccountRepository accountRepository = ctx.getBean(AccountRepository.class);
TransferService transferService = ctx.getBean(TransferService.class);
// create accounts to test against
accountRepository.add(new Account("A123", 1000.00));
accountRepository.add(new Account("C456", 0.00));
System.out.println("accountRepository.findById(\"A123\").getBalance() = " +
accountRepository.findById("A123").getBalance());
System.out.println("accountRepository.findById(\"C456\").getBalance() = " +
accountRepository.findById("C456").getBalance());
// check account balances before transfer
assertThat(accountRepository.findById("A123").getBalance(), equalTo(1000.00));
assertThat(accountRepository.findById("C456").getBalance(), equalTo(0.00));
// perform transfer
System.out.println("Transfering 100 dollars from A123 to C456 ");
transferService.transfer(100.00, "A123", "C456");
System.out.println("accountRepository.findById(\"A123\").getBalance() = " +
accountRepository.findById("A123").getBalance());
System.out.println("accountRepository.findById(\"C456\").getBalance() = " +
accountRepository.findById("C456").getBalance());
// check account balances after transfer
assertThat(accountRepository.findById("A123").getBalance(), equalTo(900.00));
assertThat(accountRepository.findById("C456").getBalance(), equalTo(100.00));
}
Homework Exercise
1. The homework is to modify the di_basics_InjectRef project in the basics to use Java-based
configuration.