您的位置:首页 > 编程语言 > Java开发

Spring Ioc容器(一)

2016-12-12 14:51 92 查看

配置数据源

Spring Application Framework的核心是其IoC容器。该容器的工作职责包括对应用程序的对象进行实例化、初始化、装配以及在对象的整个生命周期中提供其他Sping功能。那些构成应用程序主要部分且由Spring容器进行管理的对象被称为Bean。虽然它们都是普通的Java对象--也被称为POJO,却是由Spring容器实例化和装配,并进行管理。

配置元数据有三种方式:(一)基于XML格式,(二)基于注解,(三)基于java

首先开始了解基于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="AccountServiceImpl">
<property name="accountDao" ref="accountDao"/>
</bean>

<bean id="accountDao" class="ccountDaoInMemoryImpl">
</bean>

</beans>

上面代码中 所有的bean都被定义在<beans>元素中  每一个bean使用<bean>元素进行定义 bean的名称由id特性定义  应用程序或配置元数据中的其他bean定义可以通过名称来访问相应的bean 
 在上述代码中 accountService Bean有一个被称为accountDao的属性 而该属性又满足配置中所定义accountDao

基于注解配置元数据

@Service("accountService")
public class AccountServiceImpl implements AccountService{
    private AccountDao accountDao;
    @Autowrired
    public void setAccountDao(AccountDao accountDao){
     this.accountDao=accountDao;
  }
}

@Repository("accountDao")
public class AccountDaoInMemoryImpl implements AccountDao{
}
上面代码中 实用化java注解定义Bean 其中注解@Service和@Repository被用来定义两个Bean 实际上 他们又是注解@Component的具体形式 @Autowired通常来指定在运行时被spring容器所注入的Bean依赖

配置和使用容器

spring也是一个java对象 在某些特定的时间点被创建,并且允许管理应用程序的其他部分 可以使用两种不同的方法来实例化spring容器 在独立的应用程序中 可以使用编程的方法 而在web应用程序中 使用声明方法是更好的选择(需要借助web.xml文件中的一些配置来完成)

1:在独立环境中通过基于xml的配置元数据来创建和使用spring容器
 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="AccountServiceImpl">
<property name="accountDao" ref="accountDao"/>
</bean>

<bean id="accountDao" class="ccountDaoInMemoryImpl">
</bean>

</beans>
  讲xml Bean定义文件作为构造函数的参数来实例化spring容器
public class Main {

    public static void main(String []args){

        ClassPathXmlApplicationContext applicationContext=new ClassPathXmlApplicationContext("beans-comfiguration.xml");

        AccountService accountService=applicationContext.getBean("accountService",AccountService.class);

        System.out.println("Before money transfer");

        System.out.println("Account 1 balance :" + accountService.getAccount(1).getBalance());

        System.out.println("Account 2 balance :" + accountService.getAccount(2).getBalance());

        accountService.transferMoney(1, 2, 5.0);

        System.out.println("After money transfer");

        System.out.println("Account 1 balance :" + accountService.getAccount(1).getBalance());

        System.out.println("Account 2 balance :" + accountService.getAccount(2).getBalance());

    }

}

当前代码中 使用一个Xml Bean 定义文件创建了ApplicationContext实例 使用ClassPathXmlApplicationContext去加载XMl配置文件 创建了applicationContext后
通过调用getBean方法去执行一次查找bean

2:在独立环境中通过使用基于Java注解的配置来创建和使用spring容器

先在配置文件配置
 <!--启用注解-->

    <context:annotation-config></context:annotation-config>

<!--自动扫描-->

    <context:component-scan base-package="ch2"></context:component-scan>

编写加载spring类public class Main{
 public static void main(String args[]){

  ClassPathXmlApplicationContext applicationContext=new ClassPathXmlApplicationContext("2/beans-autowrired.xml");

        AccountService accountService=applicationContext.getBean("accountService", AccountService.class);

        System.out.println("Before money transfer");

        System.out.println("Account 1 balance :" + accountService.getAccount(1).getBalance());

        System.out.println("Account 2 balance :" + accountService.getAccount(2).getBalance());

        accountService.transferMoney(1, 2, 5.0);

        System.out.println("After money transfer");

        System.out.println("Account 1 balance :" + accountService.getAccount(1).getBalance());

        System.out.println("Account 2 balance :" + accountService.getAccount(2).getBalance()); 

}
}

下载基于xml和注解配置元数据完整代码和演示

http://download.csdn.net/detail/qq_33913037/9709010

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  xml bean spring maven ioc