原始基本数据类型注入:
- <?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-3.1.xsd">
- <!--bean元素声明需要Spring创建的实例, id为实例对象的名称,class为指定实例的类型 -->
- <bean id="helloSpring" class="cn.lilin.helloSpring.HelloSpring" p:who="Spring"></bean>
- <!-- property为实例的属性赋值,name为属性名,此处实际是调用set方法 -->
- <property name="who">
- <value>Spring</value>
- </property>
- </beans>
原始引用数据类型注入:
- <?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-3.1.xsd">
- <!--bean元素声明需要Spring创建的实例, id为实例对象的名称,class为指定实例的类型 -->
- <bean id="userDaoImpl" class="cn.lilin.dao.user.impl.UserDaoImpl"></bean>
- <bean id="userServiceImpl" class="cn.lilin.service.user.impl.UserServiceImpl"
- <!-- property为实例的属性赋值,name为属性名,此处实际是调用set方法-->
- <property name="dao" ref="userDaoImpl"></property>
- </bean>
- <bean id="userServiceImpl" class="cn.lilin.service.user.impl.UserServiceImpl"
- <!-- 内部Bean-->
- <property name="dao">
- <bean class="cn.lilin.service.user.impl.UserServiceImpl"></bean>
- </property>
- </bean>
集合属性和null、空字符串的注入:
- <bean id="userServiceImpl" class="cn.lilin.service.user.impl.UserServiceImpl"
- <property name="dao">
- <list>
- <value>111</value>
- <value>222</value>
- <value>333</value>
- </list>
- <set>
- <value>111</value>
- <value>222</value>
- <value>333</value>
- </set>
- <map>
- <entry>
- <key><value>key1</value></key>
- <value>111</value>
- </entry>
- <entry>
- <key><value>key1</value></key>
- <value>222</value>
- </entry>
- </map>
- <!-- 如果map中的的键或值是Bean对象,可以将<value>换成<ref> -->
- <props>
- <prop key="key1">111</prop>
- <prop key="key2">222</prop>
- <prop key="key3">222</prop>
- </props>
- <!-- key和value通常是字符串类型 -->
- <!-- 注入null -->
- <property name="dao"><null/></property>
- <!-- 注入空字符串 -->
- <property name="dao"><value></value></property>
- </property>
- </bean>
p命名空间注入普通数据类型:
- <?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"
- xsi:schemaLocation="http://www.springframework.org/schema/beans
- http://www.springframework.org/schema/beans/spring-beans-3.1.xsd">
- <!-- p命名空间简化属性的赋值,引用类型用:p:属性名-ref="Bean的id" -->
- <bean id="helloSpring" class="cn.lilin.helloSpring.HelloSpring" p:who="Spring"></bean>
- </beans>
p命名空间注入引用数据类型:
- <?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"
- xsi:schemaLocation="http://www.springframework.org/schema/beans
- http://www.springframework.org/schema/beans/spring-beans-3.1.xsd">
- <!-- p命名空间简化属性的赋值,引用类型用:p:属性名-ref="Bean的id" -->
- <bean id="userDaoImpl" class="cn.lilin.dao.user.impl.UserDaoImpl"></bean>
- <bean id="userServiceImpl" class="cn.lilin.service.user.impl.UserServiceImpl" p:dao-ref="userDaoImpl"></bean>
- </beans>
使用注解实现Ioc:
@Repository("userDao")//用于标注DAO类
@Service("userService")//用于标注业务类
@Controller//用于标注控制器类
@Scope("..")//设置Bean作用域
@Autowired//spring注入,容器会查找与属性类型相匹配的Bean组件,类中可以省略属性相关的setter方法,@Autowired(required=false)标识依赖不是必须的,找不到Bean组件也不会抛异常,默认为true
@Qualifier("userDao")//指定所需Bean的名称
private UserMapper userMapper;//声明UserMapper接口引用
Java标准注解:
@Resource(name="userDao")//指定所需Bean的名称,如果没有指定name,将根据属性名或者setter方法名,找不到再按照类型匹配的方式进行装配
private UserMapper userMapper;//声明UserMapper接口引用
Spring配置文件信息
<?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:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.1.xsd">
<!-- 扫描包中用注解标注的类 -->
<context:component-scan base-package="cn.mySmbms.dao,cn.mySmbms.utils"></context:component-scan>