目录
(1 )编写Dao接口及其实现类,使用恰当的注解将实现类标注为Bean组件。
(2) 编写业务接口及其实现类,使用恰当的注解将实现类标注为Bean组件。
(4 )编写Spring配置文件,使用注解配置信息启动Spring容器。
5. 使用.properties文件加载配置注入集合类型的属性
一、实验目的:
1. 掌握XML配置完成依赖注入
2. 掌握注解方式完成依赖注入
二、实验环境:
Intellij idea 2019
三、实验内容和要求:
1. 使用构造注入完成属性赋值
需求说明
说话人和说话内容都通过构造方法注入。
输出:
张嘎说:“三天不打小鬼子,手都痒痒!”
Rod说:“世界上有10种人,认识二进制的和不认识二进制的。”
1、DAO对象(Greeting类)
public Greeting(String person, String words) { this.person = person; this.words = words; }——须有 构造方法
2、配置文件(spring-config.xml)
<bean id="zhangGa" class="com.Greeting"> <constructor-arg name="person" value="张嘎"/> <constructor-arg name="words" value="三天不打小鬼子,手都痒痒!"/> </bean>
- com.Greeting 类 ,构造方法,有2个参数,所以有2个<constructor-arg>元素
- <constructor-arg name="person" value="张嘎"/>
表示 构造方法中的一个参数 person,通过构造方法赋值
<?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="zhangGa" class="com.Greeting">
<constructor-arg name="person" value="张嘎"/>
<constructor-arg name="words" value="三天不打小鬼子,手都痒痒!"/>
</bean>
<bean id="rod" class="com.Greeting">
<constructor-arg name="person" value="Rod"/>
<constructor-arg name="words" value="世界上有10种人,认识二进制的和不认识二进制的。"/>
</bean>
</beans>
3、测试类
结果:
2. 使用p命名空间注入直接量
需求说明
改造第1题的代码,说话人和说话内容使用p命名空间通过setter方法注入。
输出:
张嘎说:“三天不打小鬼子,手都痒痒!”
Rod说:“世界上有10种人,认识二进制的和不认识二进制的。”
1、DAO对象(Greeting类)
public void setPerson(String person) { this.person = person; } public void setWords(String words) { this.words = words; }——set 方法注入
2、配置文件(spring-config.xml)
<bean id="zhangGa" class="com.Greeting" p:person="张嘎" p:words="三天不打小鬼子,手都痒痒!"/>p:person="张嘎" p:words="三天不打小鬼子,手都痒痒!"
- p:属性1=【赋值】 p:属性2=【赋值】
<?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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="zhangGa" class="com.Greeting"
p:person="张嘎" p:words="三天不打小鬼子,手都痒痒!"/>
<bean id="rod" class="com.Greeting"
p:person="Rod" p:words="世界上有10种人,认识二进制的和不认识二进制的。"/>
</beans>
3、测试类
3. 使用p命名空间注入Bean组件
需求说明
改造示例1和示例2(教材中125-126页)的代码,使用p命名空间为业务Bean注入DAO对象。
1、实体
public void setPerson(String person) { this.person = person; } public void setWords(String words) { this.words = words; }——p空间注入,使用set 方法
2、数据访问层
- 接口类
——设计 数据访问层接口,声明所需方法
- 接口实现类
——设计 数据访问层接口实现类,完成具体持久化操作
3、业务层
- 业务接口类
- 业务实现类
4、spring 配置文件
<bean id="zhangGa" class="com.User" p:person="张嘎" p:words="三天不打小鬼子,手都痒痒!"/>——p注入,实体类的 bean
<bean id="zhangGa_greeting" class="com.GreetingDaoImpl" p:user-ref="zhangGa"/>——p注入,数据访问层实现类的 bean
- p:user-ref="zhangGa"
——p:某属性-ref="【赋值】"
<bean id="zhangGa_service" class="com.UserServiceImpl" p:greetingDao-ref="zhangGa_greeting"/>——p注入,业务实现类的 bean
<?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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<!-- p注入 【com.User 的 set方法】 Person & words-->
<bean id="zhangGa" class="com.User"
p:person="张嘎" p:words="三天不打小鬼子,手都痒痒!"/>
<bean id="rod" class="com.User"
p:person="Rod" p:words="世界上有10种人,认识二进制的和不认识二进制的。"/>
<bean id="zhangGa_greeting" class="com.GreetingDaoImpl"
p:user-ref="zhangGa"/>
<bean id="rod_greeting" class="com.GreetingDaoImpl"
p:user-ref="rod"/>
<bean id="zhangGa_service" class="com.UserServiceImpl"
p:greetingDao-ref="zhangGa_greeting"/>
<bean id="rod_service" class="com.UserServiceImpl"
p:greetingDao-ref="rod_greeting"/>
</beans>
5、实现类
import com.UserServiceImpl;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class test {
public static void main(String[] args){
ApplicationContext context=new ClassPathXmlApplicationContext("spring-config.xml");
UserServiceImpl userService1=(UserServiceImpl)context.getBean("zhangGa_service");
userService1.sayGreeting();
UserServiceImpl userService2=(UserServiceImpl)context.getBean("rod_service");
userService2.sayGreeting();
}
}
结果:
4. 使用注解实现依赖注入
需求说明
参照示例18至示例21(教材中136-138页),使用注解完成Bean的定义和装配。
参考实现步骤如下:
准备:实体类 User
(1 )编写Dao接口及其实现类,使用恰当的注解将实现类标注为Bean组件。
1、Dao接口
2、Dao实现
@Component("greetingDao")
等同于,xml 配置文件中
<bean id="greetingDao" class="com.GreetingDaoImpl"/>
(2) 编写业务接口及其实现类,使用恰当的注解将实现类标注为Bean组件。
1、业务接口
2、业务实现
@Service("userService")
等同于,xml 配置文件中
<bean id="userService" class="com.UserServiceImpl"/>
(3 )使用注解为业务Bean注入所依赖的Dao组件。
@Autowired
自动装配合适的依赖对象
@Autowired
@Qualifier("zhangGa")
不止一个合适的依赖对象,@Qualifier 指定所需 Bean 的名称
(4 )编写Spring配置文件,使用注解配置信息启动Spring容器。
<context:component-scan base-package="com" />
context 命名空间下,component-scan 标签扫描注解标注的类,
base-package 属性指定需要扫描的基准包。
(多个包名,可用,隔开) <context:component-scan base-package="com,wzu" />
<?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
https://www.springframework.org/schema/context/spring-context.xsd">
<context:component-scan base-package="com" />
<bean id="zhangGa" class="com.User"
p:person="张嘎" p:words="三天不打小鬼子,手都痒痒!"/>
<bean id="rod" class="com.User"
p:person="Rod" p:words="世界上有10种人,认识二进制的和不认识二进制的。"/>
</beans>
(5) 编写测试代码,运行代码以检验效果。
ApplicationContext context;
@Before
public void init(){
context=new ClassPathXmlApplicationContext("spring-config.xml");
}
@Test
public void test_zhangGa(){
UserServiceImpl userService1=
(UserServiceImpl)context.getBean("userService");
userService1.sayGreeting();
}
import com.UserServiceImpl;
import org.junit.Before;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class test {
ApplicationContext context;
@Before
public void init(){
context=new ClassPathXmlApplicationContext("spring-config.xml");
}
@Test
public void test_zhangGa(){
UserServiceImpl userService1=(UserServiceImpl)context.getBean("userService");
userService1.sayGreeting();
}
}
结果
5. 使用.properties文件加载配置注入集合类型的属性
改写示例10至示例13(教材中131-132页),使用.properties文件加载配置的方式完成List、Set、Map、Properties集合类型的属性值的注入。实现步骤参考课堂代码演示。
0、实体类 List、Set、Map、Properties
package com;
import java.util.List;
public class UserList {
private String person;
private List words;
public String getPerson() {
return person;
}
public void setPerson(String person) {
this.person = person;
}
public List getWords() {
return words;
}
public void setWords(List words) {
this.words = words;
}
}
package com;
import java.util.Set;
public class UserSet {
private String person;
private Set words;
public String getPerson() {
return person;
}
public void setPerson(String person) {
this.person = person;
}
public Set getWords() {
return words;
}
public void setWords(Set words) {
this.words = words;
}
}
package com;
import java.util.List;
import java.util.Map;
public class UserMap {
private String person;
private Map words;
public String getPerson() {
return person;
}
public void setPerson(String person) {
this.person = person;
}
public Map getWords() {
return words;
}
public void setWords(Map words) {
this.words = words;
}
}
package com;
import java.util.Properties;
public class UserProp {
private String person;
private Properties words;
public String getPerson() {
return person;
}
public void setPerson(String person) {
this.person = person;
}
public Properties getWords() {
return words;
}
public void setWords(Properties words) {
this.words = words;
}
}
1、数据访问层(接口+实现类)
package com;
public interface GreetingDao {
void sayListGreeting();
void saySetGreeting();
void sayMapGreeting();
void sayPropGreeting();
}
package com;
public class GreetingDaoImpl implements GreetingDao{
private UserList userList;
private UserSet userSet;
private UserMap userMap;
private UserProp userProp;
public void setUserList(UserList userList) {
this.userList = userList;
}
public void setUserSet(UserSet userSet) {
this.userSet = userSet;
}
public void setUserMap(UserMap userMap) {
this.userMap = userMap;
}
public void setUserProp(UserProp userProp) {
this.userProp = userProp;
}
public void sayListGreeting(){
System.out.println(userList.getPerson()+"说:”"+userList.getWords()+"“");
}
public void saySetGreeting(){
System.out.println(userSet.getPerson()+"说:”"+userSet.getWords()+"“");
}
public void sayMapGreeting(){
System.out.println(userMap.getPerson()+"说:”"+userMap.getWords()+"“");
}
public void sayPropGreeting(){
System.out.println(userProp.getPerson()+"说:”"+userProp.getWords()+"“");
}
}
2、业务层(接口+实现类)
package com;
public interface UserService {
void sayGreeting();
}
package com;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service("userService")
public class UserServiceImpl implements UserService{
@Autowired //
private GreetingDaoImpl greetingDao;
public UserServiceImpl() {
}
public void sayGreeting() {
// 调用用户DAO的方法来保存用户信息
greetingDao.sayListGreeting();
greetingDao.saySetGreeting();
greetingDao.sayMapGreeting();
greetingDao.sayPropGreeting();
}
}
3、配置文件
<?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
https://www.springframework.org/schema/context/spring-context.xsd">
<!-- 扫描包中注解标注的类 -->
<context:component-scan base-package="com" />
<bean id="list" class="com.UserList">
<property name="person">
<value>张嘎</value>
</property>
<property name="words">
<list>
<value>List:123</value>
<value>List:abc</value>
</list>
</property>
</bean>
<bean id="set" class="com.UserSet">
<property name="person">
<value>Set</value>
</property>
<property name="words">
<set>
<value>足球</value>
<value>篮球</value>
</set>
</property>
</bean>
<bean id="map" class="com.UserMap">
<property name="person">
<value>Map</value>
</property>
<property name="words">
<map>
<entry>
<key><value>football</value></key>
<value>足球</value>
</entry>
<entry>
<key><value>basketball</value></key>
<value>篮球</value>
</entry>
</map>
</property>
</bean>
<bean id="properties" class="com.UserProp">
<property name="person">
<value>Set</value>
</property>
<property name="words">
<props>
<!-- 定义Properties中的键值对 -->
<prop key="football">足球</prop>
<prop key="basketball">篮球</prop>
</props>
</property>
</bean>
<bean id="greetingDao" class="com.GreetingDaoImpl"
p:userList-ref="list" p:userSet-ref="set"
p:userMap-ref="map" p:userProp-ref="properties"/>
</beans>
4、测试类
import com.UserServiceImpl;
import org.junit.Before;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class test {
ApplicationContext context;
@Before
public void init(){
context=new ClassPathXmlApplicationContext("spring-config.xml");
}
@Test
public void test(){
UserServiceImpl userService1=(UserServiceImpl)context.getBean("userService");
userService1.sayGreeting();
}
}
总结
(1)构造方法注入
1、类,要有构造方法
2、xml中 配置 bean
<bean id="zhangGa" class="com.Greeting">
<constructor-arg name="person" value="张嘎"/>
<constructor-arg name="words" value="三天不打小鬼子,手都痒痒!"/>
</bean>
3、执行
ApplicationContext context = new ClassPathXmlApplicationContext("spring-config.xml");
UserServiceImpl userService1=(UserServiceImpl)context.getBean("userService");
userService1.sayGreeting();
(2)p注入