1. 程式人生 > >spring筆記(2)spring的3種IOC容器配置方式

spring筆記(2)spring的3種IOC容器配置方式

enc get utf-8 xmla height frame lan system ()

1.通過 @Configuration 和 @bean 實現

@Configuration
public class Ch2BeanConfiguration {

@Bean
public AccountService accountService(){
AccountServiceImpl bean = new AccountServiceImpl();
bean.setAccountDao(accountDao());
return bean;
}

public AccountDao accountDao(){
AccountDaoInMemoryImpl bean = new AccountDaoInMemoryImpl();
return bean;
}
}
public class Main
{
public static void main( String[] args )
{
AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(Ch2BeanConfiguration.class);
//在容器中獲取bean類
AccountService accountService = applicationContext.getBean("accountService", AccountService.class);

System.out.println("Before money transfer");
System.out.println("Account 1 balance :" + accountService.getAccount(1L).getBalance());
System.out.println("Account 2 balance :" + accountService.getAccount(2L).getBalance());
accountService.transferMoney(1L,2L,5.0);
System.out.println("After money transfer");
System.out.println("Account 1 balance :" + accountService.getAccount(1L).getBalance());
System.out.println("Account 2 balance :" + accountService.getAccount(2L).getBalance());
}
}


2.通過 xml 實現
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

<bean id="accountService" class="org.lin.maven.test.AccountServiceImpl">
<property name="accountDao" ref="accountDao"/>
</bean>

<bean id="accountDao" class="org.lin.maven.test.AccountDaoInMemoryImpl"></bean>
</beans>
public class Main
{
public static void main( String[] args )
{
ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("file:E:\\spring-book-ch2\\src\\main\\resources\\ch2-beans.xml");
//在容器中獲取bean類
AccountService accountService = applicationContext.getBean("accountService", AccountService.class);

System.out.println("Before money transfer");
System.out.println("Account 1 balance :" + accountService.getAccount(1L).getBalance());
System.out.println("Account 2 balance :" + accountService.getAccount(2L).getBalance());
accountService.transferMoney(1L,2L,5.0);
System.out.println("After money transfer");
System.out.println("Account 1 balance :" + accountService.getAccount(1L).getBalance());
System.out.println("Account 2 balance :" + accountService.getAccount(2L).getBalance());
}
}
3.通過 註解 和 xml 實現
在對應的類上寫@Component @Repository @Service @Controller
@Component 最普通的組件,可以被註入到spring容器進行管理
@Repository 作用於持久層
@Service 作用於業務邏輯層
@Controller 作用於表現層(spring-mvc的註解)
因為原生的java操作數據庫所產生的異常只定義了幾種,但是產生數據庫異常的原因卻有很多種,這樣對於數據庫操作的報錯排查造成了一定的影響;而Spring拓展了原生的持久層異常,針對不同的產生原因有了更多的異常進行描述。所以,在註解了@Repository的類上如果數據庫操作中拋出了異常,就能對其進行處理,轉而拋出的是翻譯後的spring專屬數據庫異常,方便我們對異常進行排查處理 
public class Main
{
public static void main( String[] args )
{

ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("file:E:\\spring-book-ch2\\src\\main\\java\\org\\lin\\maven\\test\\resources\\ch2-beans.xml");
//在容器中獲取bean類
AccountService accountService = applicationContext.getBean("accountService", AccountService.class);

System.out.println("Before money transfer");
System.out.println("Account 1 balance :" + accountService.getAccount(1L).getBalance());
System.out.println("Account 2 balance :" + accountService.getAccount(2L).getBalance());
accountService.transferMoney(1L,2L,5.0);
System.out.println("After money transfer");
System.out.println("Account 1 balance :" + accountService.getAccount(1L).getBalance());
System.out.println("Account 2 balance :" + accountService.getAccount(2L).getBalance());
}
}
註:ClassPathXmlApplicationContext路勁如果找不到,直接用("file:絕對路徑")

spring筆記(2)spring的3種IOC容器配置方式