您的位置:首页 > 编程语言 > Java开发

spring基于构造方法的依赖注入

2017-12-13 16:42 645 查看
一 构造方法注入bean

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 兴趣爱好: 爱好女人,哈哈!开个玩笑!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: