今天在开发的过程中,突然对Spring的依赖注入几种方式出现混交,打算做个简单的小结,如有总结不对的地方,请大家不吝指教!
在Spring中依赖注入有四种方式:set注入(通常也叫属性注入),构造函数注入,接口注入(这个现在基本不用),注解注入(@Autowire),下面对set方法注入,构造函数注入,以及注解注入的用法举例说明。
1、set方法注入(属性注入)
UserDao.java
public class UserDao{
public void inserUser(User user){
//具体逻辑省略
}
}
UserService.java
public Interface UserService{
void inserUser(User user);
}
UserServiceImpl.java
public class UserServiceImpl implements UserService{
private UserDao userDao;
public void setUserDao(UserDao userDao){
this.userDao = userDao;
}
public void insertUser(User user){
userDao.insert(user);
}
}
Spring配置文件
<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-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd ">
<bean id="userDao" class="UserDao">bean>
<bean id="userService" class="UserServiceImpl">
<property name="userDao" ref="userDao">
bean>
beans>
以上就可以把userDao注入到UserServiceImpl中
2、构造函数注入
User.java
public class User{
//为了简便,就写两个属性
private String name;
private Integer age;
//关于name,age的getter/setter方法省略
public User(String name,int age){
this.name = name;
this.age = age;
}
}
现在通过Spring配置文件来注入这User这个对象
<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-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd ">
<bean id="user" class="User">
<constructor-arg type="java.lang.String" index="0" value="zhangsan"/>
<constructor-arg type="java.lang.Integer" index="1" value="20>
以上就可以将属性值注入到User对象中。当然如果想以set方法注入的例子话,其实也一样。就是在UserServiceImpl需要构造函数,形参就是userDao了,就不需要setter方法了。那么关于其Spring的配置文件的书写应该改成这样
<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-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd ">
<bean id="userDao" class="UserDao">bean>
<bean id="userService class="UserServiceImpl">
<constructor-arg index="0" ref="userDao">
bean>
beans>
这样也能实现上面set方法注入达到的效果
3、注解注入
我没试过在这些类上不加@Component,@Service,@Controller等直接@Autowire,是否能成功注入,不过养成一个比较好的编程习惯,建议在相应的层次加上相应的注解。如下例子中UserDao属于Modul层,在Spring中可以用@Component注解
UserDao.java
@Component
public class UserDao{
public void inserUser(User user){
//具体逻辑省略
}
}
UserService.java
public Interface UserService{
void inserUser(User user);
}
UserServiceImpl.java
//引入相应的Spring包
@Service
public class UserServiceImpl implements UserService{
@Autowire
private UserDao userDao;
public void insertUser(User user){
userDao.insert(user);
}
}
Spring对应的配置文件
<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-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd ">
<context:component-scan base-package="*">
context:component-scan>
<context:annotation-config>
context:annotation-config>
<bean id="userDao" class="UserDao">bean>
<bean id="userService" class="UserServiceImpl"/>
beans>
不过在开发过程中既开启了set方法注入,有开启了注解注入的话,Spring会首选set注入的,所以不忘了提供相应的set方法的,否则会出现失败。
通过比较,注解注入要比其余注入要方便的多,代码和配置文件也写的少,在开发的过程中还是建议使用注解注入。