Spring依赖注入_set注入_构造器注入_注解注入
属性setter方法注入_方式一(通用bean)_方法二(使用内部bean)_ 基本类型的注入_集合类型的注入
一、在beans.xml中,配置要被注入的bean.
二、在需要注入的bean配置中添加<property name="personDao" ref="personDao" />设置
1、被注入类接口 及 被注入的bean。
PersonDao.java
package com.dwt1220; public interface PersonDao { public abstract void add(); }
PersonDaoBean.java
package com.dwt1220; public class PersonDaoBean implements PersonDao{ public void add(){ System.out.println("执行PersonDaoBean中的add()方法"); } }
2.需要注入bean的类,及接口
PersonService.java
package com.dwt1220; public interface PersonService { public abstract void save(); }
PersonServiceBean.java
package com.dwt1220; public class PersonServiceBean implements PersonService { private PersonDao personDao; public PersonDao getPersonDao() { return personDao; } public void setPersonDao(PersonDao personDao) { this.personDao = personDao; } public void save() { personDao.add(); } }
3.beans.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" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd"> <bean id="personDao1" class="com.dwt1220.PersonDaoBean" /> <bean id="personService" class="com.dwt1220.PersonServiceBean"> <!-- name 需要注入的变量名,ref beans.xml中注册的的bean的id或name --> <property name="personDao" ref="personDao1" /> </bean> </beans>
方法二(使用内部bean,但该bean不能被其他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"> <bean id="personService" class="com.dwt1220.PersonServiceBean"> <!-- 使用内部bean,但该bean不能被其他bean使用 --> <property name="personDao"> <bean class="com.dwt1220.PersonDaoBean" /> </property> </bean> </beans>
4.Test.java
package com.dwt1220; import org.springframework.context.support.AbstractApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class Test { public static void main(String[] args) { AbstractApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml"); PersonService personService = (PersonService) ctx.getBean("personService"); personService.save(); ctx.close(); } }
基本类型的注入
在<bean>标签体内添加 <property name="string" value="stringvalue"/>。
<?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"> <bean id="personService" class="com.dwt1220.PersonServiceBean"> <!-- 基本类型的注入 name=变量名,value=值,会自动转换类型 --> <property name="string" value="stringvalue"/> <property name="id" value="88"/> </bean> </beans>
集合类型的注入
1、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-2.5.xsd"> <bean id="personService" class="com.dwt1220.PersonServiceBean"> <!-- set --> <property name="sets"> <set> <value>第一个set</value> <value>第二个set</value> <value>第三个set</value> </set> </property> <!-- list --> <property name="lists"> <list> <value>第一个list</value> <value>第二个list</value> <value>第三个list</value> </list> </property> <!-- properties --> <property name="properties"> <props> <prop key="key1">value1</prop> <prop key="key2">value2</prop> <prop key="key3">value4</prop> </props> </property> <!-- map --> <property name="maps"> <map> <entry key="key-1" value="value-1"/> <entry key="key-2" value="value-2"/> <entry key="key-3" value="value-3"/> </map> </property> </bean> </beans>
2、PersonService.java 接口
package com.dwt1220; import java.util.List; import java.util.Map; import java.util.Properties; import java.util.Set; public interface PersonService { public void save(); public Set<String> getSets(); public List<String> getLists(); public Properties getProperties(); public Map<String, String> getMaps(); }
3、PersonServiceBean.java 类
package com.dwt1220; import java.util.ArrayList; import java.util.HashMap; import java.util.HashSet; import java.util.List; import java.util.Map; import java.util.Properties; import java.util.Set; public class PersonServiceBean implements PersonService { private Set<String> sets = new HashSet<String>(); private List<String> lists = new ArrayList<String>(); private Properties properties = new Properties(); private Map<String, String> maps = new HashMap<String, String>(); public Set<String> getSets() { return sets; } public void setSets(Set<String> sets) { this.sets = sets; } public List<String> getLists() { return lists; } public void setLists(List<String> lists) { this.lists = lists; } public Properties getProperties() { return properties; } public void setProperties(Properties properties) { this.properties = properties; } public Map<String, String> getMaps() { return maps; } public void setMaps(Map<String, String> maps) { this.maps = maps; } public void save() { System.out.println("这是save方法"); } }
4、Test.java
package com.dwt1220; import java.util.List; import java.util.Map; import java.util.Properties; import java.util.Set; import org.springframework.context.support.AbstractApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class Test { public static void main(String[] args) { AbstractApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml"); PersonService personService = (PersonService) ctx.getBean("personService"); Set<String> sets=personService.getSets(); List<String> lists=personService.getLists(); Properties properties = personService.getProperties(); Map<String,String> maps=personService.getMaps(); System.out.println("----------Set--------------"); for(String set:sets){ System.out.println(set); } System.out.println("----------List--------------"); for(String list:lists){ System.out.println(list); } System.out.println("----------Properties--------------"); for(Object key:properties.keySet()){ System.out.println(key+"="+properties.getProperty((String)key)); } System.out.println("----------Map--------------"); for(String key:maps.keySet()){ System.out.println(key+"="+maps.get(key)); } ctx.close(); } }
构造器注入
1、在需要注入的类中,编写注入的构造方法
public PersonServiceBean(PersonDao personDao, String string) { this.personDao = personDao; this.string = string; }
2、在beans.xml中申明被注入的类
<bean id="personDao" class="com.dwt1220.PersonDaoBean" />
3、在注入类的<bean>标签体中使用<constructor-arg>标签
<bean id="personService" class="com.dwt1220.PersonServiceBean"> <!--index=构造方法的参数位置(下标从0开始), type=注入的类型,ref=注入类在beans.xml中注册的ID --> <constructor-arg index="0" type="com.dwt1220.PersonDao" ref="personDao"/> <constructor-arg index="1" value="dwt1220" /> </bean>
PersonServiceBean.java类
package com.dwt1220; public class PersonServiceBean implements PersonService { private PersonDao personDao; private String string; public PersonServiceBean(PersonDao personDao, String string) { this.personDao = personDao; this.string = string; } public PersonDao getPersonDao() { return personDao; } public void setPersonDao(PersonDao personDao) { this.personDao = personDao; } public void save() { System.out.println("这是save方法"); System.out.println(string); } }
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-2.5.xsd"> <bean id="personDao" class="com.dwt1220.PersonDaoBean" /> <bean id="personService" class="com.dwt1220.PersonServiceBean"> <!--index=构造方法的参数位置(下标从0开始), type=注入的类型,ref=注入类在beans.xml中注册的ID --> <constructor-arg index="0" type="com.dwt1220.PersonDao" ref="personDao"/> <constructor-arg index="1" value="dwt1220" /> </bean> </beans>
Test.java
package com.dwt1220; import org.springframework.context.support.AbstractApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class Test { public static void main(String[] args) { AbstractApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml"); PersonService personService = (PersonService) ctx.getBean("personService"); personService.save(); ctx.close(); } }
注解注入
1、所需配置
@Autowired private PersonDao personDao;//用于字段上 @Autowired public void setOrderDao(OrderDao orderDao) {//用于属性的setter方法上 this.orderDao = orderDao; }
@Autowired @Qualifier("personDaoBean") private PersonDao personDao;
package com.dwt1220; import javax.annotation.Resource; public class PersonServiceBean implements PersonService { /** * 如果没有指定name属性,并且按照默认的名称仍然找不到依赖对象时, * * @Resource注解会回退到按类型装配。但一旦指定了name属性,就只能按名称装配了。 **/ @Resource(name = "personDao1") private PersonDao personDao; @Resource(name = "personDao1") public void setPersonDao(PersonDao personDao) { this.personDao = personDao; } public void save() { personDao.add(); } }
PersonServiceBean.java(使用@Autowired)
package com.dwt1220; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; public class PersonServiceBean implements PersonService { /** @Autowired注解是按类型装配依赖对象,默认情况下它要求依赖对象必须存在, * 如果允许null值,可以设置它required属性为false。 * 如果想使用按名称装配,可以结合@Qualifier注解一起使用 */ @Autowired(required = false)@Qualifier("personDao1") private PersonDao personDao; @Autowired public void setPersonDao(PersonDao personDao) { this.personDao = personDao; } public void save() { personDao.add(); } }
<?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"> <context:annotation-config/> <bean id="personDao1" class="com.dwt1220.PersonDaoBean" /> <bean id="personService" class="com.dwt1220.PersonServiceBean"/> </beans>
自动装配依赖对象