使用注解实现IOC
利用配置文件实现Spring的IOC/DI
①、导入spring的核心架包
②、在src目录下创建applicationContext.xml的配置文件
③、和上篇文章所创包一致
④、在applicationContext.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"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd">
<!-- 使用注解实现IOC -->
<context:component-scan base-package="com.web"/>
<!--
<bean name="userAction" class="com.web.action.UserAction" scope="prototype">
<property name="userService" ref="userServiceImpl"></property>
</bean>
-->
<!-- set注入 -->
<!--
<bean name="userServiceImpl" class="com.web.service.impl.UserServiceImpl">
<property name="userDAO" ref="userDAOImpl"></property>
</bean>
-->
<!-- 构造注入 -->
<!--
<bean name="userServiceImpl" class="com.web.service.impl.UserServiceImpl">
<constructor-arg ref="userDAOImpl"></constructor-arg>
</bean>
-->
<!-- 配置此类受spring容器所管理的组件 -->
<!--
<bean name="userDAOImpl" class="com.web.dao.impl.UserDAOImpl"></bean>
-->
<bean name="user" class="com.web.bean.User">
<!-- 简单属性注入 -->
<property name="username" value="tyh"></property>
<property name="password" value="123456"></property>
<!-- 集合装配 -->
<!-- 装配List集合 -->
<property name="list">
<list>
<value>lifan</value>
<value>zhangsan</value>
<value>lisi</value>
<value>lifan</value>
<value>wangwu</value>
</list>
</property>
<!-- 装配set集合 -->
<property name="set">
<set>
<value>lifan</value>
<value>zhangsan</value>
<value>lisi</value>
<value>lifan</value>
<value>wangwu</value>
</set>
</property>
<!-- 装配map集合 -->
<property name="map">
<map>
<entry key="110" value="lifan1"></entry>
<entry key="111" value="lifan2"></entry>
<entry key="112" value="lifan3"></entry>
</map>
</property>
</bean>
<!--
autowire属性 自动装配
byName 根据属性名称自动查找与之匹配的组件注入(如果没有此名称属性装配不了)
byType 根据属性类型自动查找与之匹配的组件注入(如果有多个同类型的组件装配不了)
constructor 根据构造方法自动查找与之匹配的主键注入(如果构造方法没有匹配的也装配不了)
default 默认的注入
-->
<!--
<bean name="userServiceImpl" class="com.web.service.impl.UserServiceImpl" autowire="default"></bean>
-->
<!--
scope属性 配置组件的作用域
singleton 单例模式
prototype 非单例模式
request 请求对象
session 会话对象
-->
<!-- lazy-init属性 延迟加载(为true时则在getBean时再实例化) -->
<!--
<bean name="userServiceImpl" class="com.web.service.impl.UserServiceImpl" scope="singleton" lazy-init="default">
<property name="userDAO" ref="userDAOImpl"></property>
</bean>
-->
</beans>
其中主要的部分为使用注解实现IOC,从com.web bean下去扫描注解,注解为@component表示此类受spring所管理(若该类不是自己所写,则无法用注解实现受spring容器所管理,但有其他办法可以让其间接受管理)
<context:component-scan base-package="com.web"/>
以及两种注入方式
<bean name="userServiceImpl" class="com.web.service.impl.UserServiceImpl">
<property name="userDAO" ref="userDAOImpl"></property>
</bean>
-->
<!-- 构造注入 -->
<!--
<bean name="userServiceImpl" class="com.web.service.impl.UserServiceImpl">
<constructor-arg ref="userDAOImpl"></constructor-arg>
</bean>
推荐用set注入,更加灵活