时间:2021-07-01 10:21:17 帮助过:24人阅读
依赖注入(dependency injection):是Spring框架核心ioc(inversion of control)的具体实现
package com.example.demo.service.impl;
import com.example.demo.dao.UserDao;
import com.example.demo.service.UserService;
public class UserServiceImpl implements UserService {
private UserDao userDao;
public void setUserDao(UserDao userDao) {
this.userDao = userDao;
}
@Override
public void save() {
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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="userDao" class="com.example.demo.dao.UserDao" />
<bean id="userServiceImpl" class="com.example.demo.service.impl.UserServiceImpl">
<property name="userDao" ref="userDao"/>
</bean>
</beans>
备注:
p命名空间注入的本质就是set注入
<?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.example.demo.dao.UserDao" />
<bean id="userServiceImpl" class="com.example.demo.service.impl.UserServiceImpl" p:userDao-ref="userDao"/>
</beans>
备注:
package com.example.demo.service.impl;
import com.example.demo.dao.UserDao;
import com.example.demo.service.UserService;
public class UserServiceImpl implements UserService {
private UserDao userDao;
public UserServiceImpl(UserDao userDao) {
this.userDao = userDao;
}
@Override
public void save() {
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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="userDao" class="com.example.demo.dao.UserDao" />
<bean id="userServiceImpl" class="com.example.demo.service.impl.UserServiceImpl">
<constructor-arg name="userDao" ref="userDao"/>
</bean>
</beans>
备注:
SQL语言基础
标签:efi 不同的 修改 不同 tran 查询 工作 创建数据库 grant