spring基于构造方法的依赖注入
2017-12-13 16:42
645 查看
一 构造方法注入bean
1. 创建HelloService接口
2. HelloServiceImpl实现类
3. SelfIntroductionService(自我介绍)接口
4. SelfIntroductionServiceImpl实现类
5. spring关于构造器注入Bean的xml配置applicationContext-Constructor.xml
6. 测试构造器注入bean
7. 运行结果
hello:大家好!
二 构造方法注入字面量
1. 创建一个实体类
2. spring关于构造器注入字面量的xml配置applicationContext-Constructor.xml
3. 测试构造器注入字面量
4. 运行结果
姓名: tom 性别: 男 年龄: 26 兴趣爱好: 爱好女人,哈哈!开个玩笑!
1. 创建HelloService接口
package com.lanhuigu.spring.common; public interface HelloService { void sayHello(String name); }
2. HelloServiceImpl实现类
package com.lanhuigu.spring.common; public class HelloServiceImpl implements HelloService{ @Override public void sayHello(String name) { System.out.println("hello:" + name); } }
3. SelfIntroductionService(自我介绍)接口
package com.lanhuigu.spring.constructor; public interface SelfIntroductionService { void selfIntroduction(); }
4. SelfIntroductionServiceImpl实现类
package com.lanhuigu.spring.constructor; import com.lanhuigu.spring.common.HelloService; public class SelfIntroductionServiceImpl implements SelfIntroductionService{ private HelloService helloService; // 构造器注入Bean public SelfIntroductionServiceImpl(HelloService helloService) { this.helloService = helloService; } @Override public void selfIntroduction() { // 向大家打招呼 helloService.sayHello("大家好!"); } }
5. spring关于构造器注入Bean的xml配置applicationContext-Constructor.xml
<?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声明: 该bean类似于javaConfig中的@Bean注解; 用于创建bean的类通过class属性来指定,并且需要使用全限定的类名。 通过id指定bean的ID。如果不显示指定,默认使用class的全限定名进行命名。 eg: com.lanhuigu.spring.common.HelloServiceImpl#0,其#0是一个计数器的形式, 用来区分相同类型的其他bean。 使用自动化命名很方便,但是没有多少实际用处,还是建议自己给bean显示设定ID。 --> <bean id="helloService" class="com.lanhuigu.spring.common.HelloServiceImpl"/> <!-- 构造器注入bean --> <bean id="selfIntroductionService" class="com.lanhuigu.spring.constructor.SelfIntroductionServiceImpl"> <!-- 构造器注入bean --> <constructor-arg ref="helloService"/> </bean> </beans>
6. 测试构造器注入bean
package com.lanhuigu.spring; import com.lanhuigu.spring.constructor.SelfIntroductionService; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; /** * 测试构造器注入Bean */ public class TestHelloConstructor { @Test public void testHello() { // 根据spring配置文件创建应用上下文 ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext-Constructor.xml"); // 从容器中获取bean SelfIntroductionService selfIntroductionService = (SelfIntroductionService)context.getBean("selfIntroductionService"); // 调用自我介绍 selfIntroductionService.selfIntroduction(); } }
7. 运行结果
hello:大家好!
二 构造方法注入字面量
1. 创建一个实体类
package com.lanhuigu.spring.entity; /** * 个人信息 */ public class Person { /** 姓名 */ private String name; /** 性别 */ private String sex; /** 年龄 */ private int age; /** 兴趣爱好 */ private String hobby; /** * 构造器注入时打开构造器 * @param name * @param sex * @param age * @param hobby */ public Person(String name, String sex, int age, String hobby) { this.name = name; this.sex = sex; this.age = age; this.hobby = hobby; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public String getHobby() { return hobby; } public void setHobby(String hobby) { this.hobby = hobby; } }
2. spring关于构造器注入字面量的xml配置applicationContext-Constructor.xml
<?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="person" class="com.lanhuigu.spring.entity.Person"> <!-- 第一种方式: 根据参数索引位置匹配 --> <constructor-arg index="0" value="tom"/> <constructor-arg index="1" value="男"/> <constructor-arg index="2" value="26"/> <constructor-arg index="3" value="爱好女人,哈哈!开个玩笑!"/> <!-- 第二种方式: 根据参数类型匹配 --> <!--<constructor-arg value="tom" type="java.lang.String"/> <constructor-arg value="男" type="java.lang.String"/> <constructor-arg value="26" type="int"/> <constructor-arg value="爱好女人,哈哈!开个玩笑!" type="java.lang.String"/>--> </bean> </beans>
3. 测试构造器注入字面量
package com.lanhuigu.spring; import com.lanhuigu.spring.entity.Person; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; /** * 测试构造器注入字面量 */ public class TestPersonConstructor { @Test public void testPerson() { // 根据spring配置文件创建应用上下文 ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext-Constructor.xml"); // 从容器中获取bean Person person = (Person) context.getBean("person"); // 打印个人属性 System.out.println(" 姓名: " + person.getName() + " 性别: " + person.getSex() + " 年龄: " + person.getAge() + " 兴趣爱好: " + person.getHobby()); } }
4. 运行结果
姓名: tom 性别: 男 年龄: 26 兴趣爱好: 爱好女人,哈哈!开个玩笑!
相关文章推荐
- Spring - 基于构造方法的依赖注入
- Spring的两种依赖注入方式:setter方法注入与构造方法注入
- Spring通过构造方法依赖注入
- Spring-Context之六:基于Setter方法进行依赖注入
- Spring - 基于 Setter 方法的依赖注入
- (spring-第3回【IoC基础篇】)spring的依赖注入-属性、构造函数、工厂方法等的注入(基于XML)
- Spring(六)属性依赖注入(构造方法,setter方法,p命名空间,spel表达式,集合注入)
- Spring的两种依赖注入方式:setter方法注入与构造方法注入
- Spring基于xml的构造方法注入&setter注入
- Spring的两种依赖注入方式:setter方法注入与构造方法注入 以及两种方式的区别!
- Spring基于xml的构造方法注入&amp;amp;setter注入
- Spring依赖注入的三种方式详解之二:构造方法注入
- Spring中基于setter方法的依赖注入
- Spring依赖注入的三种方式详解之二:构造方法注入
- Spring通过构造方法依赖注入
- spring基于setter方法的依赖注入
- [优质例子存档]Spring的两种依赖注入方式:setter方法注入与构造方法注入
- Spring依赖注入的三种方式详解之三:工厂方法注入
- 详解 Spring 3.0 基于 Annotation 的依赖注入实现
- spring学习(一)spring依赖注入四种底层方法