依赖注入(Dependency Injection):处理对象的依赖关系。下面就简单的介绍一下依赖注入的几种方式
通过构造函数
用IOC容器创建了一个对象,给对象的成员初始化
<bean id="user1" class="cn.hts2.User">
<constructor-arg index="1" type="java.lang.String" ref="str"></constructor-arg>
<constructor-arg index="0" type="int" value="100"></constructor-arg>
</bean>
User
package cn.hts2;
public class User {
private int id;
private String name;
public User(int id, String name) {
this.id = id;
this.name = name;
}
@Override
public String toString() {
return "User [id=" + id + ", name=" + name + "]";
}
}
通过set方法注入
通过成员的set方法进行注入
<?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.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<!-- 通过set方式进行依赖注入 -->
<bean id="userdao" class="cn.hts.set.UserDao"></bean>
<bean id="userservice" class="cn.hts.set.UserService">
<property name="userDao" ref="userdao"></property>
</bean>
<bean id="useraction" class="cn.hts.set.UserAction">
<property name="userService" ref="userservice"></property>
</bean>
</beans>
上面的配置还可以写成(内部bean)
<bean id="useraction" class="cn.hts.innerbean.UserAction">
<property name="userService">
<bean id="userservice" class="cn.hts.innerbean.UserService">
<property name="userDao">
<bean id="userdao" class="cn.hts.innerbean.UserDao"></bean>
</property>
</bean>
</property>
</bean>
UserDao
package cn.hts.set;
public class UserDao {
public void save() {
System.out.println("DB:保存用户");
}
}
UserService
package cn.hts.set;
public class UserService {
private UserDao userDao;
public void setUserDao(UserDao userDao) {
this.userDao = userDao;
}
public void save() {
userDao.save();
}
}
UserAction
package cn.hts.set;
public class UserAction {
// Service: springIOC容器注入
private UserService userService;
public void setUserService(UserService userService) {
this.userService = userService;
}
public String execute() {
userService.save();
return null;
}
}
P名称空间
通过p名称空间进行注入(适用于spring3.0以上版本)
<bean id="userdao" class="cn.hts.p.UserDao"></bean>
<bean id="userservice" class="cn.hts.p.UserService" p:userDao-ref="userdao"></bean>
<bean id="useraction" class="cn.hts.p.UserAction" p:userService-ref="userservice"></bean>
自动装配(不推荐使用,不好维护)
根据名称自动装配:autowire=”byName”
自动去IOC容器中找与属性名同名的引用的对象,并自动注入
<bean id="userDao" class="cn.hts.autowire.UserDao"></bean>
<bean id="userService" class="cn.hts.autowire.UserService" autowire="byName"></bean>
<bean id="useraction" class="cn.hts.autowire.UserAction" autowire="byName"></bean>
也可以定义到全局,这样就不用每个bean节点都去写autowire=”byName”
<?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"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd" default-autowire="byName">
<bean id="userDao" class="cn.hts.autowire.UserDao"></bean>
<bean id="userService" class="cn.hts.autowire.UserService"></bean>
<bean id="useraction" class="cn.hts.autowire.UserAction" ></bean>
</beans>
根据类型自动装配:autowire=”byType”
必须确保类型在IOC容器中只有一个对象,否则报错。
<?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.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd" default-autowire="byType">
<!-- ###############自动装配############### -->
<bean id="userDao" class="cn.hts.autowire.UserDao"></bean>
<bean id="userService" class="cn.hts.autowire.UserService" ></bean>
<bean id="useraction" class="cn.hts.autowire.UserAction" ></bean>
<!--报错: 因为上面已经有一个该类型的对象,且使用了根据类型自动装配 -->
<!-- <bean id="userService" class="cn.hts.autowire.UserService" ></bean> -->
</beans>
注解
注解方式可以简化spring的IOC容器的配置!
使用注解步骤:
1)先引入context名称空间
xmlns:context=”http://www.springframework.org/schema/context”
2)开启注解扫描
<context:component-scan base-package="cn.itcast.e_anno2"></context:component-scan>
3)使用注解
通过注解的方式,把对象加入ioc容器。
创建对象以及处理对象依赖关系,相关的注解:
@Component 指定把一个对象加入IOC容器
@Repository 作用同@Component;在持久层使用
@Service 作用同@Component;在业务逻辑层使用
@Controller 作用同@Component;在控制层使用
@Resource 属性注入
总结:
1) 使用注解,可以简化配置,且可以把对象加入IOC容器,及处理依赖关系(DI)
2) 注解可以和xml配合使用