spring笔记(2)spring的3种IOC容器配置方式

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:绝对路径")

猜你喜欢

转载自www.cnblogs.com/Tomlin/p/9716034.html