Spring构造器注入
beans.xml
<?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 id="UserDaoImpl" class="com.zhangpn.daoImpl.UserDaoImpl"></bean> <bean id="UserServiceImpl" class="com.zhangpn.serviceImpl.UserServiceImpl"> <constructor-arg index="0" ref="UserDaoImpl"></constructor-arg> </bean> </beans>
测试:
package com.zhangpn.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import com.zhangpn.serviceImpl.UserServiceImpl; public class Test { public static void main(String[] args) { ApplicationContext applicationContext = new ClassPathXmlApplicationContext("beans.xml"); UserServiceImpl userDao = (UserServiceImpl) applicationContext.getBean("UserServiceImpl"); userDao.getUserDao().run(); } }
结果:
凝一眸碧水,拈一缕清风,于一怀静谧中倾听凡尘的落音。
不再奢求什么,做简单的自己,过简单的生活,心在,梦在,你在,便是光阴赐予我的最美。