spring依赖注入小结

    xiaoxiao2024-12-19  3

    spring依赖注入两大方式: 1.基于xml配置文件实现依赖注入(记得提供get/set方法 ) 1.1构造方法注入 1.2set方法注入 1.3Field注入 步骤: 1.加入spring所需要的jar包 2.在spring配置文件中进行相关配置 3.通过getBean()获取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" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd"> <!--1.2set方法注入--> <bean id="menuDao" class="com.xiaogang.menu.dao.MenuDao" /> <bean id="menuService" class="com.xiaogang.menu.service.impl.MenuService"> <property name="menuDao" ref="menuDao" /> </bean> <!-- Field注入--> <bean id="person" class="com.demo.Person"> <property name="name" value="jack"/> <property name="age" value="28"/> <property name="sex" value="男"/> </bean> <!-- 1.1构造方法注入--> <bean id="person2" class="com.demo.Person"> <constructor-arg index="0" value="奔放哥"></constructor-arg> <constructor-arg index="1" value="29"></constructor-arg> <constructor-arg index="2" value="女"></constructor-arg> </bean> <bean id="demo_list" class="com.demo.Demo"> <property name="emails"> <list> <value>xiaogang@sina.com.cn</value> <value>xiaowei@163.com</value> <value>xiaobai@mm.com</value> </list> </property> <property name="mobiles"> <set> <value>05937732324</value> <value>24324234332</value> <value>32432432432</value> </set> </property> <property name="contacts"> <map> <entry key="jack" value="ross"/> <entry> <key> <value>武当</value> </key> <value>张三丰</value> </entry> </map> </property> <property name="props"> <props> <prop key="bb">张梓兵</prop> <prop key="dd">伍德的</prop> </props> </property> </bean> </beans> java代码: import com.xiaogang.dao.BaseDao; import com.xiaogang.menu.service.IMenuService; import com.xiaogang.pojo.Menu; //业务层 public class MenuService implements IMenuService { private BaseDao menuDao; //需要提供set方法 public void setMenuDao(BaseDao menuDao) { this.menuDao = menuDao; } @Override public boolean save(Menu menu) { this.menuDao.save(""); return false; } } java的测试代码: import java.util.Set; import java.util.Map.Entry; import org.junit.BeforeClass; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import com.demo.Demo; import com.demo.Person; import com.xiaogang.menu.service.IMenuService; public class Test01 { private static ApplicationContext ctx=null; @BeforeClass public static void setUpBeforeClass() throws Exception { //导入spring配置文件 ctx = new ClassPathXmlApplicationContext("beans.xml"); } @Test public void testSaveMenu(){ //通过bean获取menuService对象 IMenuService menuService=(IMenuService)ctx.getBean("menuService"); menuService.save(null); } @Test public void testPersonValue(){ Person person=(Person)ctx.getBean("person"); System.out.println(person); } @Test public void testPersonContructor(){ Person person2=(Person)ctx.getBean("person2"); System.out.println(person2); } @Test public void testDemo(){ 通过bean获取集合对象 Demo demo=(Demo)ctx.getBean("demo_list"); for(String temp:demo.getEmails()){ System.out.println(temp); } System.out.println("**************"); for(String m:demo.getMobiles()){ System.out.println(m); } System.out.println("**************"); for(Entry<String,String> entry:demo.getContacts().entrySet()){ System.out.println(entry); } System.out.println("**************"); for(Object s:demo.getProps().keySet()){ System.out.println(s+"="+demo.getProps().get(s)); } } }

    2.基于注解实现依赖注入,分为手动装配依赖注入(一般常用)和自动装配依赖注入(不需要在代码添加注解,通过配置文件中的autowire="byName",容易出错)。

    注解流程解析:

    当创建ServiceImpl类对象的时候,发现这个类中有一个属性daoImpl而且有一个resource的注解,故回到配置文件中去找有没有一个id叫“daoImpl”的bean,如果有,就创建它的对象,并赋值给类中的daoImpl.

    spring配置文件: <?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 http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd"> <!-- 启动Spring注解实现依赖注入--> <context:annotation-config /> <bean id="daoImpl" class="com.xiaogang.dao.impl.DaoImpl" /> <bean id="serviceImpl" class="com.xiaogang.service.impl.ServiceImpl" autowire="byName"/> </beans> java代码: import com.xiaogang.dao.Dao; import com.xiaogang.service.IService; /* * 注解实现依赖注入(DI) * @Resource * 按属性名称注入,如按名称找不到,则按类型查找.若明确指定按属性名称查找,则只会按名称查找。 * @Autowired * 只按类型查找 */ public class ServiceImpl implements IService { //@Resource //@Resource(name="daoImpl") private Dao daoImpl; //@Resource //@Resource(name="daoImpl") public void setDaoImpl(Dao daoImpl) { this.daoImpl = daoImpl; } @Override public void save() { daoImpl.save(); } } java测试代码: import org.junit.BeforeClass; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import com.xiaogang.service.IService; public class Test01 { static ApplicationContext ctx; @BeforeClass public static void setUpBeforeClass() throws Exception { ctx = new ClassPathXmlApplicationContext("beans.xml"); } @Test public void testSave(){ IService service = (IService)ctx.getBean("serviceImpl"); service.save(); } }
    转载请注明原文地址: https://ju.6miu.com/read-1294759.html
    最新回复(0)