public class Student {
}
创建 Mapper 接口
public interface StudentMapper {
void insert(Student stu);
int delete(Long id);
}
创建 Mapper 实现类
public class StudentMapperImpl implements StudentMapper{
public void insert(Student stu) {
System.out.println("保存学生信息");
}
public int delete(Long id) {
System.out.println("删除id="+id+"的学生信息");
return 1;
}
}
将 Mapper 交给容器管理
<bean id="studentMapper" class="cn.sycoder.di.mapper.StudentMapperImpl">bean>
创建 service 接口
public interface IStudentService {
void insert(Student stu);
int delete(Long id);
}
创建 service 实现类
public class StudentServiceImpl implements IStudentService {
private StudentMapper mapper;
public void insert(Student stu) {
mapper.insert(stu);
}
public int delete(Long id) {
return mapper.delete(id);
}
}
将 service 交给容器管理
<bean id="iStudentService" class="cn.sycoder.di.service.impl.StudentServiceImpl">bean>
配置 service 构造器
public class StudentServiceImpl implements IStudentService {
private StudentMapper mapper;
public StudentServiceImpl(StudentMapper mapper){
this.mapper = mapper;
}
public void insert(Student stu) {
mapper.insert(stu);
}
public int delete(Long id) {
return mapper.delete(id);
}
}
配置 xml
<bean id="iStudentService" class="cn.sycoder.di.service.impl.StudentServiceImpl">
<constructor-arg name="mapper" ref="studentMapper">constructor-arg>
bean>
<bean id="studentMapper" class="cn.sycoder.di.mapper.StudentMapperImpl">bean>
注意:
service
public class StudentServiceImpl implements IStudentService {
private StudentMapper mapper;
private UserMapper userMapper;
public StudentServiceImpl(StudentMapper mapper,UserMapper userMapper){
this.mapper = mapper;
this.userMapper = userMapper;
}
public void insert(Student stu) {
mapper.insert(stu);
}
public int delete(Long id) {
userMapper.delete(id);
return mapper.delete(id);
}
}
mapper
public interface UserMapper {
int delete(Long id);
}
mapper 实现类
public class UserMapperImpl implements UserMapper{
public int delete(Long id) {
System.out.println("删除id="+id+"的用户信息");
return 1;
}
}
配置
<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="iStudentService" class="cn.sycoder.di.service.impl.StudentServiceImpl">
<constructor-arg name="mapper" ref="studentMapper">constructor-arg>
<constructor-arg name="userMapper" ref="userMapper">constructor-arg>
bean>
<bean id="studentMapper" class="cn.sycoder.di.mapper.StudentMapperImpl">bean>
<bean id="userMapper" class="cn.sycoder.di.mapper.UserMapperImpl">bean>
beans>
service
public class StudentServiceImpl implements IStudentService {
private String name;
private int age;
private StudentMapper mapper;
private UserMapper userMapper;
public StudentServiceImpl(String name,int age,StudentMapper mapper,UserMapper userMapper){
this.name = name;
this.age = age;
this.mapper = mapper;
this.userMapper = userMapper;
}
public void insert(Student stu) {
mapper.insert(stu);
}
public int delete(Long id) {
System.out.println( name+":"+age);
userMapper.delete(id);
return mapper.delete(id);
}
}
xml
<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="iStudentService" class="cn.sycoder.di.service.impl.StudentServiceImpl">
<constructor-arg name="userMapper" ref="userMapper">constructor-arg>
<constructor-arg name="mapper" ref="studentMapper">constructor-arg>
<constructor-arg type="int" value="18">constructor-arg>
<constructor-arg type="java.lang.String" value="sy">constructor-arg>
bean>
<bean id="studentMapper" class="cn.sycoder.di.mapper.StudentMapperImpl">bean>
<bean id="userMapper" class="cn.sycoder.di.mapper.UserMapperImpl">bean>
beans>
这种方式会存在参数覆盖的问题,解决方式,删除 type 添加 index 属性
<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="iStudentService" class="cn.sycoder.di.service.impl.StudentServiceImpl">
<constructor-arg name="userMapper" ref="userMapper">constructor-arg>
<constructor-arg name="mapper" ref="studentMapper">constructor-arg>
<constructor-arg index="2" value="18">constructor-arg>
<constructor-arg index="1" value="1">constructor-arg>
<constructor-arg type="java.lang.String" value="sy">constructor-arg>
bean>
<bean id="studentMapper" class="cn.sycoder.di.mapper.StudentMapperImpl">bean>
<bean id="userMapper" class="cn.sycoder.di.mapper.UserMapperImpl">bean>
beans>
使用 set 方法实现属性的注入
使用 property 属性
public class Employee {
}
mapper 接口
public interface EmployeeMapper {
int delete(Long id);
}
mapper 实现类
public class EmployeeMapperImpl implements EmployeeMapper {
public int delete(Long id) {
System.out.println("删除当前员工id:"+id);
return 1;
}
}
创建 service 接口
public interface IEmployeeService {
int delete(Long id);
}
创建 service 接口实现类
public class EmployeeServiceImpl implements IEmployeeService {
private EmployeeMapper mapper;
public int delete(Long id) {
return mapper.delete(id);
}
}
配置bean
<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="empMapper" class="cn.sycoder.di.setter.mapper.EmployeeMapperImpl">bean>
<bean id="empService" class="cn.sycoder.di.setter.service.impl.EmployeeServiceImpl">bean>
beans>
service 实现中提供 mapper 的setter 方法
public class EmployeeServiceImpl implements IEmployeeService {
private EmployeeMapper employeeMapper;
public int delete(Long id) {
return employeeMapper.delete(id);
}
public void setEmployeeMapper(EmployeeMapper employeeMapper){
this.employeeMapper = employeeMapper;
}
}
修改 beans.xml 通过 setter 注入
<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="empMapper" class="cn.sycoder.di.setter.mapper.EmployeeMapperImpl">bean>
<bean id="empService" class="cn.sycoder.di.setter.service.impl.EmployeeServiceImpl">
<property name="employeeMapper" ref="empMapper">property>
bean>
beans>
获取 service 执行 delete 方法
@Test
public void testSetDi(){
final ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("DiSetterBeans.xml");
final IEmployeeService empService = (IEmployeeService) context.getBean("empService");
empService.delete(2L);
}
setter 注入过程分析
给service 添加新的属性以及新的setter方法
public class EmployeeServiceImpl implements IEmployeeService {
private EmployeeMapper employeeMapper;
private UserMapper userMapper;
public int delete(Long id) {
return employeeMapper.delete(id);
}
public void setEmployeeMapper(EmployeeMapper employeeMapper){
System.out.println("=======使用 setter 注入=======");
this.employeeMapper = employeeMapper;
}
public void setUserMapper(UserMapper mapper){
this.userMapper = mapper;
}
}
配置 userMapper bean
<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="empMapper" class="cn.sycoder.di.setter.mapper.EmployeeMapperImpl">bean>
<bean id="empService" class="cn.sycoder.di.setter.service.impl.EmployeeServiceImpl">
<property name="employeeMapper" ref="empMapper">property>
bean>
<bean id="userMapper" class="cn.sycoder.di.constructor.mapper.StudentMapperImpl">bean>
beans>
通过 setter 注入
<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="empMapper" class="cn.sycoder.di.setter.mapper.EmployeeMapperImpl">bean>
<bean id="empService" class="cn.sycoder.di.setter.service.impl.EmployeeServiceImpl">
<property name="employeeMapper" ref="empMapper">property>
<property name="userMapper" ref="userMapper">property>
bean>
<bean id="userMapper" class="cn.sycoder.di.constructor.mapper.UserMapperImpl">bean>
beans>
获取 service 操作delete 方法
@Test
public void testSetterSDi(){
final ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("DiSetterBeans.xml");
final IEmployeeService empService = (IEmployeeService) context.getBean("empService");
empService.delete(2L);
}
修改 service 类,提供两个属性 int age = 18,String name = “sy”
public class EmployeeServiceImpl implements IEmployeeService {
private EmployeeMapper employeeMapper;
private UserMapper userMapper;
private String name;
private int age;
public void setName(String name){
this.name = name;
}
public void setAge(int age){
this.age = age;
}
public int delete(Long id) {
System.out.println(name + ":" + age);
userMapper.delete(id);
return employeeMapper.delete(id);
}
public void setEmployeeMapper(EmployeeMapper employeeMapper){
System.out.println("=======EmployeeMapper使用 setter 注入=======");
this.employeeMapper = employeeMapper;
}
public void setUserMapper(UserMapper mapper){
System.out.println("=======UserMapper使用 setter 注入=======");
this.userMapper = mapper;
}
}
配置 xml 设置值
<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="empMapper" class="cn.sycoder.di.setter.mapper.EmployeeMapperImpl">bean>
<bean id="empService" class="cn.sycoder.di.setter.service.impl.EmployeeServiceImpl">
<property name="employeeMapper" ref="empMapper">property>
<property name="userMapper" ref="userMapper">property>
<property name="name" value="sy">property>
<property name="age" value="18">property>
bean>
<bean id="userMapper" class="cn.sycoder.di.constructor.mapper.UserMapperImpl">bean>
beans>
可能出现的问题
public class CollectionsDemo {
private List<Integer> list;
private Map<String,String> map;
private Set<String> set;
private Properties properties;
private int[] arr;
public void print(){
System.out.println("list:"+list);
System.out.println("map:"+map);
System.out.println("set:"+set);
System.out.println("properties:"+properties);
System.out.println("arr:"+ Arrays.toString(arr));
}
public void setList(List<Integer> list) {
this.list = list;
}
public void setMap(Map<String, String> map) {
this.map = map;
}
public void setSet(Set<String> set) {
this.set = set;
}
public void setProperties(Properties properties) {
this.properties = properties;
}
public void setArr(int[] arr) {
this.arr = arr;
}
}
<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="collectionsDemo" class="cn.sycoder.collections.CollectionsDemo">
<property name="list">
<list>
<value>1value>
<value>2value>
<value>3value>
list>
property>
<property name="map">
<map>
<entry key="name" value="sy"/>
<entry key ="age" value="18"/>
map>
property>
<property name="set">
<set>
<value>just some stringvalue>
<value>just stringvalue>
set>
property>
<property name="properties">
<props>
<prop key="url">@example.orgprop>
<prop key="user">rootprop>
<prop key="password">123456prop>
props>
property>
<property name="arr">
<array>
<value>2value>
<value>2value>
<value>2value>
array>
property>
bean>
beans>
准备工作
public class EmployeeService {
private EmployeeMapperImpl employeeMapper;
public int delete(Long id) {
return employeeMapper.delete(id);
}
public void setEmployeeMapper(EmployeeMapperImpl employeeMapper){
System.out.println("=======EmployeeMapper使用 setter 注入=======");
this.employeeMapper = employeeMapper;
}
}
public class EmployeeMapperImpl{
public int delete(Long id) {
System.out.println("删除当前员工id:"+id);
return 1;
}
}
配置 bean 并且通过 bype 自动装配
<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="empService" class="cn.sycoder.autowired.EmpService" autowire="byType">bean>
<bean id="empMapperImpl" class="cn.sycoder.autowired.EmpMapperImpl">bean>
beans>
配置 bean 并且通过 byName 自动装配
<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="empService" class="cn.sycoder.autowired.EmpService" autowire="byName">bean>
<bean id="empMapperImpl" class="cn.sycoder.autowired.EmpMapperImpl">bean>
beans>
通过名称和类型的自动装配
注意:
常见的作用域
作用域 | 说明 |
---|---|
singleton | 单例的 |
prototype | 多例 |
request | 请求 |
session | 会话 |
单例 singleton
修改对象变成多个实例的
注意:容器模式就是以单例的方式创建对象的,如果需要修改成非单例,使用 scope 属性修改即可
以后开发中适合将那些bean对象交给 spring 管理
单例bean会出现线程安全吗
创建用户类
public class User {
private String name;
public User(){
System.out.println("构造器执行====");
}
public void setName(String name) {
System.out.println("调用 set 方法");
this.name = name;
}
public void init(){
System.out.println("调用 init 方法");
}
public void destroy(){
System.out.println("调用销毁方法");
}
}
配置 bean
<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="user" class="cn.sycoder.lifecycle.User" init-method="init" destroy-method="destroy">
<property name="name" value="sy">property>
bean>
beans>
获取 bean 出现如下问题,没有打印销毁方法
自定义自己 bean 处理器
public class MyBeanPostProcessor implements BeanPostProcessor{
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
//bean 前置处理器
System.out.println("bean 的前置处理器");
return bean;
}
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
System.out.println("bean 的后置处理器");
//bean 后置处理器
return bean;
}
}
配置 bean
<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="user" class="cn.sycoder.lifecycle.User" init-method="init" destroy-method="destroy">
<property name="name" value="sy">property>
bean>
<bean class="cn.sycoder.lifecycle.MyBeanPostProcessor">bean>
beans>