一、搭建步骤
1、创建一个maven项目
2、在pom.xml中导入项目所需要的库
<!-- spring -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.3.18</version>
</dependency>
<!-- 测试 -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
3、编写配置类
applicationContext.xml
<?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
https://www.springframework.org/schema/beans/spring-beans.xsd">
<!-- 将对象交给spring进行管理-->
<bean id="mapperImpl" class="com.tao.dao.UserMapperImpl"/>
<bean id="mysqlImpl" class="com.tao.dao.UserMapperMysqlImpl"/>
<bean id="oracleImpl" class="com.tao.dao.UserMapperOracleImpl"/>
<!--根据注入的对象,实现对应的功能-->
<bean id="userServiceImpl" class="com.tao.service.UserServiceImpl">
<property name="userMapper" ref="oracleImpl"/>
</bean>
</beans>
4、测试
创建dao和service包,进行测试
在dao包中创建三个实现类分别做不同的操作
UserMapperImpl
public class UserMapperImpl implements UserMapper{
@Override
public void insertUser(User user) {
System.out.println("插入一个新用户");
}
}
UserMapperMysqlImpl
public class UserMapperMysqlImpl implements UserMapper{
@Override
public void insertUser(User user) {
System.out.println("使用mysql插入一条信息");
}
}
UserMapperOracleImpl
public class UserMapperOracleImpl implements UserMapper{
@Override
public void insertUser(User user) {
System.out.println("使用Oracle插入一条信息");
}
}
在Service层通过set注入
public class UserServiceImpl implements UserService{
private UserMapper userMapper;
public void setUserMapper(UserMapper userMapper) {
this.userMapper = userMapper;
}
@Override
public void add() {
userMapper.insertUser(new User());
}
}
测试
@Test
public void test(){
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
UserServiceImpl userServiceImpl = context.getBean("userServiceImpl", UserServiceImpl.class);
userServiceImpl.add();
}
二、set方法注入值和构造函数注入值
1、set方法设置属性值
使用<property 标签 :通过setter对应的方法注入。
<!-- 通过set方法设置属性值-->
<bean id="user" class="com.tao.pojo.User">
<property name="id" value="12"/>
<property name="name" value="小明"/>
<property name="pwd" value="123456"/>
</bean>
2、构造器注入(三种方式)
constructor-arg:通过构造函数注入。
<!-- 方法一:通过构造函数参数下标设置参数值 -->
<bean id="user" class="com.tao.pojo.User">
<constructor-arg index="0" value="1"/>
<constructor-arg index="1" value="小明"/>
<constructor-arg index="2" value="123456"/>
</bean>
<!-- 方法二:通过构造函数参数名字设置参数值 -->
<bean id="user" class="com.tao.pojo.User">
<constructor-arg name="id" value="1"/>
<constructor-arg name="name" value="小明"/>
<constructor-arg name="pwd" value="123456"/>
</bean>
<!-- 方法三:通过类型设置参数值 ,不建议使用 -->
<!--<bean id="user" class="com.tao.pojo.User">
<constructor-arg type="int" value="1"/>
<constructor-arg type="java.lang.String" value="小明"/>
<constructor-arg type="java.lang.String" value="123456"/>
</bean>-->
三、spring的配置
import的使用
团队的合作通过import来实现 .
<?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
https://www.springframework.org/schema/beans/spring-beans.xsd">
<import resource="bean.xml"/>
</beans>
bean的作用域
在配置bean的时候,可以使用scope来设置bean的作用域
<!--根据注入的对象,实现对应的功能
id:唯一标识符
class:bean的全限定名=包名+类名
scope:bean的作用域,默认为singleton(单例模式)
-->
<bean id="userServiceImpl" class="com.tao.service.UserServiceImpl" scope="singleton">
<property name="userMapper" ref="oracleImpl"/>
</bean>
bean作用域四个类别
Singleton(默认值):Spring IoC容器中只会存在一个共享的bean实例,并且所有对bean的请求,只要id与该bean定义相匹配,则只会返回bean的同一实例。Singleton是单例类型,就是在创建起容器时就同时自动创建了一个bean的对象,不管你是否使用,他都存在了,每次获取到的对象都是同一个对象。
Prototype:每次从容器中调用Bean时,都返回一个新的实例,即每次调用getBean()时,相当于执行new XxxBean()。
Request:每次HTTP请求都会创建一个新的Bean,该作用域仅适用于WebApplication Context环境。
Session:同一个HTTP Session共享一个Bean,不同Session使用不同Bean,仅适用于WebApplication Context环境。