Spring基础:快速入门spring(3):构造注入
2016-11-26 18:41
495 查看
spring可以使用构造注入/setter方法/java注解等,本文通过实例来学习一下构造注入的方法。
定义注入依赖的interface
package com.liumiao.demo.spring; public interface TeachingService { public String provideTeachingService(); }
定义注入依赖的类
package com.liumiao.demo.spring; public class SwimmingTeachingService implements TeachingService { @Override public String provideTeachingService(){ return "I provide service of teaching swimming."; } }
Person interface
package com.liumiao.demo.spring; public interface Person { public String sayhello(); public String provideTeachingService(); }
修改构造函数
package com.liumiao.demo.spring; public class Teacher implements Person{ private TeachingService service; public Teacher(TeachingService theService){ this.service=theService; } public String sayhello(){ return "Hello, I am a teacher"; } @Override public String provideTeachingService(){ return service.provideTeachingService(); } }
配置spring设定文件
如下修改配置文件,唯一需要注意的是ref和id的一致<?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="thePerson" class="com.liumiao.demo.spring.Teacher"> <constructor-arg ref="theService"></constructor-arg> </bean> <bean id="theService" class="com.liumiao.demo.spring.SwimmingTeachingService"> </bean> </beans>
修改TestDemo
package com.liumiao.demo.spring; import com.liumiao.demo.spring.Person; import org.springframework.context.support.ClassPathXmlApplicationContext; public class TestDemo { public TestDemo() { } public static void main(String[] args) { ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("classpath:com/liumiao/demo/spring/spring-cfg.xml"); Person person = (Person)context.getBean("thePerson", Person.class); System.out.println(person.sayhello()); System.out.print(person.provideTeachingService()); context.close(); } }
执行结果
十一月 26, 2016 6:39:09 下午 org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh 信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@25f38edc: startup date [Sat Nov 26 18:39:09 CST 2016]; root of context hierarchy 十一月 26, 2016 6:39:09 下午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions 信息: Loading XML bean definitions from class path resource [com/liumiao/demo/spring/spring-cfg.xml] 十一月 26, 2016 6:39:09 下午 org.springframework.context.support.ClassPathXmlApplicationContext doClose 信息: Closing org.springframework.context.support.ClassPathXmlApplicationContext@25f38edc: startup date [Sat Nov 26 18:39:09 CST 2016]; root of context hierarchy Hello, I am a teacher I provide service of teaching swimming.
相关文章推荐
- Spring基础:快速入门spring(9):注解注入
- Spring基础:快速入门spring(4):setter注入
- Spring基础:快速入门spring(10):Java配置注入
- Spring基础:快速入门spring(5):值注入
- Spring基础:快速入门spring boot(5):Spring中的Cron--Scheduling
- spring 学习(1.1) 入门基础- IoC/依赖注入
- Spring Boot 基础知识学习(一)——快速入门
- Spring基础:快速入门spring(8):bean scope
- Spring入门_03_构造注入
- Spring基础:快速入门spring cloud(5):断路器之Hystrix
- Spring基础:快速入门spring boot(1):CLI方式的Helloworld
- 注入参数spring入门(7)---装配Bean中构造参数的注入
- Spring入门_03_构造注入
- Spring基础:快速入门spring cloud(2):服务发现之eureka
- spring基础:快速入门spring cloud(6):统一配置管理之spring cloud config
- Spring基础:快速入门spring boot(4):使用slf4j输出日志
- Spring基础:快速入门spring(1):基础概念
- Spring自动注入,利用注解实现spring基本配置详解,Spring注解快速入门
- Spring基础:快速入门spring(6):properties文件方式的值注入
- Spring基础:快速入门spring(7):bean的lifecyle