首先在项目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了