Spring依赖注入_set注入_构造器注入_注解注入
属性setter方法注入_方式一(通用bean)_方法二(使用内部bean)_ 基本类型的注入_集合类型的注入
一、在beans.xml中,配置要被注入的bean.
二、在需要注入的bean配置中添加
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)
方法二(使用内部bean,但该bean不能被其他bean使用)
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();
}
}
基本类型的注入
在
集合类型的注入
1、beans.xml
第一个set
第二个set
第三个set
第一个list
第二个list
第三个list
value1
value2
value4
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 getSets();
public List getLists();
public Properties getProperties();
public Map 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 sets = new HashSet();
private List lists = new ArrayList();
private Properties properties = new Properties();
private Map maps = new HashMap();
public Set getSets() {
return sets;
}
public void setSets(Set sets) {
this.sets = sets;
}
public List getLists() {
return lists;
}
public void setLists(List lists) {
this.lists = lists;
}
public Properties getProperties() {
return properties;
}
public void setProperties(Properties properties) {
this.properties = properties;
}
public Map getMaps() {
return maps;
}
public void setMaps(Map 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 sets=personService.getSets();
List lists=personService.getLists();
Properties properties = personService.getProperties();
Map 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中申明被注入的类
3、在注入类的
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
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();
}
}
自动装配依赖对象