Spring:IoC 用法(三、XML配置用法)
来源:互联网 发布:有没有抢购软件 编辑:程序博客网 时间:2024/06/06 07:11
XML配置注入
分为两种注入方式:构造注入、方法注入
1、构造注入
1)、定义Bean
模拟工作中的流程:
定义 Controller 控制层
public class TestController { // 被注入对象 private TestService testService; // 测试注入方法 public String testController() { return testService.testService(); } // -------------------------------------------------- // 构造注入 public TestController(TestService testService) { this.testService = testService; }}
定义 Service 服务层接口
public interface TestService { // 测试注入:调用方法 String testService();}
定义 Service 服务层接口实现
// 接口实现类public class TestServiceImpl implements TestService { // 被注入对象 private TestRepository repository; // 测试注入方法 public String testService() { return repository.testRepository(); } // -------------------------------------------------- // 构造注入 public TestServiceImpl(TestRepository repository) { this.repository = repository; }}
定义 Repository 数据层接口
public interface TestRepository { // 测试注入:调用方法 String testRepository();}
定义 Repository 数据层接口实现
// 接口实现类public class TestRepositoryImpl implements TestRepository { // 测试注入方法 public String testRepository() { return "------------------------- >>> 获取数据成功!!"; }}
2)、注入Bean
XML配置文件注入:属性 constructor-arg
<?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.xsd"> <bean id="controller" class="test.define.controller.TestController"> <constructor-arg ref="serviceId"/> </bean> <bean id="serviceId" class="test.define.service.impl.TestServiceImpl"> <constructor-arg ref="repositoryImpl"/> </bean> <bean id="repositoryImpl" class="test.define.repository.impl.TestRepositoryImpl"/></beans>
3)、完整测试代码
依赖
<properties><spring.version>4.2.1.RELEASE</spring.version></properties><dependencies><!-- Spring 依赖注入 --><dependency><groupId>org.springframework</groupId><artifactId>spring-context</artifactId><version>${spring.version}</version></dependency><!-- 仅测试 --><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>4.12</version></dependency></dependencies>测试类
public class Test { @org.junit.Test public void test() { ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("spring/spring.xml"); TestController controller = context.getBean(TestController.class); String data = controller.testController(); System.out.println("结果:" + data); context.close(); }}结果
结果:------------------------- >>> 获取数据成功!!
2、方法注入
1)、定义Bean
模拟工作中的流程:
定义 Controller 控制层
public class TestController { // 被注入对象 private TestService testService; // 测试注入方法 public String testController() { return testService.testService(); } // -------------------------------------------------- // 方法注入 public void setTestService(TestService testService) { this.testService = testService; }}
定义 Service 服务层接口
public interface TestService { // 测试注入:调用方法 String testService();}
定义 Service 服务层接口实现
// 接口实现类public class TestServiceImpl implements TestService { // 被注入对象 private TestRepository repository; // 测试注入方法 public String testService() { return repository.testRepository(); } // -------------------------------------------------- // 方法注入 public void setRepository(TestRepository repository) { this.repository = repository; }}
定义 Repository 数据层接口
public interface TestRepository { // 测试注入:调用方法 String testRepository();}
定义 Repository 数据层接口实现
// 接口实现类public class TestRepositoryImpl implements TestRepository { // 测试注入方法 public String testRepository() { return "------------------------- >>> 获取数据成功!!"; }}
2)、注入Bean
XML配置文件注入:属性 property
<?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.xsd"> <bean id="controller" class="test.define.controller.TestController"> <property name="testService" ref="serviceId"/> </bean> <bean id="serviceId" class="test.define.service.impl.TestServiceImpl"> <property name="repository" ref="repositoryImpl"/> </bean> <bean id="repositoryImpl" class="test.define.repository.impl.TestRepositoryImpl"/></beans>
3)、完整测试代码
依赖(不变)
<properties><spring.version>4.2.1.RELEASE</spring.version></properties><dependencies><!-- Spring 依赖注入 --><dependency><groupId>org.springframework</groupId><artifactId>spring-context</artifactId><version>${spring.version}</version></dependency><!-- 仅测试 --><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>4.12</version></dependency></dependencies>
测试类(不变)
public class Test { @org.junit.Test public void test() { ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("spring/spring.xml"); TestController controller = context.getBean(TestController.class); String data = controller.testController(); System.out.println("结果:" + data); context.close(); }}结果(不变)
结果:------------------------- >>> 获取数据成功!!
阅读全文
0 0
- Spring:IoC 用法(三、XML配置用法)
- Spring:IoC 用法(四、Java配置用法)
- Spring:IoC 用法(五、混合配置用法)
- Spring:IoC 用法(七、<beans>用法)
- Spring:IoC 用法(九、@Bean 用法)
- Spring:IoC 用法(八、<bean>用法)
- Spring的IOC用法
- Java Spring IOC用法
- Spring IOC详细用法
- Java Spring IOC用法
- Spring:IoC 用法(一、大纲)
- Spring:IoC 用法(二、自动注入用法)
- Spring:IoC 用法(六、自动扫描Bean用法)
- Spring之IOC基本用法
- Spring(三):IoC容器装配Bean(xml配置方式和注解方式)
- 基于XML -- Spring IOC配置
- Spring IOC之XML配置
- IOC容器的基本用法三
- SVN服务器搭建
- 救救服务器!IT人三伏天都怎么过?
- SwipeRefreshLayout + RecyclerView 实现上拉加载下拉刷新
- ios蓝牙开发(二)ios连接外设的代码实现
- 数据库事物
- Spring:IoC 用法(三、XML配置用法)
- 插入排序
- 在Spring中注入配置文件
- mybatis 处理结果集对象,对象包含对象[{a,b,c:[{}]}]
- 【HDU 1421】搬寝室
- Vue2 移动端开发环境搭建
- Unity3D项目程序加密2
- hibernate的表的映射和hql--第三天
- note____js