package com.taotao.service;
/**
* create by 刘鸿涛
* 2022/4/5 17:53
*/
public interface UserService {
public void save();
}
package com.taotao.service.impl;
import com.taotao.dao.UserDao;
import com.taotao.service.UserService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
* create by 刘鸿涛
* 2022/4/5 17:55
*/
public class UserServiceImpl implements UserService {
public void save() {
ApplicationContext app = new ClassPathXmlApplicationContext("applicationContext.xml");
//向下转型为UserDao类型
UserDao userDao = (UserDao) app.getBean("userDao");
userDao.save();
}
}
package com.taotao.demo;
import com.taotao.service.impl.UserServiceImpl;
/**
* create by 刘鸿涛
* 2022/4/5 17:59
*/
public class UserController {
public static void main(String[] args) {
UserServiceImpl userService = new UserServiceImpl();
userService.save();
}
}
package com.taotao.demo;
import com.taotao.service.UserService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
* create by 刘鸿涛
* 2022/4/5 17:59
*/
public class UserController {
public static void main(String[] args) {
ApplicationContext app = new ClassPathXmlApplicationContext("applicationContext.xml");
UserService userService = (UserService) app.getBean("userService");
userService.save();
}
}
把userService配到容器中
<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="userDao" class="com.taotao.dao.impl.UserDaoImpl">bean>
<bean id="userService" class="com.taotao.service.impl.UserServiceImpl">bean>
beans>
依赖注入(Dependency Injection):他是Spring框架核心IOC的具体实现
在编写程序时,通过控制反转,把对象的创建交给了Spring,但是代码中不可能出现没有依赖的情况。IOC解耦只是降低他们的依赖关系,但不会消除。例如:业务层仍会调用持久层的方法。
那这种业务层和持久局的依赖关系,在使用Spring之后,就让Spring来维护了。简单的说,就是坐等框架把持久层对象传入业务层,而不用我们自己去获取。
package com.taotao.service.impl;
import com.taotao.dao.UserDao;
import com.taotao.service.UserService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
* create by 刘鸿涛
* 2022/4/5 17:55
*/
public class UserServiceImpl implements UserService {
private UserDao userDao;
public void setUserDao(UserDao userDao) {
this.userDao = userDao;
}
public void save() {
// ApplicationContext app = new ClassPathXmlApplicationContext("applicationContext.xml");
//
// //向下转型为UserDao类型
// UserDao userDao = (UserDao) app.getBean("userDao");
userDao.save();
}
}
<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="userDao" class="com.taotao.dao.impl.UserDaoImpl">bean>
<bean id="userService" class="com.taotao.service.impl.UserServiceImpl">
<property name="userDao" ref="userDao">property>
bean>
beans>
P命名空间注入本质也是set方法注入,但比起上述的set方法注入更加方便,主要体现在配置文件中,如下:首先,需要引入P命名空间:
xmlns:p="http://www.springframework.org/schema/p"
其次,需要修改注入方法
<bean id="userService" class="com.taotao.service.impl.UserServiceImpl" p:userDao-ref="userDao"/>
package com.taotao.service.impl;
import com.taotao.dao.UserDao;
import com.taotao.service.UserService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
* create by 刘鸿涛
* 2022/4/5 17:55
*/
public class UserServiceImpl implements UserService {
private UserDao userDao;
public UserServiceImpl(UserDao userDao) {
this.userDao = userDao;
}
public UserServiceImpl() {
}
// public void setUserDao(UserDao userDao) {
// this.userDao = userDao;
// }
public void save() {
// ApplicationContext app = new ClassPathXmlApplicationContext("applicationContext.xml");
//
// //向下转型为UserDao类型
// UserDao userDao = (UserDao) app.getBean("userDao");
userDao.save();
}
}
<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="userDao" class="com.taotao.dao.impl.UserDaoImpl">bean>
<bean id="userService" class="com.taotao.service.impl.UserServiceImpl">
<constructor-arg name="userDao" ref="userDao">constructor-arg>
bean>
beans>
上面的操作,都是注入的引用Bean,除了对象的引河以注入,普通数据类型,集合等都可以在容器中进行注入。
package com.taotao.dao.impl;
import com.taotao.dao.UserDao;
/**
* create by 刘鸿涛
* 2022/4/3 22:36
*/
public class UserDaoImpl implements UserDao {
private String username;
private int age;
public void setUsername(String username) {
this.username = username;
}
public void setAge(int age) {
this.age = age;
}
public void save() {
System.out.println(username + "\n" + age );
System.out.println("save running...");
}
}
<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="userDao" class="com.taotao.dao.impl.UserDaoImpl">
<property name="username" value="taotao"/>
<property name="age" value="18"/>
bean>
<bean id="userService" class="com.taotao.service.impl.UserServiceImpl">
<constructor-arg name="userDao" ref="userDao">constructor-arg>
bean>
beans>
package com.taotao.dao.impl;
import com.taotao.dao.UserDao;
import com.taotao.domain.User;
import java.util.List;
import java.util.Map;
import java.util.Properties;
/**
* create by 刘鸿涛
* 2022/4/3 22:36
*/
public class UserDaoImpl implements UserDao {
private String username;
private int age;
private List<String> strList;
private Map<String, User> userMap;
private Properties properties;
public void setStrList(List<String> strList) {
this.strList = strList;
}
public void setUserMap(Map<String, User> userMap) {
this.userMap = userMap;
}
public void setProperties(Properties properties) {
this.properties = properties;
}
public void setUsername(String username) {
this.username = username;
}
public void setAge(int age) {
this.age = age;
}
public void save() {
System.out.println(strList);
System.out.println(userMap);
System.out.println(properties);
System.out.println("save running...");
}
}
package com.taotao.domain;
/**
* create by 刘鸿涛
* 2022/4/5 22:06
*/
public class User {
private String name;
private String addr;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAddr() {
return addr;
}
public void setAddr(String addr) {
this.addr = addr;
}
@Override
public String toString() {
return "User{" +
"name='" + name + '\'' +
", addr='" + addr + '\'' +
'}';
}
}
<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="userDao" class="com.taotao.dao.impl.UserDaoImpl">
<property name="strList" >
<list>
<value>111value>
<value>222value>
<value>333value>
list>
property>
bean>
<bean id="userService" class="com.taotao.service.impl.UserServiceImpl">
<constructor-arg name="userDao" ref="userDao">constructor-arg>
bean>
beans>
<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="userDao" class="com.taotao.dao.impl.UserDaoImpl">
<property name="strList" >
<list>
<value>111value>
<value>222value>
<value>333value>
list>
property>
<property name="userMap">
<map>
<entry key="u1" value-ref="user1">entry>
<entry key="u2" value-ref="user2">entry>
map>
property>
bean>
<bean id="user1" class="com.taotao.domain.User">
<property name="name" value="tom"/>
<property name="addr" value="beijing"/>
bean>
<bean id="user2" class="com.taotao.domain.User">
<property name="name" value="jack"/>
<property name="addr" value="shanghai"/>
bean>
<bean id="userService" class="com.taotao.service.impl.UserServiceImpl">
<constructor-arg name="userDao" ref="userDao">constructor-arg>
bean>
beans>
<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="userDao" class="com.taotao.dao.impl.UserDaoImpl">
<property name="strList" >
<list>
<value>111value>
<value>222value>
<value>333value>
list>
property>
<property name="userMap">
<map>
<entry key="u1" value-ref="user1">entry>
<entry key="u2" value-ref="user2">entry>
map>
property>
<property name="properties">
<props>
<prop key="p1">pp1prop>
<prop key="p2">pp2prop>
<prop key="p3">pp3prop>
props>
property>
bean>
<bean id="user1" class="com.taotao.domain.User">
<property name="name" value="tom"/>
<property name="addr" value="beijing"/>
bean>
<bean id="user2" class="com.taotao.domain.User">
<property name="name" value="jack"/>
<property name="addr" value="shanghai"/>
bean>
<bean id="userService" class="com.taotao.service.impl.UserServiceImpl">
<constructor-arg name="userDao" ref="userDao">constructor-arg>
bean>
beans>