方式一:Set注入
UserDao:
package com.spring.demo02; public class UserDao { public void save() { System.out.println("持久层。。。"); } }
UserService:
package com.spring.demo02; public class UserService { //配置文件注入:提供成员属性,设置set方法 private UserDao userDao; public void setUserDao(UserDao userDao) { this.userDao = userDao; } public void save() { System.out.println("业务层。。。"); //调用 userDao.save(); } }
配置文件设置依赖注入:
<?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="userDao" class="com.spring.demo02.UserDao"/> <bean id="userService" class="com.spring.demo02.UserService"> <property name="userDao" ref="userDao"/> </bean> </beans>
测试依赖注入:
package com.spring.demo02; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class TestDI { @Test public void run() { //创建容器对象,加载配置文件,创建userDao对象,userService也被创建 ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml"); //获取userService对象 UserService userService = (UserService) context.getBean("userService"); //调用save方法 userService.save(); } }
运行结果:
方式二:构造函数注入
User:
package com.spring.demo03; public class User { private String name; private Integer age; public User(String name, Integer age) { this.name = name; this.age = age; } @Override public String toString() { return "User [name=" + name + ", age=" + age + "]"; } }
配置文件设置依赖注入:
<?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"> <!-- 方式一:Set注入 --> <bean id="userDao" class="com.spring.demo02.UserDao"/> <bean id="userService" class="com.spring.demo02.UserService"> <property name="userDao" ref="userDao"/> </bean> <!-- 方式二:构造方法的注入方式:name是属性名称,value是注入的值,如果要出入引用使用raf标签 --> <bean id="user" class="com.spring.demo03.User"> <constructor-arg name="name" value="Tom"/> <constructor-arg name="age" value="18"/> <!-- 第二种方式 index写序号就行 --> <!-- <constructor-arg index="0" value="Tom"/> <constructor-arg name="1" value="18"/> --> </bean>
测试:
package com.spring.demo03; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class TestDI { @Test public void run() { //创建容器对象,加载配置文件,创建User类的对象,并构造函数初始化 ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml"); //获取user对象 User user = (User) context.getBean("user"); System.out.println(user); } }
运行结果:
方式三:p命名空间注入方式
User:
package com.spring.demo03; public class User { private String name; private Integer age; public void setName(String name) { this.name = name; } public void setAge(Integer age) { this.age = age; } @Override public String toString() { return "User [name=" + name + ", age=" + age + "]"; } }
配置文件设置依赖注入:
<?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命名空间注入方式 --> <bean id="user" class="com.spring.demo03.User" p:name="Tom" p:age="18" /> </beans>
配置文件中需要设置命名空间p
测试:
package com.spring.demo03; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class TestDI { @Test public void run() { //创建容器对象,加载配置文件,创建User类的对象,并构造函数初始化 ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml"); //获取user对象 User user = (User) context.getBean("user"); System.out.println(user); } }
运行结果;
方式四:集合方式
原文地址:https://www.cnblogs.com/jumpkin1122/p/11623374.html
时间: 2024-10-10 10:07:44