spring依赖注入的6种实现方式分别是:set方法注入,内部bean,p命名空间,自动装配,注解,接口注入。本文我们通过举例的方式,把依赖注入的6种实现方式介绍给读者。
spring中如何给对象的属性复制?
1)通过构造方法
2)通过set方法给属性注入值
3)p命名空间
4)自动转配(了解即可,不推荐使用)
5)注解
6)通过接口
准备工作(模拟业务方法)Action-->service-->dao
1)UserDao:
<pre class="brush:html;toolbar:false"> p<span style="font-family:Courier New;">ublic class UserDao { public void save(){ System.out.println("save方法"); } }</span></pre>
2)UserService:
<pre class="brush:html;toolbar:false"> p<span style="font-family:Courier New;">ublic class UserService { private UserDao userDao; public void setUserDao(UserDao userDao) { this.userDao = userDao; } public void save(){ userDao.save(); } }</span></pre>
3)UserAction:
<pre class="brush:html;toolbar:false"> <span style="font-family:Courier New;">public class UserAction { private UserService userService; public void setUserService(UserService userService) { this.userService = userService; } public String execute(){ userService.save(); return null; } }</span></pre>
4)App测试类:
<pre class="brush:html;toolbar:false"> public class App { ApplicationContext ac=new ClassPathXmlApplicationContext("cn/itcast/property/bean.xml"); @Test public void test(){ UserAction userAction=(UserAction) ac.getBean("userAction"); System.out.println(userAction.execute()); } }</pre>
5)bean.xml的约束
<pre class="brush:xml;toolbar:false"> <?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"> </beans></pre>
方式一.set方法注入
1)必须保证有set方法
<pre class="brush:html;toolbar:false;"> <bean id="userDao" class="cn.itcast.property.UserDao"></bean> <bean id="userService" class="cn.itcast.property.UserService"> <property name="userdao" ref="userDao"></property> </bean> <bean id="userAction" class="cn.itcast.property.UserAction"> <property name="userService" ref="userService"></property> </bean></pre>
方式二.内部bean(因为是内部bean 因此如果还有一个action 就不能引用里面的service方法)
<pre class="brush:html;toolbar:false"> bean id="userAction" class="cn.itcast.property.UserAction"> <property name="userService"> <bean id="userService" class="cn.itcast.property.UserService"> <property name="userDao"> <bean id="userDao" class="cn.itcast.property.UserDao"></bean> </property> </bean> </property> </bean></pre>
方式三.p命名空间
<pre class="brush:html;toolbar:false"> <bean id="userDao" class="cn.itcast.property.UserDao"></bean> <bean id="userService" class="cn.itcast.property.UserService" p:userdao-ref="userDao"></bean> <bean id="userAction" class="cn.itcast.property.UserAction" p:userService-ref="userService"></bean></pre>
方式四:自动装配(不推荐使用)
<!-- 根据“名称”自动装配: userAction注入的属性,会去ioc容器中自动查找与属性同名的对象 -->
<pre class="brush:html;toolbar:false"> <bean id="userDao" class="cn.itcast.property.UserDao" ></bean> <bean id="userService" class="cn.itcast.property.UserService" autowire="byName"></bean> <bean id="userAction" class="cn.itcast.property.UserAction" autowire="byName"></bean></pre>
方式五:注解
使用注解步骤:
1)先引入context名称空间
xmlns:context="http://www.springframework.org/schema/context"
2)开启注解扫描
<context:component-scanbase-package="cn.itcast.e_anno2"></context:component-scan>
3)使用注解
通过注解的方式,把对象加入ioc容器。
创建对象以及处理对象依赖关系,相关的注解:
@Component 指定把一个对象加入IOC容器
@Repository 作用同@Component; 在持久层使用
@Service 作用同@Component; 在业务逻辑层使用
@Controller 作用同@Component; 在控制层使用
@Resource 属性注入
配置文件中开启扫描机制
<pre class="brush:xml;toolbar:false;"> <!-- 开启扫描机制 --> <context:component-scan base-package="cn.itcast.property"></context:component-scan></pre>
UserDao使用注解(使用注解就不用set方法了)
<pre class="brush:java;toolbar:false;"> @Repository public class UserDao { public void save(){ System.out.println("save方法"); } }</pre>
UserService使用注解:
<pre class="brush:java;toolbar:false"> @Service public class UserService { @Resource private UserDao userDao; public void save(){ userDao.save(); } }</pre>
UserAction使用注解
<pre class="brush:java;toolbar:false"> @Controller public class UserAction { @Resource private UserService userService; public String execute(){ userService.save(); return null; } }</pre>
方式:接口方式(interface injection)
接口注入指的就是在接口中定义要注入的信息,并通过接口完成注入。结合前面的示例,其具体步骤如下。
(1)编写一个接口IBusiness,各种数据库的注入将通过这个接口进行。IBusiness.java的示例代码如下:
//******* IBusiness.java**************
}
(2)任何想要使用数据库实例的类都必须实现这个接口,业务逻辑类Business实现这个接口IBusiness。Business.java的示例代码如下:
//******* Business.java**************
this.db = db;
}
……
//根据注入的数据库类,从×××数据库中获取数据
public void getData() {
……
db.getData();
……
}
}
(3)编写测试类TestBusiness。TestBusiness.java的示例代码如下:
//******* TestBusiness.java**************
public class TestBusiness {
private Business business = new Business();
……
//根据注入的数据库类,从Oracle数据库中获取数据
public void getData() {
……
business. createDI (new OracleDataBase());
business.getData();
……
}
}
如果要完成依赖关系注入的对象,必须实现IBusiness接口。
您可能感兴趣的文章:
Spring 学习笔记--强烈推荐
Spring开源框架松耦合示例
Xml解析校验引起的依赖问题
快速部署 Spring PetClinic 到函数计算平台
找不到 org.junit.jupiter.api.Test报错
php为什么要用依赖注入?
关于PHP中依赖注入的详细介绍
java框架spring依赖注入的6种方式
springboot和mybatis两个框架结合教程
Springboot 系列(二)Spring Boot 配置文件全解析