Spring中需要配置不同插件的时候配置文件应该怎么写?

#在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>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值