Spring依赖注入方式_属性setter方法注入_构造器注入_注解注入
2012-02-16 16:48
856 查看
Spring依赖注入_set注入_构造器注入_注解注入
属性setter方法注入_方式一(通用bean)_方法二(使用内部bean)_ 基本类型的注入_集合类型的注入
一、在beans.xml中,配置要被注入的bean.
二、在需要注入的bean配置中添加<property name="personDao" ref="personDao" />设置
1、被注入类接口 及 被注入的bean。
PersonDao.java
PersonDaoBean.java
2.需要注入bean的类,及接口
PersonService.java
PersonServiceBean.java
3.beans.xml 配置文件 方式一(通用bean)
方法二(使用内部bean,但该bean不能被其他bean使用)
4.Test.java
基本类型的注入
在<bean>标签体内添加 <property name="string" value="stringvalue"/>。
集合类型的注入
1、beans.xml
2、PersonService.java 接口
3、PersonServiceBean.java 类
4、Test.java
构造器注入
1、在需要注入的类中,编写注入的构造方法
2、在beans.xml中申明被注入的类
3、在注入类的<bean>标签体中使用<constructor-arg>标签
PersonServiceBean.java类
beans.xml
Test.java
注解注入
1、所需配置
在java代码中使用@Autowired或@Resource注解方式进行装配。但我们需要在xml配置文件中配置以下信息:
<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/>
</beans>
这个配置隐式注册了多个对注释进行解析处理的处理器:AutowiredAnnotationBeanPostProcessor,CommonAnnotationBeanPostProcessor,PersistenceAnnotationBeanPostProcessor,RequiredAnnotationBeanPostProcessor
注: @Resource注解在spring安装目录的lib\j2ee\common-annotations.jar
在java代码中使用@Autowired或@Resource注解方式进行装配,这两个注解的区别是:@Autowired 默认按类型装配,@Resource默认按名称装配,当找不到与名称匹配的bean才会按类型装配。
@Autowired注解是按类型装配依赖对象,默认情况下它要求依赖对象必须存在,如果允许null值,可以设置它required属性为false。如果我们想使用按名称装配,可以结合@Qualifier注解一起使用。如下:
@Resource注解和@Autowired一样,也可以标注在字段或属性的setter方法上,但它默认按名称装配。名称可以通过@Resource的name属性指定,如果没有指定name属性,当注解标注在字段上,即默认取字段的名称作为bean名称寻找依赖对象,当注解标注在属性的setter方法上,即默认取属性名作为bean名称寻找依赖对象。
@Resource(name=“personDaoBean”)
private PersonDao personDao;//用于字段上
注意:如果没有指定name属性,并且按照默认的名称仍然找不到依赖对象时, @Resource注解会回退到按类型装配。但一旦指定了name属性,就只能按名称装配了。
一般使用@Resource
PersonServiceBean.java(使用@Resource)
PersonServiceBean.java(使用@Autowired)
beans.xml
自动装配依赖对象
对于自动装配,大家了解一下就可以了,实在不推荐大家使用。例子:
<bean id="..." class="..." autowire="byType"/>
autowire属性取值如下:
byType:按类型装配,可以根据属性的类型,在容器中寻找跟该类型匹配的bean。
如果发现多个,那么将会抛出异常。如果没有找到,即属性值为null。
byName:按名称装配,可以根据属性的名称,在容器中寻找跟该属性名相同的bean,
如果没有找到,即属性值为null。
constructor:与byType的方式类似,不同之处在于它应用于构造器参数。
如果在容器中没有找到与构造器参数类型一致的bean,那么将会抛出异常。
autodetect:通过bean类的自省机制(introspection)来决定是使用constructor还是byType方式进行自动装配。
如果发现默认的构造器,那么将使用byType方式。
属性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、所需配置
在java代码中使用@Autowired或@Resource注解方式进行装配。但我们需要在xml配置文件中配置以下信息:
<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/>
</beans>
这个配置隐式注册了多个对注释进行解析处理的处理器:AutowiredAnnotationBeanPostProcessor,CommonAnnotationBeanPostProcessor,PersistenceAnnotationBeanPostProcessor,RequiredAnnotationBeanPostProcessor
注: @Resource注解在spring安装目录的lib\j2ee\common-annotations.jar
在java代码中使用@Autowired或@Resource注解方式进行装配,这两个注解的区别是:@Autowired 默认按类型装配,@Resource默认按名称装配,当找不到与名称匹配的bean才会按类型装配。
@Autowired private PersonDao personDao;//用于字段上 @Autowired public void setOrderDao(OrderDao orderDao) {//用于属性的setter方法上 this.orderDao = orderDao; }
@Autowired注解是按类型装配依赖对象,默认情况下它要求依赖对象必须存在,如果允许null值,可以设置它required属性为false。如果我们想使用按名称装配,可以结合@Qualifier注解一起使用。如下:
@Autowired @Qualifier("personDaoBean") private PersonDao personDao;
@Resource注解和@Autowired一样,也可以标注在字段或属性的setter方法上,但它默认按名称装配。名称可以通过@Resource的name属性指定,如果没有指定name属性,当注解标注在字段上,即默认取字段的名称作为bean名称寻找依赖对象,当注解标注在属性的setter方法上,即默认取属性名作为bean名称寻找依赖对象。
@Resource(name=“personDaoBean”)
private PersonDao personDao;//用于字段上
注意:如果没有指定name属性,并且按照默认的名称仍然找不到依赖对象时, @Resource注解会回退到按类型装配。但一旦指定了name属性,就只能按名称装配了。
一般使用@Resource
PersonServiceBean.java(使用@Resource)
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(); } }
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" 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>
自动装配依赖对象
对于自动装配,大家了解一下就可以了,实在不推荐大家使用。例子:
<bean id="..." class="..." autowire="byType"/>
autowire属性取值如下:
byType:按类型装配,可以根据属性的类型,在容器中寻找跟该类型匹配的bean。
如果发现多个,那么将会抛出异常。如果没有找到,即属性值为null。
byName:按名称装配,可以根据属性的名称,在容器中寻找跟该属性名相同的bean,
如果没有找到,即属性值为null。
constructor:与byType的方式类似,不同之处在于它应用于构造器参数。
如果在容器中没有找到与构造器参数类型一致的bean,那么将会抛出异常。
autodetect:通过bean类的自省机制(introspection)来决定是使用constructor还是byType方式进行自动装配。
如果发现默认的构造器,那么将使用byType方式。
相关文章推荐
- Spring第六弹—-依赖注入之使用构造器注入与使用属性setter方法注入
- spring的两种属性注入方式setter注入和构造器注入或者自动注入
- spring四种依赖注入方式 (set,构造器,工厂,注解 )
- ######Spring第5天ssh整合(注解版)难理解知识点:【自定义 方法加注解的方式】给dao注入sessionFactory属性+【搞清@Autowared和@Resource含义】
- Spring的两种依赖注入方式:setter方法注入与构造方法注入 以及两种方式的区别!
- Spring中使用事务搭建转账环境方法二 相对简便的注解方法 ——配置文件注入对象属性需要setter方法 注解方法,不需要生成setter方法
- Spring的两种依赖注入方式:setter方法注入与构造方法注入
- Spring的两种依赖注入方式:setter方法注入与构造方法注入
- Spring 配置bean, 注解方式配置 xml方式要生产set方法方便属性注入,注解方式不需要,利用反射机制注入
- Spring(六)属性依赖注入(构造方法,setter方法,p命名空间,spel表达式,集合注入)
- spring Ioc 依赖注入的三种方式:构造函数注入、setter方法注入和接口注入
- [优质例子存档]Spring的两种依赖注入方式:setter方法注入与构造方法注入
- Spring中使用事务搭建转账环境方法二 相对简便的注解方法 ——配置文件注入对象属性需要setter方法 注解方法,不需要生成setter方法
- 从头认识Spring-2.3 注解装配-@autowired(3)-通过构造器方法注入
- Spring - 基于 Setter 方法的依赖注入
- Spring IOC三种注入方式(接口注入、setter注入、构造器注入)
- Spring bean属性注入(Setter 方法)
- Spring IOC三种注入方式(接口注入、setter注入、构造器注入)
- Spring DI依赖注入setter,构造,注解,三种依赖方式的总结
- 详解Spring通过@Value注解注入属性的几种方式