springBoot引入xml管理Bean

首先在项目resources目录下新建xml文件,编写bean内容

<?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:context="http://www.springframework.org/schema/context"
       xmlns:task="http://www.springframework.org/schema/task"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
	   http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
       http://www.springframework.org/schema/context
       http://www.springframework.org/schema/context/spring-context-3.1.xsd
       http://www.springframework.org/schema/task
		http://www.springframework.org/schema/task/spring-task-3.1.xsd">



    <bean id="taskExecutor" class="org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor">
        <property name="corePoolSize" value="100" />
        <property name="maxPoolSize" value="1000" />
        <property name="queueCapacity" value="1500" />
    </bean>

    <bean id="UserServiceImpl" class="com.fk.service.Impl.UserServiceImpl">
    </bean>
    <bean id="usercontroller" class="com.fk.controller.UserController">
        <property name="userService" ref="UserServiceImpl"></property>
    </bean>


</beans>

如上图。我在我的xml中写了几个bean。把UserServiceImpl注入到controller中,很尴尬的事好久不用xml配置了,忘了怎么配,这里的<property name="userService" ref="UserServiceImpl"></property>里面的name属性要和controller中声明的service名称一样,而且要在注入该service的controller里面提供set方法,依稀记得spring是要根据set方法注入的

最后,需要在springboot项目里引入自己写的xml文件,简单点的话直接在入口类里注解引入:

清晰一点的方法是在项目可以扫描到的包下新建一个配置类,加上@ImportResource("classpath:application-bean.xml"),注意这里用了@Configuration注解而不是@Component,两者在创建bean时有所不同,Configuration可以理解为<beans></beans>


@Configuration
@ImportResource(locations={"classpath:application-bean.xml"})
public class ConfigClass {
    
}

这样也可以达到引入的目的,到此就可以使用xml管理bean了 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

WannaRunning

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值