基于XML配置的方式实现
1. 基本使用
1.1 添加Spring依赖
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.1.17.RELEASE</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
</dependencies>
1.2 添加Spring配置文件
<?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">
</beans>
1.3 注册Bean
将需要被IoC容器管理的对象通过标签来注册
<?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 class="com.gupaoedu.pojo.UserBean" />
</beans>
1.4 从IoC容器中获取对象
/**
* IoC的方式获取 UserBean 对象
*/
@Test
public void fun2(){
// 1.IoC容器的初始化操作 调用UserBean中无参构造器创建对象
ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
// 2.从容器中获取UserBean对象 没有显示的new
UserBean user = (UserBean) ac.getBean("userBean");
user.say();
}
2.从容器中获取对象的方式
2.1 根据ID获取
容器中Bean的ID只能声明一个
<?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 class="com.gupaoedu.pojo.UserBean" id="userBean"/>
</beans>
2.2 根据Name获取
name可以声明多个,name=“u1,u2,u3” 表示会被拆分为3个name属性【拆分会根据 ‘,’ ‘;’ ’ ’ 空格 】
<?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 class="com.gupaoedu.pojo.UserBean" id="userBean" name="userBean2"/>-->
<bean class="com.gupaoedu.pojo.UserBean" id="user1,user2,user3" name="u1,u2,u3"/>
</beans>
2.3 根据类型获取
@Test
public void fun6(){
ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
UserBean bean = ac.getBean(UserBean.class);
bean.say();
}
如果同一类型的对象在容器中存在多个,这时通过类型在获取对象就会报错!!!
<?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 class="com.gupaoedu.pojo.UserBean" id="userBean" name="userBean2"/>-->
<bean class="com.gupaoedu.pojo.UserBean" id="user1,user2,user3" name="u1 u2 u3"/>
<bean class="com.gupaoedu.pojo.UserBean" id="userBean1" name="ub1"/>
</beans>
2.4 primary属性
通过设置的primary属性为true,来表示当前bean对象优先被获取,如果同一类型有多个对象时,会优先返回primary属性为true的对象。
3.注入方式(DI)
3.1 构造注入
通过构造方法进行注入,首先得提供对应的构造方法。
既可以通过name也可以通过index来进行赋值。
<?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 class="com.gupaoedu.pojo.UserBean" id="user">
<!-- 构造注入 -->
<!--<constructor-arg name="id" value="666"/>
<constructor-arg name="userName" value="bobo"/>-->
<constructor-arg index="0" value="999" />
<constructor-arg index="1" value="gp" />
</bean>
</beans>
3.2 设值注入
设置注入的属性必须提供对应的**set()**方法。
<bean class="com.gupaoedu.pojo.UserBean" id="user1">
<!-- 设值注入 -->
<property name="id" value="1"/>
<property name="userName" value="张三"/>
</bean>
3.3 简化构造注入与设值注入
在xml文件头部引入对应的名称空间
xmlns:p="http://www.springframework.org/schema/p"
xmlns:c="http://www.springframework.org/schema/c"
<?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"
xmlns:c="http://www.springframework.org/schema/c"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<!-- 构造注入 -->
<bean class="com.gupaoedu.pojo.UserBean" id="user" c:_0="111" c:_1="lisi">
<!--
<constructor-arg index="0" value="999" />
<constructor-arg index="1" value="gp" />
-->
</bean>
<!-- 设值注入 -->
<bean class="com.gupaoedu.pojo.UserBean" id="user1" p:id="222" p:userName="wangwu">
<!--
<property name="id" value="1"/>
<property name="userName" value="张三"/>
-->
</bean>
</beans>
3.4 其他类型注入
private Cat cat;
private String[] favorites;
private List<Cat> cats;
private Map<String,Object> map;
private Properties props;
<?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"
xmlns:c="http://www.springframework.org/schema/c"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<!-- 注册一个Cat对象-->
<bean class="com.gupaoedu.pojo.Cat" id="cat" p:nick="花花" p:color="黑色"/>
<bean class="com.gupaoedu.pojo.UserBean" id="user">
<!-- 设值注入 -->
<property name="cat" ref="cat">
<!--<bean class="com.gupaoedu.pojo.Cat" />-->
</property>
<property name="favorites">
<array>
<value>篮球</value>
<value>爬山</value>
<value>逛街</value>
</array>
</property>
<property name="cats">
<list>
<bean class="com.gupaoedu.pojo.Cat" p:nick="小花1" p:color="红色"/>
<bean class="com.gupaoedu.pojo.Cat" p:nick="小花2" p:color="绿色"/>
<bean class="com.gupaoedu.pojo.Cat" p:nick="小花3" p:color="黄色"/>
</list>
</property>
<property name="map" >
<map>
<entry key="name1" value="张三1"/>
<entry key="name2" value="张三2"/>
<entry key="name3" value="张三3"/>
</map>
</property>
<property name="props">
<props>
<prop key="username" >root</prop>
<prop key="password">123</prop>
</props>
</property>
</bean>
</beans>
3.5 工厂注入
3.5.1 静态工厂注入
package com.gupaoedu.factory;
import com.gupaoedu.pojo.UserBean;
import java.util.HashMap;
import java.util.Map;
public class StaticFactoryDemo {
public static Map<String,UserBean> hashMap ;
static {
hashMap = new HashMap<String, UserBean>();
hashMap.put("a1",new UserBean());
hashMap.put("a2",new UserBean());
hashMap.put("a3",new UserBean());
}
/**
* 静态工厂提供的方法
* @return
*/
public static UserBean getInstance(){
return hashMap.get("a1");
}
}
<?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 class="com.gupaoedu.factory.StaticFactoryDemo" factory-method="getInstance" id="user"></bean>
</beans>
3.5.2 动态工厂注入
package com.gupaoedu.factory;
import com.gupaoedu.pojo.UserBean;
/**
* 让每一个人的职业生涯不留遗憾
*
* @author 波波老师【咕泡学院】
*/
public class DynamicFactoryDemo {
public UserBean getInstance(){
return new UserBean();
}
}
<?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 class="com.gupaoedu.factory.DynamicFactoryDemo" id="dynamicFactoryDemo" ></bean>
<!-- 从工厂对象中获取 需要的对象-->
<bean id="user2" factory-bean="dynamicFactoryDemo" factory-method="getInstance"/>
</beans>
4.其他配置
4.1 事物管理配置
<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"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.1.xsd">
<context:component-scan base-package="com.gupaoedu.*" />
<context:property-placeholder location="db.properties"/>
<!-- 配置数据源 -->
<bean class="org.springframework.jdbc.datasource.DriverManagerDataSource" id="dataSource">
<property name="url" value="${jdbc_url}"/>
<property name="driverClassName" value="${jdbc_driver}"/>
<property name="username" value="${jdbc_username}"/>
<property name="password" value="${jdbc_password}"/>
</bean>
<!-- 配置JdbcTemplate -->
<bean class="org.springframework.jdbc.core.JdbcTemplate" >
<constructor-arg name="dataSource" ref="dataSource"/>
</bean>
<bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
<tx:advice id="txAdvice" transaction-manager="txManager">
<!-- the transactional semantics... -->
<tx:attributes>
<!-- all methods starting with 'get' are read-only -->
<tx:method name="bus*" propagation=" "/>
<!-- other methods use the default transaction settings (see below) -->
<tx:method name="*"/>
</tx:attributes>
</tx:advice>
<aop:config>
<aop:pointcut id="tx" expression="execution(* com.gupaoedu.service..*.*(..))"/>
<aop:advisor advice-ref="txAdvice" pointcut-ref="tx"/>
</aop:config>
</beans>
4.2 SpringMVC配置
引入映射器与适配器
<?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 class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping" />
<!-- 处理器适配器 -->
<bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter" />
</beans>
5.配合注解使用
5.1 扫描标签
在配置文件中添加扫描标签来指定哪些包路径下的类被Spring管理,前提是这些类被@Component注解标识。
<?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:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<!-- 添加扫描的路径 指定从哪些package下加载被 @Component标注的类型-->
<!--<context:component-scan base-package="com.gupaoedu"/>-->
<!--<context:component-scan base-package="com.gupaoedu.controller
,com.gupaoedu.service.impl
,com.gupaoedu.dao.impl"/>-->
<context:component-scan base-package="com.gupaoedu.controller" />
<context:component-scan base-package="com.gupaoedu.service.impl" />
<context:component-scan base-package="com.gupaoedu.dao.impl" />
</beans>
可以自定义扫描规则
<?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:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<!--
use-default-filters="false" 表示不适用默认的过滤器
默认过滤器会识别 @Componenet @Controller @Service @Repository
-->
<context:component-scan base-package="com.gupaoedu.controller" use-default-filters="false">
<!-- 只扫描Controller注解标识的类 -->
<context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
<context:component-scan base-package="com.gupaoedu.service.impl,com.gupaoedu.dao.impl" use-default-filters="true" >
<!-- 排除掉某个注解 -->
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller" />
</context:component-scan>
</beans>
5.2 开启事务注解支持
<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"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.1.xsd">
<bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
<!-- 开启事务的注解支持-->
<tx:annotation-driven transaction-manager="txManager"/>
</beans>
5.3 开启SpringMVC注解支持
<?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:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
">
<!-- 开启扫描 -->
<context:component-scan base-package="com.gupaoedu.controller" />
<!-- 开启SpringMVC注解的使用 -->
<mvc:annotation-driven />
</beans>