#在Spring中我们Beans.xml通常需要进行添加不同的配置.每次都得去官方文档复制粘贴。尤其麻烦,其实不需要。
基础的控制反转Loc如下所示:
<?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">
<!--我们可以把现在整个xml文件当成一个容器-->
<!--使用Spring来创建对象,在Spring中这些都是Bean-->
<!--
java中的写法:
类型 变量名 = new 类型();
Hello hello = new Hello();
在Spring中:
id = 变量名
class = new 的对象
property 相对于给对象中属性设置一个值。
-->
<bean id="MysqlImpl" class="com.xkl.Dao.UserDaoMysqlImpl"/>
<bean id="OracleImpl" class="com.xkl.Dao.UserDaoOracleImpl"/>
<bean id="DaoImpl" class="com.xkl.Dao.UserDaoImpl"/>
<!--以上创建了两个底层对象,但我们实际要操作的是业务层对象,所以还需要创建一个业务层对象-->
<bean id="ServiceImpl" class="com.xkl.Service.UserServiceImpl">
<property name="userDao" ref="DaoImpl"/><!--这里的ref:业务层获取DAO层。引用Spring中创建好的对象
val:具体的值,基本数据类型,就比如Hello中的spring
-->
</bean>
</beans>
如果我们需要进行的是自动装配中的组件注解配置时:
复制这几行粘贴即可,把后面的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
https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
https://www.springframework.org/schema/context/spring-context.xsd">
<context:component-scan base-package="com.xkl"/>
<context:annotation-config/>
<bean id="username" class="com.xkl.pojo.User"/>
</beans>